#!/usr/bin/python
'''
    # DESCRIPTION:
    # this application gets a .tbz2 file as input and creates a database entry
    # with all the information needed by the Entropy 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
'''

# Never do "import portage" here, please use entropyTools binding

import os, sys
sys.path.insert(0,'../libraries')
sys.path.insert(1,'../client')
sys.path.insert(2,'../server')
sys.path.insert(3,'/usr/lib/entropy/client')
sys.path.insert(4,'/usr/lib/entropy/libraries')
sys.path.insert(5,'/usr/lib/entropy/server')
from outputTools import *

# CONSTANTS
def print_help():

    print_info("Sabayon Linux "+sys.argv[0]+" (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(blue("Tools available: "))

    print_info(" \t"+green("update")+brown("\t\t Update Entropy Database analyzing the system for new installed packages"))
    print_info(" \t\t"+red("--branch=<branch name>")+"\t\t Choose which branch assign to the packages")
    print_info(" \t\t"+red("--seekstore")+"\t\t\t Skip differential COUNTERS scanning and analyze STORE directory")
    print_info(" \t\t"+red("--repackage")+"\t\t\t Repackage specified atoms")
    print_info(" \t\t"+red("--noask")+"\t\t\t\t Automatically handle things without questioning")

    print_info(" \t"+green("inject")+brown("\t\t Inject .tbz2s into database without removing others in the scope (multipackage)"))
    print_info(" \t\t"+red("--branch=<branch name>")+"\t\t Choose which branch assign to the packages")

    print_info(" \t"+green("query")+brown("\t Entropy Query manager"))
    print_info(" \t\t"+red("search")+"\t\t Search a package inside the Entropy packages database")
    print_info(" \t\t"+red("needed")+"\t\t print runtime libraries needed for the provided atoms")
    print_info(" \t\t"+red("depends")+"\t\t search which packages depend on the provided atoms")
    print_info(" \t\t"+red("tags")+"\t\t search packages that have the specified tags")
    print_info(" \t\t"+red("files")+"\t\t list files owned by the provided atoms")
    print_info(" \t\t"+red("belongs")+"\t\t search from what package a file belongs [*filename* allowed]")
    print_info(" \t\t"+red("description")+"\t search packages by description")
    print_info(" \t\t"+red("eclass")+"\t\t search packages using the specified eclasses")
    print_info(" \t\t"+brown("--verbose")+"\t show more details")
    print_info(" \t\t"+brown("--quiet")+"\t\t print results in a scriptable way")


    print_info(" \t"+green("database")+brown("\t Entropy database tool manager"))
    print_info(" \t\t"+red("--initialize")+"\t\t\t Initialize the Entropy packages database")
    print_info(" \t\t\t"+red("--empty")+"\t\t\t do not fill database with packages")
    print_info(" \t\t\t"+red("--repo=<repoid>")+"\t\t create database for the specified repo id")
    print_info(" \t\t"+green("bump")+"\t\t\t\t Do a revision bump on the database")
    print_info(" \t\t\t"+red("--sync")+"\t\t\t synchronize the database")
    print_info(" \t\t"+green("remove")+"\t\t\t\t Remove a package or a list of packages")
    print_info(" \t\t\t"+red("--branch=<branch name>")+"\t Choose which branch of the package to remove")
    print_info(" \t\t"+green("multiremove")+"\t\t\t Remove injected packages (all if no atom specified, multipackage)")
    print_info(" \t\t\t"+red("--branch=<branch name>")+"\t Choose which branch of the package to remove")
    print_info(" \t\t"+green("create-empty-database")+"\t\t Create an empty Entropy database file in the specified <path>")
    print_info(" \t\t"+green("switchbranch")+"\t\t\t Switch to the specified branch, a package, a list of packages, world")
    print_info(" \t\t"+green("md5check")+"\t\t\t Check digest of a package, a list of packages, world")
    print_info(" \t\t"+green("md5remote")+"\t\t\t Compare digest of a package between database and mirrors")

    print_info(" \t"+green("spm")+brown("\t\t Source Package Manager tool manager"))
    print_info(" \t\t"+green("compile")+"\t\t\t Start SPM and compile something")
    print_info(" \t\t\t"+red("categories")+"\t compile provided categories")
    print_info(" \t\t\t"+brown("--list")+"\t\t list packages and quit")

    print_info(" \t"+green("deptest")+brown("\t\t Look for unsatisfied dependencies inside database"))
    print_info(" \t"+green("libtest")+brown("\t\t Look for broken libraries on the live system"))
    print_info(" \t"+green("depends")+brown("\t\t Regenerate depends table (plus database lock and bump)"))
    print_info(" \t\t"+red("--quiet")+"\t\t\t just print the dependencies list")
    print_info(" \t"+green("cleanup")+brown("\t\t to clean temporary files"))

options = sys.argv[1:]

from entropyConstants import *

# print version
if (' '.join(options).find("--version") != -1) or (' '.join(options).find(" -V") != -1):
    print_generic(sys.argv[0]+": "+etpConst['entropyversion'])
    sys.exit(0)

# print help
if len(options) < 1 or ' '.join(options).find("--help") != -1 or ' '.join(options).find(" -h") != -1:
    print_help()
    if len(options) < 1:
	print_error("not enough parameters")
    sys.exit(1)

import entropyTools

# preliminary options parsing
_options = []
for opt in options:
    if (opt == "--nocolor"):
        nocolor()
    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
        else:
            _options.append(opt)
options = _options

if not entropyTools.isRoot():
    print_error("you must be root in order to run "+sys.argv[0])

elif (options[0] == "update"):
    import server_reagent
    rc = server_reagent.update(options[1:])
    server_reagent.Entropy.close_server_databases()
    sys.exit(rc)

elif (options[0] == "inject"):
    import server_reagent
    rc = server_reagent.inject(options[1:])
    server_reagent.Entropy.close_server_databases()
    sys.exit(rc)

# database tool
elif (options[0] == "database"):
    import server_reagent
    server_reagent.database(options[1:])
    server_reagent.Entropy.close_server_databases()
    sys.exit(0)

elif (options[0] == "query"):
    import server_query
    rc = server_query.query(options[1:])
    sys.exit(rc)

# deptest tool
elif (options[0] == "deptest"):
    import server_reagent
    server_reagent.Entropy.dependencies_test()
    server_reagent.Entropy.close_server_databases()
    sys.exit(0)

elif (options[0] == "libtest"):
    import server_reagent
    rc = server_reagent.Entropy.libraries_test()
    server_reagent.Entropy.close_server_databases()
    sys.exit(rc)

# deptest tool
elif (options[0] == "depends"):
    import server_reagent
    rc = server_reagent.Entropy.depends_table_initialize()
    server_reagent.Entropy.close_server_databases()
    sys.exit(rc)

# cleanup
elif (options[0] == "cleanup"):
    rc = entropyTools.cleanup()
    sys.exit(rc)

# deptest tool
elif (options[0] == "spm"):
    import server_reagent
    rc = server_reagent.spm(options[1:])
    server_reagent.Entropy.close_server_databases()
    sys.exit(rc)
