#!/usr/bin/env python

'''
    Porthole
    A graphical frontend to Portage

    Copyright (C) 2003 - 2008 Fredrik Arnerup and Daniel G. Taylor,
    Brian Dolbec, William F. Wheeler, Brian Bockelman, Tommy Iorns

    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 2 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.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
'''
import sys
from getopt import getopt, GetoptError

print "Porthole: python version = ", sys.version

def usage():
    tabs = "\t\t"
    print "Usage: porthole [OPTION...]\n"
    print "  -h, --help" + tabs + "Show this help message"
    print "  -l, --local" + tabs + \
          "Run a local version (use modules in current directory)"
    print "  -v, --version" + tabs + "Output version information and exit"
    print "  -d string,"
    print "  --debug string\tOutput debugging information to stderr (terminal)"
    print "             " + tabs + "string, ALL = all debug text printed.  Any other string will"
    print "             " + tabs + "print only those texts containing 'string'"
    print "             " + tabs + "Also if PyCrash-0.4pre3 or newer is found it will use it"
    print "             " + tabs + "to try to save additional crash info to send in for debugging"
    print "  -b, [portage, pkgcore, dbus]  select one of the backends"
    print "  -p, --pkgcore" + tabs + "Use the pkgcore (aka portage-ng, portage-3) code"
   
if __name__ == "__main__":
    try:
        opts, args = getopt(sys.argv[1:], 'lvd:b:hp', ["local", "version", "debug=",
                                              "backend=", "help", "pkgcore"])
        #print opts
        #print args
    except GetoptError, e:
        print >> sys.stderr, e.msg
        usage()
        sys.exit(1)
    # run thru the options and set the DATA_PATH & imported modules properly
    # bug ==> any porthole script can be used to load both --local and installed modules
    RUN_LOCAL = False
    #print 'opts=',opts
    #print 'args=',args
    for opt, arg in opts:
        if opt in ("-l", "--local"):
            # running a local version (i.e. not installed in /usr/*)
            print "importing local modules"
            print sys.path
            import os
            print "dir = ", os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
            sys.path.insert(0, (os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) #+"/porthole"))
            print "\n"
            print sys.path
            from porthole.startup import *
            RUN_LOCAL = True
            local()

    if not RUN_LOCAL:
        print "importing installed modules"
        from porthole.startup import *
        print "inserting path to modules"
        insert_path()

    for opt, arg in opts:
        if opt in ("-p", "--pkgcore"):
            startup.BACKEND = choices["pkgcore"]
        if opt in ("-b"):
            if arg in choices:
                set_backend(arg)
            else:
                useage()

    for opt, arg in opts:
        print "opt, arg ", opt, arg, type(arg)
        if opt in ("-v", "--version"):
            print_version()
        elif opt in ("-d", "--debug"):
            set_debug(arg)
        elif opt in ("-h", "--help"):
            usage()
    print "starting main()"
    main()
    sys.exit()

