#!/bin/sh
PKG="$1"

[ -f /tmp/.debmissing ] && sudo rm /tmp/.debmissing
[ -f /tmp/.debneeded ] && sudo rm /tmp/.debneeded
[ -f /tmp/.scemissing ] && sudo rm /tmp/.scemissing

if [ ! -d /tmp/tcloop/"$PKG" ]; then
	echo ""$PKG" is not loaded.  Use 'loadsce "$PKG"' first."
	exit 1
fi

if [ -f /tmp/tcloop/"$PKG"/usr/local/sce/"$PKG"/"$PKG".debdeps ]; then
	for I in `cat /tmp/tcloop/"$PKG"/usr/local/sce/"$PKG"/"$PKG".debdeps`; do grep "^$I:" /tmp/.debinstalled > /dev/null 2>&1 || echo "$I" >> /tmp/.debmissing ; done
else
	echo "No dependency information is available for "$PKG".  Exiting.."
	exit 1
fi
if ls /tmp/tcloop/"$PKG"/usr/local/sce/"$PKG"/*.debs > /dev/null 2>&1; then
   for I in `ls /tmp/tcloop/"$PKG"/usr/local/sce/"$PKG"/*.debs`; do
	SCEDEPPKGNAME=`basename $I .debs`
	if [ -f /tmp/.debmissing ] && [ -s /tmp/.debmissing ]; then
		for E in `cat /tmp/.debmissing`; do 
			if grep "^$E$" "$I" > /dev/null 2>&1; then
			 	echo "$E" >> /tmp/.debneeded
			fi
		done
	fi
	if [ -f /tmp/.debneeded ] && [ -s /tmp/.debneeded ]; then
		echo " "
		echo "The below Debian packages were previously provided by"
		echo "the dependent SCE "$SCEDEPPKGNAME".sce, but are not"
		echo "currently installed in the system.  Re-import '"$PKG"' "
		echo "to add these packages to '"$PKG"' and fix the issue."
		echo " "
		echo "`cat /tmp/.debneeded`"
		echo " "
	else
		echo " "
		echo "No Debian packages provided by the dependency"
		echo "SCE "$SCEDEPPKGNAME".sce appear to be missing."
		echo " "
	fi
	[ -f /tmp/.debneeded ] && sudo rm /tmp/.debneeded
   done	
fi 

if [ -f /tmp/tcloop/"$PKG"/usr/local/sce/"$PKG"/"$PKG".md5sum ]; then
	for I in `cat /tmp/tcloop/"$PKG"/usr/local/sce/"$PKG"/"$PKG".md5sum | cut -f1 -d:`; do 
		grep "^$I$" /tmp/.debmissing > /dev/null 2>&1 && echo "$I" >> /tmp/.scemissing
	done
else 
	echo "The list of Debian packages that "$PKG" is made from is missing.  Exiting.."
fi

if [ -f /tmp/.scemissing ] && [ -s /tmp/.scemissing ]; then
	echo "The below packages were imported as part of '"$PKG"',"
	echo "but for some reason are missing.  Re-import '"$PKG"'"
	echo "to add these packages to '"$PKG"' and fix the issue."
	echo " "
	echo "`cat /tmp/.scemissing`"
	echo " "
fi

[ -f /tmp/.debmissing ] && sudo rm /tmp/.debmissing
[ -f /tmp/.debneeded ] && sudo rm /tmp/.debneeded
[ -f /tmp/.scemissing ] && sudo rm /tmp/.scemissing
