#!/bin/bash
#
#retrivedb: version 1.0
#
#a script containing some useful functions used to trace dependencies of a tgz
#and if required to remove tgz dependency tree.
#
#usage:
#
#--------------------------------------------------------------


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

#load core functions
. $INSTDIR/removecore
#--------------------------------------------------------------


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


REVROOTFILE=$1

echo ""

##retrive the list of all dependency files:
#this is the list of all non read files into the database
DEPFILELIST=(`find $LOGDIR -type f`)

[ ${#DEPFILELIST[@]} -eq 0 ] && {
	echo "no dependency list found in $LOGDIR"
	exit 1
}


#retrive files that use $1
USINGLIST=(`checkList $REVROOTFILE`)
[ ${#USINGLIST[@]} -eq 0 ] && {
	echo "  --> there are no files using $1!"
	removecore $1 ${DEPFILELIST[@]}
	exit 0
}

#init values for the while control-flow
OLDROOTLIST=(${USINGLIST[@]})
ROOTLIST=("")
ROOTCANDIDATES=("")

j=1
while [ $j -eq 1 ]; do

	#for each $FILE that uses a tgz we check if, in its turn, it is used by someone else
	for FILE in ${USINGLIST[@]}; do
		FILENAME=$PKGDIR/`basename $FILE`

		NEWCANDIDATES=(`checkList $FILENAME`)

		[ ${#NEWCANDIDATES[@]} -eq 0 ] && {
			#if $FILE is not used by anyone its a root and we store it
			ROOTLIST=(${ROOTLIST[@]} $FILENAME)
		} || {
			#else we store the ${NEWCANDIDATES[@]} that use $FILE, because they are
			#our new 'root candidates'
			for CANDIDATE in ${NEWCANDIDATES[@]}; do
				CANDIDATEBASE=`basename $CANDIDATE`
				if [ ! "$CANDIDATEBASE" = "`basename $REVROOTFILE`" ]; then #paranoia?
					echo ${USINGLIST[@]} | grep -m 1 -w $CANDIDATE > /dev/null
					#if we haven't scanned the file previously we store it
					[ ! $? -eq 0 ] && ROOTCANDIDATES=(${ROOTCANDIDATES[@]} $CANDIDATE)
				fi
			done
		}
	done

	#clean partial results
	ROOTLIST=(`echo ${ROOTLIST[@]} | sed 's/ /\n/g' | sort | uniq`)

	#compare root lists.
	#- if no differences are found we are at the same level of the previous step:
	#  we have reached the top level of the dependency reverse-tree
	#- else we restart the search overwriting old file names with new found files:
	#  we jump to another (higher) level of dependency reverse-tree
#	[ "`echo ${OLDROOTLIST[@]} | sed -e 's/ //g'`" = "`echo ${ROOTLIST[@]} | sed -e 's/ //g'`" ] && j=0 || {
	[ "${ROOTCANDIDATES[0]}" = "" ] && j=0 || {
		ROOTCANDIDATES=(`echo ${ROOTCANDIDATES[@]} | sed 's/ /\n/g' | sort | uniq`)

		#reset values for the next loop
		USINGLIST=(${ROOTCANDIDATES[@]})
		OLDROOTLIST=(${ROOTLIST[@]})
		ROOTCANDIDATES=("");
	}

done

echo "  --> these are roots of $1:"
echo ${ROOTLIST[@]} | sed -e 's/ /\n/g'

echo ""

YES_NO=""
[ $AUTO -eq 0 ] && ask_yesno "remove them [yes,no]? " || YES_NO="yes"
if [ "$YES_NO" = "yes" ]; then
	for REVROOT in ${ROOTLIST[@]}; do
		removecore $REVROOT ${DEPFILELIST[@]}
	done
fi

echo "...done"
#--------------------------------------------------------------

#EOF
