#!/usr/bin/env python

# Copyright (C) 2007-2008 Instituto Nokia de Tecnologia. All rights reserved.
# Contact: Eduardo Lima (Etrunko) <eduardo.lima@indt.org.br>
#
# This file is part of Canola.
#
# This software, including documentation, is protected by copyright
# controlled by Instituto Nokia de Tecnologia. All rights are reserved.
# Copying, including reproducing, storing, adapting or translating, any
# or all of this material requires the prior written consent of
# Instituto Nokia de Tecnologia. This material also contains
# confidential information which may not be disclosed to others without
# the prior written consent of Instituto Nokia de Tecnologia.

import sys
import os, os.path
from optparse import OptionParser

progs = ["disable-plugin",
         "enable-plugin",
         "get-prefs",
         "install-collection",
         "list-plugins",
         "rescan-collections",
         "set-prefs",
         "uninstall-collection"]

arg_count = 1

# How we are called?
prog = os.path.basename(sys.argv[0])
progname = prog[4:]

opt_parser = OptionParser()
opt_parser.add_option("-c", "--config", action="store", type="string",
                      default=None, dest="config_file",
                      help="Path to canola configuration file")

# Sanity checks
if progname not in progs:
    if len(sys.argv) < 2:
        opt_parser.print_help()
        sys.exit(1)

    if sys.argv[1] not in progs:
        print "You must specify one of the following programs: ", progs
        sys.exit(1)

    progname = sys.argv[1]
    arg_count += 1

terra_progargs = sys.argv[arg_count:]
terra_progargs.insert(0, "terra-%s" % progname)

opts, args = opt_parser.parse_args()

if not opts.config_file:
    default_config_file = os.getenv("CNL_CONFIG_FILE",
                                    os.path.join(os.sep, "etc", "canola.conf"))
    terra_progargs.insert(1, "--config")
    terra_progargs.insert(2, default_config_file)

os.execvp(terra_progargs[0], terra_progargs)
