#!/usr/bin/env python

# Cherokee CLI configuration conversion
#
# Authors:
#      Alvaro Lopez Ortega <alvaro@alobbs.com>
#
# Copyright (C) 2001-2009 Alvaro Lopez Ortega
# This file is distributed under the GPL2 license.

import sys

from config import *
from configured import *
from config_version import *


def main():
    # Read the command line parameter
    try:
        cfg_file = sys.argv[1]
    except:
        print "Incorrect parameters: %s CONFIG_FILE" % (sys.argv[0])
        raise SystemExit

    # Parse the configuration file
    cfg = Config(cfg_file)

    # Update the configuration file if needed
    ver_config  = int (cfg.get_val('config!version', 0))
    ver_release = int (config_version_get_current())

    print "Upgrading '%s' from %d to %d.." % (cfg_file, ver_config, ver_release),
    
    # Convert it
    updated = config_version_update_cfg (cfg)
    print ["Not upgraded.", "Upgraded."][updated]

    # Save it
    if updated:
        print "Saving new configuration..",
        cfg.save()
        print "OK"

if __name__ == '__main__':
    main()
