# -*- coding: utf-8 -*-
#
# This file is part of PycWeather
#
# Copyright (c) 2008-2009 Vlad Glagolev <enqlave@gmail.com>
# All rights reserved.
#
# Permission to use, copy, modify, and 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.

""" pycweather/dmanager.py: weather display manager """

import getopt
import os
import sys

from pycweather import __version__
from pycweather import weather


# default values stay for Moscow (RU) location, 1 days to display (means only
# current day), metric measurement system and internal XSL stylesheet usage
_CODE = "RSXX0063"
_DAYS = 1
_UNIT = "m"


def usage():
    print """Usage: %s [options]\n
    -h, --help		show this help
    -v, --version	version information
    -a, --auth <id:key> authority data in format: partner-id:license-key
    -c, --code <code>	location ID code (default is %s)
    -d, --days <days>	number of days to display [1-5] (default is %d)
    -u, --unit <unit>	measurement system [m]etric|[i]mperial (default is %s)
    -s, --search <word>	search for the specified location (city, state, etc.)
    -x, --xsl <file>	custom XSL file\n""" % (sys.argv[0], _CODE, _DAYS, _UNIT)


def version():
    print """PycWeather version:
    %s. (major)
      %s. (minor)
        %s  (revision)""" % tuple(__version__.split("."))
    exit(0)


def main(template):
    try:
        opts, args = getopt.getopt(sys.argv[1:], "hva:c:d:u:s:x:", ["help", \
                                   "version", "auth=", "code=", "days=", \
                                   "unit=", "search=", "xsl="])
    except getopt.error, msg:
        print msg
        usage()
        exit(2)

    forecast = weather.Weather()

    for o, a in opts:
        if o in ("-h", "--help"):
            usage()
            exit(0)
        elif o in ("-v", "--version"):
            version()
        elif o in ("-a", "--auth"):
            forecast.auth(a)
        elif o in ("-c", "--code"):
            code = a
        elif o in ("-d", "--days"):
            days = int(a) if a != "" else _DAYS
        elif o in ("-u", "--unit"):
            unit = a
        elif o in ("-s", "--search"):
            forecast.search(a)
            exit(0)
        elif o in ("-x", "--xsl"):
            xsl = a

    try:
        code
    except NameError:
        code = _CODE

    try:
        days
    except NameError:
        days = _DAYS

    try:
        unit
    except NameError:
        unit = _UNIT

    try:
        xsl
    except NameError:
        xsl = template

    forecast.load(xsl)

    if forecast.id and forecast.key:
        forecast.preview(code, days, unit)
    else:
        print "Get partner ID and license key at: " + \
              "http://registration.weather.com/ursa/xmloap/step1"
