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

# CONSTANTS
APPNAME = "reagent"
APPVERSION = "1.0"
def print_help():

    print_info("Sabayon Linux "+APPNAME+" (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("database")+brown("\t Entropy database tool manager"))
    print_info(" \t\t"+red("--initialize")+"\t\t\t (Re)Initialize the Entropy packages database [DO NOT USE THIS]")
    print_info(" \t\t"+green("search")+"\t\t\t\t Search a package inside the Entropy packages database")
    print_info(" \t\t"+green("query")+"\t\t\t\t Database querying functions")
    print_info(" \t\t\t"+red("needed")+"\t\t print runtime libraries needed for the provided atoms")
    print_info(" \t\t\t"+red("depends")+"\t\t search which packages depend on the provided atoms")
    print_info(" \t\t\t"+red("tags")+"\t\t search packages that have the specified tags")
    print_info(" \t\t\t"+red("files")+"\t\t list files owned by the provided atoms")
    print_info(" \t\t\t"+red("belongs")+"\t\t search from what package a file belongs [*filename* allowed]")
    print_info(" \t\t\t"+red("description")+"\t search packages by description")
    print_info(" \t\t\t"+red("eclass")+"\t\t search packages using the specified eclasses")
    print_info(" \t\t\t"+brown("--verbose")+"\t show more details")
    print_info(" \t\t\t"+brown("--quiet")+"\t\t print results in a scriptable way")
    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\t"+green("depsregen")+"\t\t\t Recalculate and update package dependencies in repository")
    print_info(" \t\t\t"+red("--justscan")+"\t Just show differences between results")
    print_info(" \t\t\t"+red("--nochecksum")+"\t Disable checksum verification")
    print_info(" \t\t"+green("contentregen")+"\t\t\t Recalculate and update package content metadata in repository")
    print_info(" \t\t\t"+red("--justscan")+"\t Just show differences between results")
    print_info(" \t\t\t"+red("--nochecksum")+"\t Disable checksum verification")

    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\t just print the dependencies list")
    print_info(" \t"+green("cleanup")+brown("\t\t to clean temporary files"))

options = argv[1:]

from entropyConstants import *
from serverConstants import *

# print version
if (' '.join(options).find("--version") != -1) or (' '.join(options).find(" -V") != -1):
    print_generic(APPNAME+": "+APPVERSION)
    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")
    exit(1)

import entropyTools
import reagentTools
import databaseTools

# 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
        else:
            _options.append(opt)
options = _options

if (not entropyTools.isRoot()):
    print_error("you must be root in order to run "+APPNAME)

elif (options[0] == "update"):
    rc = reagentTools.update(options[1:])
    exit(rc)

elif (options[0] == "inject"):
    rc = reagentTools.inject(options[1:])
    exit(rc)

# database tool
elif (options[0] == "database"):
    reagentTools.database(options[1:])
    exit(0)

# deptest tool
elif (options[0] == "deptest"):
    rc = reagentTools.dependenciesTest()
    exit(rc)

elif (options[0] == "libtest"):
    rc = reagentTools.librariesTest()
    exit(rc)

# deptest tool
elif (options[0] == "depends"):
    rc = reagentTools.dependsTableInitialize(runActivator = False)
    exit(rc)

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

# deptest tool
elif (options[0] == "spm"):
    rc = reagentTools.spm(options[1:])
    exit(rc)
