#!/usr/bin/env python
#
#    Copyright (c) 2009-2015 Tom Keffer <tkeffer@gmail.com>
#
#    See the file LICENSE.txt for your rights.
#
"""Configure the configuration file."""
from __future__ import with_statement

import sys
import syslog
import optparse

from weecfg.config import ConfigEngine

usage = """Usage: wee_config --help
       wee_config --version
       wee_config --list-drivers
       wee_config --install --dist-config=DIST_CONFIG --output=OUT_CONFIG
           [--driver=DRIVER]  [--units=(us|metric)]
           [--latitude=yy.y] [--longitude=xx.x] [--altitude=zz.z,(foot|meter)] 
           [--location="Home Sweet Home"]
           [--no-prompt] [--no-backup]
       wee_config --upgrade CONFIG_FILE|--config=CONFIG_FILE
           --dist-config=DIST_CONFIG
           [--output=OUT_CONFIG] [--no-prompt] [--no-backup] [--warn-on-error]
       wee_config --reconfigure CONFIG_FILE|--config=CONFIG_FILE
           [--driver=DRIVER]  [--units=(us|metric)]
           [--latitude=yy.y] [--longitude=xx.x] [--altitude=zz.z,(foot|meter)] 
           [--location="Home Sweet Home"]
           [--output=OUT_CONFIG] [--no-prompt] [--no-backup]

Commands:

--list-drivers  List the available weewx device drivers, then exit.
--install       Install a new configuration file starting with the contents of
                DIST_CONFIG, prompting for station parameters.
--upgrade       Update the contents of configuration file CONFIG_FILE to the
                installed version, then merge the result with the contents of
                configuration file DIST_CONFIG. 
--reconfigure   Modify an existing configuration file CONFIG_FILE with any
                specified station parameters.  Use this command with the
                driver option to change the device driver.

Station parameters:

  --driver      --units
  --latitude    --longitude
  --altitude    --location
"""

def main():

    # Set defaults for the system logger:
    syslog.openlog('wee_config', syslog.LOG_PID|syslog.LOG_CONS)

    # Create a command line parser:
    parser = optparse.OptionParser(usage=usage)
    
    # Add the various options:
    parser.add_option("--version", action="store_true",
                      help="Show the weewx version then exit.")
    parser.add_option("--list-drivers", action="store_true",
                      help="List the available device drivers.")
    parser.add_option("--install", action="store_true",
                      help="Install a new configuration file.")
    parser.add_option("--upgrade", action="store_true",
                      help="Update an existing configuration file, then merge "
                      "with contents of DIST_CONFIG.")
    parser.add_option("--reconfigure", action="store_true",
                      help="Reconfigure an existing configuration file.")
    parser.add_option("--config", dest="config_path", metavar="CONFIG_FILE",
                      help="Use configuration file CONFIG_FILE.")
    parser.add_option("--dist-config",
                      help="Use template configuration file DIST_CONFIG.")
    parser.add_option("--output", metavar="OUT_CONFIG",
                      help="Save to configuration file OUT_CONFIG.  If not "
                      "specified then replace existing configuration file.")
    parser.add_option("--driver", metavar="DRIVER",
                      help="Use the driver DRIVER. "
                      "For example, weewx.driver.vantage")
    parser.add_option("--latitude", metavar="yy.y",
                      help="The station latitude in decimal degrees.")
    parser.add_option("--longitude", metavar="xx.x",
                      help="The station longitude in decimal degrees.")
    parser.add_option("--altitude", metavar="zz,(foot|meter)",
                      help="The station altitude in either feet or meters."
                      " For example, '750,foot' or '320,meter'")
    parser.add_option("--location",
                      help="""A text description of the station."""
                      """ For example, "Santa's workshop, North Pole" """)
    parser.add_option("--units", choices=["us", "metric"], metavar="(metric|us)",
                      help="Set display units to 'metric' or 'us'.")
    parser.add_option("--no-prompt", action="store_true",
                      help="Do not prompt. Use default or specified values.")
    parser.add_option("--no-backup", action="store_true", default=False,
                      help="When replacing an existing configuration file, "
                      "do not create a backup copy.")
    parser.add_option("--warn-on-error", action="store_true", default=False,
                      help="Only warn if an update is not possible.  Default "
                      "behavior is to warn then exit.")
    parser.add_option("--debug", action="store_true",
                      help="Show diagnostic information while running.")

    # Now we are ready to parse the command line:
    (options, args) = parser.parse_args()
    
    config_mgr = ConfigEngine()

    config_mgr.run(args, options)

    sys.exit(0)

if __name__=="__main__" :
    main()
    
