#!/bin/sh
#
#tracepkg: version 1.0
#
#frontend for the smart dependency check system by the idiot of myself :)
#
#TO DO:
#
#-add support for pkgtools cmdline options such as --install-new and so on...
#-fix bugs
#-many other things...


#--------------------------------------------------------------
#export some useful values

export TPVERSION="1.1.0rc2"

#
#	fixed paths
#

#the directory for tgz data
export PKGDIR="/var/log/packages"

#it must be an existent dir! here dependencies will be stored
export LOGDIR="/var/log/dependencies"

#it must be an existent dir! here uninstalled dependencies will be stored
export MISDIR="/var/log/missing"

#installation directory
export INSTDIR="/usr/libexec/tracepkg";
#export INSTDIR=".";	#just for debugging...

#configuration directory
export CONFDIR="/etc/tracepkg"
#export CONFDIR=".";	#just for debugging...

#
#	read config's and export them
#
. $CONFDIR/tracepkg.conf
export BIT
export ENC

#attended execution flag. the behaviour is overridded by the UNATTENDED env. values
[ ! $UNATTENDED = "" ] && [ $UNATTENDED -eq 1 ] && { AUTO=1; echo FORCING UNATTENDED EXECUTION; echo; }
[ ! $UNATTENDED = "" ] && [ $UNATTENDED -eq 0 ] && { AUTO=0; echo FORCING ATTENDED EXECUTION; echo; }
export AUTO

#interruption flag
export INTERRUPTED=0;

#--------------------------------------------------------------


#--------------------------------------------------------------
#aux. functions

. $INSTDIR/core-functions

#--------------------------------------------------------------


#--------------------------------------------------------------
#main body

#trace of execution for most important operations...
[ -f /var/run/tracepkg.pid ] && {
	echo
	echo "WARNING: another istance of tracepkg seems to be running."
	echo "WARNING: if this is wrong, please remove file: /var/run/tracepkg.pid"
	echo

	exit 1
} || {
	[ "$UID" -eq 0 ] &&	touch /var/run/tracepkg.pid
}

#sanity check
checkenv

FLAG=$1;
ARGS=$#;
shift 1;

#check the arglist
case $FLAG in

#--- root-only runnable options ----------------------------------------------------------------------------

#	"--test")  $INSTDIR/dummydb-new $1 ;;	#just for debug

	#this (re)build the whole database sync-ing it with the tgz database
	"--sync") if [ "$UID" -eq 0 ]; then $INSTDIR/refreshdb;
		  else rootOnly;
		  fi ;;

	#this resync the whole database with the tgz database
	"--resync") if [ "$UID" -eq 0 ]; then $INSTDIR/resync;
		  else rootOnly;
		  fi ;;

	#this (re)build the dependency list for the given tgz
	"--build") if [ "$UID" -eq 0 ]; then buildFile $@;
		   else rootOnly;
		   fi ;;

	#remove $@ and all dependencies
	"--remove") if [ "$UID" -eq 0 ]; then
			if [ $ARGS -eq 1 ]; then usage; else $INSTDIR/removedb $@; fi ;
		    else rootOnly;
		    fi ;;

	#remove all roots of $@ (all tgz that use $@ and are not used by any tgz)
	"--remove-root") if [ "$UID" -eq 0 ]; then
			 	if [ $ARGS -eq 1 ]; then usage; else removeFromRoot $@; fi ;
			 else rootOnly;
			 fi ;;

	#install $@ and find all dependencies
	"--install") if [ "$UID" -eq 0 ]; then
			if [ $ARGS -eq 1 ]; then usage; else $INSTDIR/installdb $@; fi ;
		     else rootOnly;
		     fi ;;

	#upgrade $@
	"--upgrade") if [ "$UID" -eq 0 ]; then
			if [ $ARGS -eq 1 ]; then usage; else $INSTDIR/upgradedb --nooption $@; fi ;
		     else rootOnly;
		     fi ;;

	#upgrade $@ using --install-new option
	"--install-new") if [ "$UID" -eq 0 ]; then
				if [ $ARGS -eq 1 ]; then usage; else $INSTDIR/upgradedb --install-new $@; fi ;
		     	 else rootOnly;
		         fi ;;

	#upgrade $@ using --reinstall option
	"--reinstall") if [ "$UID" -eq 0 ]; then
				if [ $ARGS -eq 1 ]; then usage; else $INSTDIR/upgradedb --reinstall $@; fi ;
		     	 else rootOnly;
		         fi ;;

#--- user runnable options ----------------------------------------------------------------------------

	#scan database for all tgz dependant on $@
	"-s") scanDatabase $@ ;;

	#scan database for all dependencies of $@
	"-d") getInstalledDependencies $@ ;;

	#scan database for all missing dependencies of $@
	"-m") getMissingDependencies $@ ;;

	#print out a verbose dependency check
	"-v") dummyBuild $@ ;;

	#show version
	"-V") echo -e "\ntracepkg v.$TPVERSION\n" ;;

	*) usage;;
esac

#remove execution trace
rm /var/run/tracepkg.pid 2> /dev/null

#--------------------------------------------------------------

#EOF