#!/usr/bin/env python
#
# This file is part of Canola
# Copyright (C) 2007-2009 Instituto Nokia de Tecnologia
# Contact: Renato Chencarek <renato.chencarek@openbossa.org>
#          Eduardo Lima (Etrunko) <eduardo.lima@openbossa.org>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#

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)
