#!/bb/ash
# (c) Jason Williams 2015

. /etc/init.d/tc-functions
HERE=`pwd`
BUILD=`getBuild`
checknotroot
TCEDIR=/etc/sysconfig/tcedir
DEBINXDIR=""$TCEDIR"/import/debinx"

exit_tcnet() {
	echo "There is an issue connecting to `cat /opt/tcemirror`, exiting.."
	exit 1
}

if [ "$1" == "-h" ] || [ "$1" == "--help" ]; then
        echo " "
	echo "${YELLOW}sce-pkgcheck - Check individual PKG (package) to confirm name and repository of"
	echo "               origin, including PKGs not currently installed. Can also check"
	echo "               PKGs from SCE list file(s) located in /etc/sysconfig/tcedir/sce/"
	echo "               (eg. typo in list file).${NORMAL} Results echoed to /tmp/.pkgcheck."
	echo "               May use option combinations (eg. sce-pkgcheck -paq)."
	echo "               Run 'sce-import --help' regarding list files."
	echo " "
	echo "Usage:"
	echo " "
	echo "${YELLOW}"sce-pkgcheck PKG"${NORMAL}         Update DEBINX, confirm the PKG and repository."
	echo "${YELLOW}"sce-pkgcheck -p PKG"${NORMAL}      Preserve DEBINX, confirm the PKG and repository."   
	echo "${YELLOW}"sce-pkgcheck -q PKG"${NORMAL}      Quiet mode, update DEBINX, terminal output only if PKG"
	echo "                         not available, full output echoed to /tmp/.pkgcheck."
	echo "${YELLOW}"sce-pkgcheck -l SCE.lst"${NORMAL}  Check PKGs in specific SCE directory list file."
	echo "${YELLOW}"sce-pkgcheck -a"${NORMAL}          Check PKGs in all SCE directory list files."
	echo " "
	exit 0
fi

while getopts pqla OPTION
do
	case ${OPTION} in
		a) CHECKALL=TRUE ;;
		l) LIST=TRUE ;;
		p) PRESERVEDEBINXMODE=TRUE ;;
		q) QUIET=TRUE
		   echo " "
		   echo "Using the -q option."
		   ;;
	esac
done
shift `expr $OPTIND - 1`
PKG="$1"

if [ ! -n "$PKG" ] && [ "$CHECKALL" != "TRUE" ]; then
	echo "Run sce-pkgcheck --help for usage information."
	exit 1
fi
 
[ -f /tmp/.pkgcheck ] && sudo rm /tmp/.pkgcheck

echo " "

read IMPORTMIRROR < /opt/tcemirror
PREBUILTMIRROR="${IMPORTMIRROR%/}/dCore/"$BUILD"/import"
IMPORTMIRROR="${IMPORTMIRROR%/}/dCore/import"

cd /tmp

if [ "$PRESERVEDEBINXMODE" == "TRUE" ]; then
	echo "Using the -p option."
	echo " "
	cd "$DEBINXDIR"
	if [ ! -f deb2sce.tar.gz ]; then
		wget -O deb2sce.tar.gz -cq "$PREBUILTMIRROR"/deb2sce.tar.gz 2>/dev/null ||  exit_tcnet
		tar xvf deb2sce.tar.gz PKGADDDEP >/dev/null 2>&1
		tar xvf deb2sce.tar.gz PKGPREBUILTDEP >/dev/null 2>&1
	fi
	[ -s debinx ] || sudo debGetEnv "$2"
	if [ "$?" != "0" ]; then
		echo "Error updating DEBINX files, exiting.."
	fi
else
	if [ -f deb2sce.tar.gz ]; then
		rm deb2sce.tar.gz
	fi
	
	wget -O deb2sce.tar.gz -cq "$PREBUILTMIRROR"/deb2sce.tar.gz 2>/dev/null ||  exit_tcnet
	tar xvf deb2sce.tar.gz PKGADDDEP >/dev/null 2>&1
	tar xvf deb2sce.tar.gz PKGPREBUILTDEP >/dev/null 2>&1
	
	sudo debGetEnv "$2"
	if [ "$?" != "0" ]; then
		echo "Error updating DEBINX files, exiting.."
	fi
fi

read DEBINX < "$DEBINXDIR"/debinx
DEBINX="/etc/sysconfig/tcedir/import/debinx/$DEBINX"
echo $DEBINX
read DEBINX_SECURITY < "$DEBINXDIR"/debinx_security
DEBINX_SECURITY="/etc/sysconfig/tcedir/import/debinx/$DEBINX_SECURITY"
echo $DEBINX_SECURITY
PKGPREBUILTDEP="$DEBINXDIR"/PKGPREBUILTDEP
PKGADDDEP="$DEBINXDIR"/PKGADDDEP
echo " "
pkgcheck() {
if [ "$QUIET" == "TRUE" ]; then
	if grep -i "^$1:" "$PKGPREBUILTDEP" > /dev/null 2>&1; then
		: #echo ""$1" is a dCore prebuilt package."
		echo ""$1" is a dCore prebuilt package." >> /tmp/.pkgcheck
	elif grep -i "^Package: $1$" "$TCEDIR"/debinx.* > /dev/null 2>&1 ; then
		: #echo ""$1" is an extra repo package."
		echo ""$1" is an extra repo package." >> /tmp/.pkgcheck
	elif sudo grep -i "^Package: $1$" "$DEBINX_SECURITY" > /dev/null 2>&1 ; then
		: #echo ""$1" is a security updates package."
		echo ""$1" is a security updates package." >> /tmp/.pkgcheck
	elif sudo grep -i "^Package: $1$" "$DEBINX" > /dev/null 2>&1 ; then
		: #echo ""$1" is a main repo package."
		echo ""$1" is a main repo package." >> /tmp/.pkgcheck
	elif grep -i "^$1:" "$PKGADDDEP" > /dev/null 2>&1; then
		: #echo ""$1" may be a metapackage (is in our server's dependency list)."
		echo ""$1" may be a metapackage (is in our server's dependency list)." >> /tmp/.pkgcheck
	else
		echo "${YELLOW}"$1" is not an available package!${NORMAL}"
		echo ""$1" is not an available package!" >> /tmp/.pkgcheck
		if [ "$LIST" != "TRUE" ] && [ "$CHECKALL" != "TRUE" ]; then
			exit 1
		fi
	fi
else
	if grep -i "^$1:" "$PKGPREBUILTDEP" > /dev/null 2>&1; then
		echo "${YELLOW}"$1"${NORMAL} is a dCore prebuilt package."
		echo ""$1" is a dCore prebuilt package." >> /tmp/.pkgcheck
	elif grep -i "^Package: $1$" "$TCEDIR"/debinx.* > /dev/null 2>&1 ; then
		echo "${YELLOW}"$1"${NORMAL} is an extra repo package."
		echo ""$1" is an extra repo package." >> /tmp/.pkgcheck
	elif sudo grep -i "^Package: $1$" "$DEBINX_SECURITY" > /dev/null 2>&1 ; then
		echo "${YELLOW}"$1"${NORMAL} is a security updates package."
		echo ""$1" is a security updates package." >> /tmp/.pkgcheck
	elif sudo grep -i "^Package: $1$" "$DEBINX" > /dev/null 2>&1 ; then
		echo "${YELLOW}"$1"${NORMAL} is a main repo package."
		echo ""$1" is a main repo package." >> /tmp/.pkgcheck
	elif grep -i "^$1:" "$PKGADDDEP" > /dev/null 2>&1; then
		echo "${YELLOW}"$1"${NORMAL} may be a metapackage (is in our server's dependency list)."
		echo ""$1" may be a metapackage (is in our server's dependency list)." >> /tmp/.pkgcheck
	else
		echo "${YELLOW}"$1" is not an available package!${NORMAL}"
		echo ""$1" is not an available package!" >> /tmp/.pkgcheck
		if [ "$LIST" != "TRUE" ] && [ "$CHECKALL" != "TRUE" ]; then
			exit 1
		fi
	fi

fi
}

if [ "$CHECKALL" == "TRUE" ]; then
	echo "Using the -a option."
	if [ ! -f "$TCEDIR"/sce/*.lst > /dev/null 2>&1 ]; then
		echo " "
		echo "No list files present in /etc/sysconfic/tcedir/sce/, exiting.."
		echo "No list files present in /etc/sysconfic/tcedir/sce/, exiting.." >> /tmp/.pkgcheck
		exit 1
	fi
	for I in `ls /etc/sysconfig/tcedir/sce/*.lst`; do
		I=`echo "$I" | awk '{print $1 $2}'`
		if [ "$QUIET" != "TRUE" ]; then
			echo " "
			echo "${YELLOW}*${NORMAL} Checking "$I":"
		fi
		echo "* Checking "$I":" >> /tmp/.pkgcheck
		for E in `cat "$I"`; do
			E=`echo "$E" | awk '{print $1 $2}'`
			[ -n "$E" ] && pkgcheck "$E"
		done
		echo " " >> /tmp/.pkgcheck
	done
	exit 1
fi

if [ "$LIST" == "TRUE" ]; then
	echo "Using the -l option."
	echo " "
	cd "$TCEDIR"/sce
	LST="$1"
	if [ ! -f "$TCEDIR"/sce/"$LST" ]; then
		echo "The list file '"$LST"' does not exist, exiting.."
		echo "The list file '"$LST"' does not exist, exiting.." >> /tmp/.pkgcheck
		exit 1
	fi

	echo "${YELLOW}*${NORMAL} Checking "$TCEDIR"/sce/"$LST":"
	echo "* Checking "$TCEDIR"/sce/"$LST":" >> /tmp/.pkgcheck
	for I in `cat "$LST"`; do
		I=`echo "$I" | awk '{print $1 $2}'`
		[ -n "$I" ] && pkgcheck "$I"
	done
else
	pkgcheck "$PKG"
fi

