#!/usr/bin/python
'''
    # DESCRIPTION:
    # Entropy Package Manager client

    Copyright (C) 2007-2008 Fabio Erculiani

    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
sys.path.append('../libraries')
sys.path.append('../client')
sys.path.append('/usr/lib/entropy/client')
sys.path.append('/usr/lib/entropy/libraries')
from entropyConstants import *
from outputTools import *

def print_help():
    print_info("Sabayon Linux "+darkred("Equo Package Manager")+" (C - 2007)")
    print_info("General Options:")
    print_info("  --help\t\tthis output")
    print_info("  --version\t\tprint version")
    print_info("  --nocolor\t\tdisable colorized output")
    print_info(red("Tools available: "))
    print_info(" \t"+blue("update")+brown("\t\t update repositories (download new data)"))
    print_info(" \t\t"+red("--force")+"\t\t\t force sync regardless repositories status")
    print_info(" \t"+blue("repoinfo")+brown("\t show enabled repositories"))
    print_info(" \t"+blue("status")+brown("\t\t show respositories status"))

    print_info(" \t"+blue("search")+brown("\t\t search a package trough repositories"))

    print_info(" \t"+blue("world")+brown("\t\t update system with the latest available packages"))
    print_info(" \t\t"+red("--ask")+"\t\t\t ask before making any changes")
    print_info(" \t\t"+red("--fetch")+"\t\t\t just download packages")
    print_info(" \t\t"+red("--pretend")+"\t\t just show what would be done")
    print_info(" \t\t"+red("--verbose")+"\t\t show more details about what's going on")
    print_info(" \t\t"+red("--replay")+"\t\t reinstall all the packages and their dependencies")
    print_info(" \t\t"+red("--empty")+"\t\t\t same as --replay")
    print_info(" \t\t"+red("--resume")+"\t\t resume previously interrupted operations")
    print_info(" \t\t"+red("--skipfirst")+"\t\t used with --resume, first package in queue will be skipped")
    print_info(" \t\t"+red("--upgrade")+"\t\t upgrade "+etpConst['systemname']+" to the specified release (3.5,3.6...)")
    print_info(" \t\t"+red("--nochecksum")+"\t\t disable package integrity check")

    print_info(" \t"+blue("security")+brown("\t\t security infrastructure tools"))
    print_info(" \t\t"+darkgreen("update")+red("\t\t\t download the latest Security Advisories"))
    print_info(" \t\t"+darkgreen("list")+red("\t\t\t list all the available Security Advisories"))
    print_info(" \t\t\t"+red("--affected")+"\t list only affected")
    print_info(" \t\t\t"+red("--unaffected")+"\t list only unaffected")
    print_info(" \t\t"+darkgreen("info")+red("\t\t\t show information about provided advisories identifiers"))
    print_info(" \t\t"+darkgreen("install")+red("\t\t\t automaticly install all the available security updates"))
    print_info(" \t\t\t"+red("--ask")+"\t\t\t ask before making any changes")
    print_info(" \t\t\t"+red("--fetch")+"\t\t\t just download packages")
    print_info(" \t\t\t"+red("--pretend")+"\t\t just show what would be done")
    print_info(" \t\t"+red("--quiet")+"\t\t\t show less details (useful for scripting)")

    print_info(" \t"+blue("install")+brown("\t\t install one or more packages or .tbz2"))
    print_info(" \t\t"+red("--ask")+"\t\t\t ask before making any changes")
    print_info(" \t\t"+red("--pretend")+"\t\t just show what would be done")
    print_info(" \t\t"+red("--fetch")+"\t\t\t just download packages without doing the install")
    print_info(" \t\t"+red("--nodeps")+"\t\t do not manage any dependency")
    print_info(" \t\t"+red("--resume")+"\t\t resume previously interrupted operations")
    print_info(" \t\t"+red("--skipfirst")+"\t\t used with --resume, first package in queue will be skipped")
    print_info(" \t\t"+red("--empty")+"\t\t\t set all dependencies as unsatisfied")
    print_info(" \t\t"+red("--deep")+"\t\t\t analyze dependencies deeply")
    print_info(" \t\t"+red("--verbose")+"\t\t show more details about what's going on")
    print_info(" \t\t"+red("--configfiles")+"\t\t also remove old configuration files [use with care]")
    print_info(" \t\t"+red("--nochecksum")+"\t\t disable package integrity check")

    print_info(" \t"+blue("remove")+brown("\t\t remove one or more packages"))
    print_info(" \t\t"+red("--deep")+"\t\t\t also pull unused dependencies where depends list is empty")
    print_info(" \t\t"+red("--configfiles")+"\t\t also remove configuration files")
    print_info(" \t\t"+red("--resume")+"\t\t resume previously interrupted operations")
    print_info(" \t"+blue("deptest")+brown("\t\t look for unsatisfied dependencies"))
    print_info(" \t\t"+red("--quiet")+"\t\t\t show less details (useful for scripting)")
    print_info(" \t\t"+red("--ask")+"\t\t\t ask before making any changes")
    print_info(" \t\t"+red("--pretend")+"\t\t just show what would be done")
    print_info(" \t"+blue("libtest")+brown("\t\t look for missing libraries"))
    print_info(" \t\t"+red("--listfiles")+"\t\t print broken files to stdout")
    print_info(" \t\t"+red("--quiet")+"\t\t\t show less details (useful for scripting)")
    print_info(" \t\t"+red("--ask")+"\t\t\t ask before making any changes")
    print_info(" \t\t"+red("--pretend")+"\t\t just show what would be done")

    print_info(" \t"+blue("conf")+brown("\t\t configuration files update tool"))
    print_info(" \t\t"+darkgreen("info")+red("\t\t\t show info about configuration files that should be updated"))
    print_info(" \t\t"+darkgreen("update")+red("\t\t\t run the configuration files update tool"))

    print_info(" \t"+blue(bold("query"))+brown("\t\t do misc queries on repository and local databases"))
    print_info(" \t\t"+darkgreen("installed")+red("\t\t search a package into the local database"))
    print_info(" \t\t"+darkgreen("belongs")+red("\t\t\t search from what package a file belongs [*filename* allowed]"))
    print_info(" \t\t"+darkgreen("depends")+red("\t\t\t search which packages depend on the provided atoms"))
    print_info(" \t\t"+darkgreen("needed")+red("\t\t\t print runtime libraries needed for the provided atoms"))
    print_info(" \t\t"+darkgreen("files")+red("\t\t\t list files owned by the provided atoms"))
    print_info(" \t\t"+darkgreen("removal")+red("\t\t\t print the removal tree for specified atoms"))
    print_info(" \t\t"+darkgreen("tags")+red("\t\t\t search packages that have the specified tags"))
    print_info(" \t\t"+darkgreen("slot")+red("\t\t\t search packages that have the specified slot"))
    print_info(" \t\t\t"+red("--deep")+"\t\t also pull unused dependencies where depends list is empty")
    print_info(" \t\t"+darkgreen("list")+red("\t\t\t list packages based on the chosen parameter below"))
    print_info(" \t\t\t"+green("installed")+red("\t list installed packages"))
    print_info(" \t\t"+darkgreen("orphans")+red("\t\t\t search files that don't belong to any package")+bold(" [USE WITH CARE]"))
    print_info(" \t\t"+darkgreen("description")+red("\t\t search packages by description"))
    print_info(" \t\t"+red("--verbose")+"\t\t show more details")
    print_info(" \t\t"+red("--quiet")+"\t\t\t print results in a scriptable way")

    print_info(" \t"+blue("smart")+brown("\t\t handle extended functionalities"))
    print_info(" \t\tUnpack and run applications")
    print_info(" \t\t"+darkgreen("application")+red("\t\t generate a smart application for the provided atoms (experimental)"))
    print_info(" \t\t\t"+red("--empty")+"\t\t set all dependencies as unsatisfied")
    print_info(" \t\tA .tbz2 package that can contain multiple packages  (Gentoo compatible)")
    print_info(" \t\t"+darkgreen("package")+red("\t\t\t generate a smart package for the provided atoms"))
    print_info(" \t\t"+darkgreen("quickpkg")+red("\t\t repackage installed files belonging to the provided atoms"))
    print_info(" \t\t\t"+red("--savedir")+"\t save new packages to the specified directory")
    print_info(" \t\t"+darkgreen("inflate")+red("\t\t\t convert provided Gentoo .tbz2 into Entropy ones (Portage needed)"))
    print_info(" \t\t\t"+red("--savedir")+"\t save new packages to the specified directory")
    print_info(" \t\t"+darkgreen("deflate")+red("\t\t\t convert provided Entropy .tbz2 into Gentoo ones (Portage needed)"))
    print_info(" \t\t\t"+red("--savedir")+"\t save new packages to the specified directory")
    print_info(" \t\t"+darkgreen("extract")+red("\t\t\t extract Entropy metadata from provided .tbz2 packages"))
    print_info(" \t\t\t"+red("--savedir")+"\t save new metadata to the specified directory")

    print_info(" \t"+blue("database")+brown("\t handle installed packages database"))
    print_info(" \t\t"+darkgreen("check")+red("\t\t\t Check System Database for errors"))
    print_info(" \t\t"+darkgreen("generate")+red("\t\t generate installed packages database using Portage database (Portage needed)"))
    print_info(" \t\t"+darkgreen("resurrect")+red("\t\t generate installed packages database using system content [last hope]"))
    print_info(" \t\t"+darkgreen("depends")+red("\t\t\t regenerate/generate depends caching table"))
    print_info(" \t\t"+darkgreen("counters")+red("\t\t update/generate counters table (Portage <-> Entropy packages table)"))
    print_info(" \t\t"+darkgreen("gentoosync")+red("\t\t Have you used Portage and Equo together? Run this to let Equo know the changes"))

    print_info(" \t"+blue("packages")+brown("\t handle packages helper applications"))
    print_info(" \t\t"+darkgreen("python-updater")+red("\t\t migrate all Python modules to the latest installed version"))
    print_info(" \t\t"+red("--ask")+"\t\t\t ask before making any changes")
    print_info(" \t\t"+red("--pretend")+"\t\t just show what would be done")

    print_info(" \t"+blue("cache")+brown("\t\t handle Equo On-Disk cache"))
    print_info(" \t\t"+darkgreen("clean")+red("\t\t\t clean on-disk cache"))
    print_info(" \t\t"+darkgreen("generate")+red("\t\t generate on-disk cache (to speed up Equo)"))
    print_info(" \t\t"+red("--quiet")+"\t\t\t show less details (useful for scripting)")
    print_info(" \t\t"+red("--verbose")+"\t\t show more details about what's going on")
    print_info(" \t"+blue("cleanup")+brown("\t\t remove downloaded packages and clean temporary directories (not cache)"))
    print_info(" \t"+blue("--info")+brown("\t\t show system information"))


options = sys.argv[1:]

import entropyTools
import exceptionTools

# preliminary options parsing
_options = []
for opt in options:
    if (opt == "--nocolor"):
	nocolor()
    elif (opt == "--debug"):
	entropyTools.enableDebug()
    else:
        if (opt == "--quiet"):
            etpUi['quiet'] = True
        elif (opt == "--verbose"):
            etpUi['verbose'] = True
        elif (opt == "--ask"):
            etpUi['ask'] = True
        elif (opt == "--pretend"):
            etpUi['pretend'] = True
        elif (opt == "--ihateprint"):
            etpUi['mute'] = True
        else:
            _options.append(opt)
options = _options

# print help
if (not options) or ("--help" in options):
    print_help()
    if not options:
        print_error("not enough parameters")
    sys.exit()

# print version
if (options[0] == "--version"):
    print_generic("Equo: v"+etpConst['entropyversion'])
    sys.exit(0)
elif (options[0] == "--info"):
    import text_rescue
    text_rescue.getinfo()
    sys.exit(0)

def readerrorstatus():
    try:
        f = open(etpConst['errorstatus'],"r")
        status = int(f.readline().strip())
        f.close()
        return status
    except:
        writeerrorstatus(0)
        return 0

def writeerrorstatus(status):
    try:
        f = open(etpConst['errorstatus'],"w")
        f.write(str(status))
        f.flush()
        f.close()
    except:
        pass

def reset_cache():
    try:
        from entropy import EquoInterface
        Equo = EquoInterface(noclientdb = 2)
        Equo.purge_cache()
    except:
        pass

def load_conf_cache():
    if not etpUi['quiet']: print_info(red(" @@ ")+blue("Caching equo conf"), back = True)
    import text_configuration
    try:
        oldquiet = etpUi['quiet']
        etpUi['quiet'] = True
        while 1:
            try:
                scandata = text_configuration.Equo.FileUpdates.scanfs(dcache = True)
                break
            except KeyboardInterrupt:
                continue
        etpUi['quiet'] = oldquiet
    except:
        if not etpUi['quiet']: print_info(red(" @@ ")+blue("Caching not run."))
        return
    if not etpUi['quiet']: print_info(red(" @@ ")+blue("Caching complete."))
    if scandata: # can be None
        if len(scandata) > 0: # strict check
            if not etpUi['quiet']:
                print_warning(darkgreen("There are "+str(len(scandata))+" configuration file(s) that need(s) to be updated."))
                print_warning(red("Please run: ")+bold("equo conf update"))

try:
    rc = 0
    # sync mirrors tool
    if (options[0] == "update") or (options[0] == "repoinfo") or (options[0] == "status"):
        if options[0] == "update":
            entropyTools.applicationLockCheck("update")
        import text_repositories
        rc = text_repositories.repositories(options)

    elif options[0] in ["install","remove","world","deptest","libtest"]:
        import text_ui
        if options[0] == "install":
            cr = entropyTools.applicationLockCheck("install", gentle = True)
            if (cr):
                print_warning(red("Running with ")+bold("--pretend")+red("..."))
                etpUi['pretend'] = True
        elif options[0] == "remove":
            cr = entropyTools.applicationLockCheck("remove", gentle = True)
            if (cr):
                print_warning(red("Running with ")+bold("--pretend")+red("..."))
                etpUi['pretend'] = True
        elif options[0] == "world":
            cr = entropyTools.applicationLockCheck("world", gentle = True)
            if (cr):
                print_warning(red("Running with ")+bold("--pretend")+red("..."))
                etpUi['pretend'] = True
        rc = text_ui.package(options)
        load_conf_cache()

    elif options[0] == "security":
        import text_security
        rc = text_security.security(options[1:])

    elif (options[0] == "query"):
        import text_query
        rc = text_query.query(options[1:])

    # smartapps tool
    elif (options[0] == "smart"):
        rc = 0
        if len(options) > 1:
            import text_smart
            rc = text_smart.smart(options[1:])

    elif (options[0] == "conf"):
        import text_configuration
        rc = text_configuration.configurator(options[1:])

    elif (options[0] == "cache"):
        import text_cache
        rc = text_cache.cache(options[1:])

    elif (options[0] == "search"):
        rc = 0
        if len(options) > 1:
            import text_query
            rc = text_query.searchPackage(options[1:])
        else:
            rc = -10

    elif (options[0] == "database"):
        entropyTools.applicationLockCheck("database")
        import text_rescue
        rc = text_rescue.database(options[1:])

    elif (options[0] == "packages"):
        entropyTools.applicationLockCheck("packages")
        import text_rescue
        rc = text_rescue.updater(options[1:])

    elif (options[0] == "cleanup"):
        entropyTools.applicationLockCheck("cleanup")
        entropyTools.cleanup([ etpConst['packagestmpdir'], etpConst['logdir'], etpConst['entropyunpackdir'], etpConst['packagesbindir'] ])
        rc = 0
    else:
        rc = -10

    if rc == -10:
        status = readerrorstatus()
        print_error(darkred(etpExitMessages[status]))
        # increment
        if status < len(etpExitMessages)-1:
            writeerrorstatus(status+1)
        rc = 10
    else:
        writeerrorstatus(0)
    # kill threads
    threads = entropyTools.threading.enumerate()
    for thread in threads:
        if thread.getName().startswith("download::"): # equo current download speed thread
            thread.kill()
    sys.exit(rc)
except exceptionTools.SystemDatabaseError:
    reset_cache()
    print_error(darkred(" * ")+red("Installed Packages Database not found or is corrupted. Please generate it using 'equo database' tools"))
    sys.exit(101)
except exceptionTools.OnlineMirrorError, e:
    reset_cache()
    print_error(darkred(" * ")+red(str(e)+". Cannot continue."))
    sys.exit(101)
except exceptionTools.RepositoryError, e:
    reset_cache()
    print_error(darkred(" * ")+red(str(e)+". Cannot continue."))
    sys.exit(101)
except SystemExit:
    pass
except IOError, e:
    reset_cache()
    if e.errno != 32:
        raise
except KeyboardInterrupt:
    #reset_cache()
    sys.exit(0)
#except Timeout:
#    pass
except Exception:
    reset_cache()

    Text = TextInterface()
    print_error(darkred("Hi. My name is Bug Reporter. I am sorry to inform you that Equo crashed. Well, you know, shit happens."))
    print_error(darkred("But there's something you could do to help Equo to be a better application."))
    print_error(darkred("-- EVEN IF I DON'T WANT YOU TO SUBMIT THE SAME REPORT MULTIPLE TIMES --"))
    print_error(darkgreen("Now I am showing you what happened. Don't panic, I'm here to help you."))

    entropyTools.printException()

    import traceback
    from entropy import ErrorReportInterface
    exception_data = ""
    try:
        ferror = open("/tmp/equoerror.txt","w")
        traceback.print_exc(file = ferror)
        ferror.write("\nRevision: "+etpConst['entropyversion']+"\n\n")
        exception_data = entropyTools.printException(True)
        ferror.write("\n")
        ferror.flush()
        ferror.close()
        f = open("/tmp/equoerror.txt","r")
        errorText = f.readlines()
        f.close()
        ferror = open("/tmp/equoerror.txt","aw")
        ferror.write("\n\n")
        for x in exception_data:
            ferror.write(str(x)+"\n")
        ferror.flush()
        ferror.close()
    except Exception, e:
        print
        print_error(darkred("Oh well, I cannot even write to /tmp. So, please copy the error and mail lxnay@sabayonlinux.org."))
        sys.exit(1)

    print
    print_error(darkred("!!! If the error is about missing tables in the database, just run 'equo database generate' again !!!"))
    print_error(blue("Ok, back here. Let me see if you are connected to the Internet. Yes, I am blue now, so?"))

    conntest = entropyTools.get_remote_data(etpConst['conntestlink'])
    if (conntest != False):
        print_error(darkgreen("Of course you are on the Internet..."))
        rc = Text.askQuestion("   Erm... Can I send the error to my creators so they can fix me?")
        if rc == "No":
            print_error(darkgreen("Ok, ok ok ok... Sorry!"))
            sys.exit(2)
    else:
        print_error(darkgreen("Gosh, you aren't! Well, I wrote the error to /tmp/equoerror.txt. When you want, mail the file to lxnay@sabayonlinux.org."))
        sys.exit(3)

    print_error(darkgreen("If you want to be contacted back (and actively supported), also answer the questions below:"))
    name = readtext("Your Full name: ")
    email = readtext("Your E-Mail address: ")
    errorText = ''.join(errorText)
    error = ErrorReportInterface()
    error.prepare(errorText,name,email,'\n'.join([str(x) for x in exception_data]))
    result = error.submit()
    if (result):
        print_error(darkgreen("Thank you very much. The error has been reported and hopefully, the problem will be solved as soon as possible."))
    else:
        print_error(darkred("Ugh. Cannot send the report. I saved the error to /tmp/equoerror.txt. When you want, mail the file to lxnay@sabayonlinux.org."))
        sys.exit(4)
