#!/usr/bin/python

# Copyright (C) 2009-2011, Benjamin Drung <bdrung@ubuntu.com>
#
# Permission to use, copy, modify, and/or distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

"""provides information about Ubuntu's distributions"""

import optparse
import os
import sys

from ubuntutools.distro_info import convert_date, UbuntuDistroInfo

def main():
    script_name = os.path.basename(sys.argv[0])
    usage = "%s [options]" % (script_name)
    epilog = "See %s(1) for more info." % (script_name)
    parser = optparse.OptionParser(usage=usage, epilog=epilog)

    parser.add_option("--date", dest="date", default=None,
                      help="date for calculating the version (default: today).")
    parser.add_option("-a", "--all", dest="all",
                      help="list all known versions",
                      action="store_true", default=False)
    parser.add_option("-d", "--devel", dest="devel",
                      help="latest development version",
                      action="store_true", default=False)
    parser.add_option("--lts", dest="lts",
                      help="latest long term support (LTS) version",
                      action="store_true", default=False)
    parser.add_option("-s", "--stable", dest="stable",
                      help="latest stable version",
                      action="store_true", default=False)
    parser.add_option("--supported", dest="supported",
                      help="list of all supported stable versions",
                      action="store_true", default=False)
    parser.add_option("--unsupported", dest="unsupported",
                      help="list of all unsupported stable versions",
                      action="store_true", default=False)

    (options, args) = parser.parse_args()

    if len(args) != 0:
        parser.error("This script does not take any additional parameters.")

    versions = [options.all, options.devel, options.lts, options.stable,
                options.supported, options.unsupported]
    if len([x for x in versions if x]) != 1:
        parser.error("You have to select exactly one of --all, --devel, --lts, "
                     "--stable, --supported, --unsupported.")

    if options.date is None:
        date = None
    else:
        try:
            date = convert_date(options.date)
        except ValueError:
            parser.error("Option --date needs to be a date in ISO 8601 "
                         "format.")

    if options.all:
        for distro in UbuntuDistroInfo().all:
            print distro
    elif options.devel:
        print UbuntuDistroInfo().devel(date)
    elif options.lts:
        print UbuntuDistroInfo().lts(date)
    elif options.stable:
        print UbuntuDistroInfo().stable(date)
    elif options.supported:
        for distro in UbuntuDistroInfo().supported(date):
            print distro
    elif options.unsupported:
        for distro in UbuntuDistroInfo().unsupported(date):
            print distro

if __name__ == "__main__":
    main()