#!/bin/sh
#
#removecore: version 1.0
#
#the main remove routine of tracepkg
#this file is really similar to retrivedb. the differences are that here we also remove pkg's..
#
#--------------------------------------------------------------


#--------------------------------------------------------------
#aux. functions - useful stuff for the tracing of dependencies


. $INSTDIR/core-functions


function removeRoutine ()
#ask for remotion of shared/unshared dependencies
#$1 is a string containing the question to prompt to user
#the list of dependencies is implicit, obtained by the global value 'SHAREDDEP'
#if present $2 is the automatic answer.
{
[ ${#@} -eq 2 ] && YES_NO="$2" || ask_yesnoall "$1"

if [ "$YES_NO" = "all" ]; then
	for ((j_=0; j_<k; j_++)); do
		DEPFILE=`basename ${SHAREDDEP[$j_]} .tgz`
		removepkg ${SHAREDDEP[$j_]} && removedep $DEPFILE
	done

elif [ "$YES_NO" = "yes" ]; then
	for ((j_=0; j_<k; j_++)); do
		ask_yesno "would you like to remove ${SHAREDDEP[$j_]} [yes/no]? "

		if [ "$YES_NO" = "yes" ]; then
			DEPFILE=`basename ${SHAREDDEP[$j_]} .tgz`
			removepkg ${SHAREDDEP[$j_]} && removedep $DEPFILE
		fi
	done
fi
}
#--------------------------------------------------------------


#--------------------------------------------------------------
#main body: we use a function for this, becouse we need to return a number
#return codes:
#	0-we have performed a standard remotion
#	1-we haven't removed anything
#	2-we have removed a file without dependency list


function removecore ()
#$1 is the file we want to remove
#all other arguments are the list of all dependency list files
{
echo "gathering information. Please, wait..."

DEPFILELIST=(${@:2})

DEPMIX=(`retriveRoutine $1 ${DEPFILELIST[@]}`)

DEPTREE=(`echo ${DEPMIX[@]} | cut -f1 -d %`)
DEPFILELIST=(`echo ${DEPMIX[@]} | cut -f2 -d %`)

if [ ${#DEPTREE[@]} -eq 0 ]; then
	echo "$(basename $1 .tgz) hasn't any dependency list into $LOGDIR"

	[ $AUTO -eq 0 ] && {
		ask_yesno "would you like to remove it [yes/no]? "
	} || {
		YES_NO="yes"
	}

	if [ "$YES_NO" = "yes" ]; then
		MISFILE="$MISDIR/$DEPFILE"
		removepkg $1 && [ -f $MISFILE ] && rm $MISFILE
		return 2;
	fi
return 1;
fi

DEPTREE=(`echo ${DEPTREE[@]} | sed -e 's/ /\n/g' | sort | uniq`)

#mark dependencies as shared (with other packages) or unshared
#CWD=`pwd`
#cd /$LOGDIR

unset SHARED
unset SHAREDDEP

for ((i=0; i<${#DEPTREE[@]}; i++)); do
	echo ${DEPFILELIST[@]} | xargs grep -l ${DEPTREE[$i]} > /dev/null
	if [ ! $? -eq 0 ]; then
		#not shared
		SHARED[$i]=0;
	else
		#shared
		SHARED[$i]=1;
	fi
done

#cd $CWD

#have a look if some unshared dependencies belong to some shared dependencies
loopShared ${DEPTREE[@]}

#print results...
echo -e "PACKAGE NAME:\n$(basename $1 .tgz)\n"

[ $AUTO -eq 0 ] && {
	ask_yesno "would you like to remove it [yes/no]? "
} || {
	YES_NO="yes"
}

if [ "$YES_NO" = "yes" ]; then
	DEPFILE=`basename $1 .tgz`
	removepkg $1 && removedep $DEPFILE
else
	return 1;
fi

echo -e "\nNOT SHARED DEPENDENCIES:"

k=0;
for ((j=0; j<i; j++)); do
	[ ${SHARED[$j]} -eq 0 ] && {
		SHAREDDEP[$k]=${DEPTREE[$j]}
		echo ${SHAREDDEP[$k]}
		k=$(($k+1))
	}
done

if [ ! $k -eq 0 ]; then
	echo ""
	#ask for remotion, the following function uses SHAREDDEP as list of dependencies
	if [ $AUTO -eq 0 ]; then {
		removeRoutine "would you like to remove not shared dependencies [yes/no/all]? " 
	} else {
		removeRoutine "would you like to remove not shared dependencies [yes/no/all]? " "all"
	} fi
fi

echo -e "\nSHARED DEPENDENCIES:"

unset SHAREDDEP;
k=0;
for ((j=0; j<i; j++)); do
	[ ! ${SHARED[$j]} -eq 0 ] && {
		SHAREDDEP[$k]=${DEPTREE[$j]}
		echo ${SHAREDDEP[$k]}
		k=$(($k+1))
	}
done

if [ ! $k -eq 0 ]; then
	echo ""
	[ $AUTO -eq 0 ] && {
		ask_yesno "would you like to *_REMOVE_* *_SHARED_* dependencies [yes/no]? "
	} || {
		YES_NO="no"
	}

	if [ "$YES_NO" = "yes" ]; then
		removeRoutine "once again: are you sure you want to *_REMOVE_* *_SHARED_* dependencies [yes/no/all]? "
	fi
fi

return 0;
}
#--------------------------------------------------------------

#EOF