#!/bin/sh
#
#tracepkg: version 1.1.0rc3
#
#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.0rc3"

# For debugging mode, run 'TPDEBG=1 .tracepkg' or set it in environment.
export TPDEBG=${TPDEBG:-0}

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

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

#This must be an existent dir! Missing dependencies will be stored here.
export MISDIR="/var/log/missing"

export INSTDIR=".";
export CONFDIR=".";

[ $TPDEBG = 0 ] && {
#installation directory
export INSTDIR="/usr/libexec/tracepkg";

#configuration directory
export CONFDIR="/etc/tracepkg"
}

#
#	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 instance of tracepkg seems to be running."
	echo "WARNING: If this is incorrect, 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 lock
rm /var/run/tracepkg.pid 2> /dev/null

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

#EOF
