#!/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 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
import sys
import string

sys.path.append('../libraries')
sys.path.append('../client')
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")+yellow("\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"+green("database")+yellow("\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("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("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"+green("deptest")+yellow("\t\t Look for unsatisfied dependencies inside database"))
    print_info(" \t"+green("depends")+yellow("\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("smartapps")+yellow("\t Entropy application tool to create self-dependant applications [EXPERIMENTAL]"))
    print_info(" \t\t"+green("create")+"\t\t\t\t Create a smartapp package using the package names provided")
    print_info(" \t"+green("cleanup")+yellow("\t\t to clean temporary files"))

options = sys.argv[1:]

# print version
if (string.join(options).find("--version") != -1) or (string.join(options).find(" -V") != -1):
    print_generic(APPNAME+": "+APPVERSION)
    sys.exit(0)

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

import entropyTools
import reagentTools
import databaseTools
from entropyConstants import *
from serverConstants import *

# preliminary options parsing
_options = []
for opt in options:
    if (opt == "--nocolor"):
	entropyTools.nocolor()
    elif (opt == "--debug"):
	entropyTools.enableDebug()
    else:
	_options.append(opt)
options = _options



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


elif (options[0] == "generator"):
    reagentTools.generator(options[1:])
    sys.exit(0)

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

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

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

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

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

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