#!/bin/sh
#
# RETURN CODE:
#
#		0 Some Patch are install
#		1 No Patch to install
#		2 Cannot build the Kernel
#		3 Problem in the patch installation
# 		4 MiniRoot Patches to be install by the user and some Some Patch are install
# 		5 MiniRoot Patches to be install by the user and No Patch are install


kernel_check () {
	echo -n "Checking if kernel links ($1)..." 
	if { /etc/autoconfig -v 1>> /tmp/$$.kernelerror  2>&1; } then
		echo "Done"
	else
		echo ""
		echo "********************************************************"
		echo "YOUR IRIX KERNEL DOES NOT BUILD CORRECTLY"
		echo "THIS PROBLEM MUST BE FIXED BEFORE CONTINUING...."
		if [ -n ${MANUALPATCHES:-""} ]; then
			echo "You MAY need to install manually the following "
			echo "PATCHES: ${MANUALPATCHES}"
			echo "from the ${MANUALDIST} directory"
			echo "Please read carefully the release notes to"
			echo "know how to install theses and verify if theses patches apply to you"
		fi	
		echo "HERE ARE THE ERROR MESSAGES";
		cat /tmp/$$.kernelerror;
		echo "********************************************************"
		exit 2;
	fi
	return 0;
}

check_for_patch () {
	DISTPATH=$1
	echo "Checking for Patches to install in $1"
	echo "install patch installable" > /tmp/f$$
	echo "keep patch downgrade"     >> /tmp/f$$
	echo "keep same"		>> /tmp/f$$
	echo "list install"		>> /tmp/f$$
	echo "conflicts"		>> /tmp/f$$
	echo "quit"			>> /tmp/f$$

	PATCHES="";
	NOCONFLICT="";
	PATCHES_INDIR="";

	for DIRECTORY in ${DISTPATH};
	do
	    inst -Vinteractive:false -Vverbosity:0 -n -f  ${DIRECTORY} -Vinst_terse_mode:true -Vsmock_sproc:true < /tmp/f$$ 1> /tmp/lstpatch.$$ 2> /tmp/$$.error

	    LOCALPATCHES="`grep '^i' /tmp/lstpatch.$$ | cut -c 16-19| sort|uniq`"
	    LOCALPATCHES=${LOCALPATCHES}"`grep '^Inst> i' /tmp/lstpatch.$$ | cut -c 22-25| sort|uniq`"

	    NOCONFLICT=${NOCONFLICT}"`grep -i 'No conflicts' /tmp/lstpatch.$$`"

	    if [ -n ${LOCALPATCHES:-""} ]; then
		PATCHES=${PATCHES}${LOCALPATCHES};
		PATCHES_INDIR="${PATCHES_INDIR} ${DIRECTORY}"
 	    fi
	done
	return 0;
}

check_for_conflict () {
	if [ -z ${NOCONFLICT:-""} ]; then
		echo "Problem with selected patch"
		cat /tmp/lstpatch.$$
		cat /tmp/$$.error
		exit 3
	fi
}

install_patches () {
	DISTPATH=$1;
	echo "**Installing patches from $1: $PATCHES"
	echo "install patch installable" > /tmp/f$$
	echo "keep patch downgrade"     >> /tmp/f$$
	echo "keep same"		>> /tmp/f$$
	echo "go"			>> /tmp/f$$
	echo "quit"			>> /tmp/f$$


	for DIRECTORY in ${DISTPATH};
	do
	    inst -Vinteractive:false -Vverbosity:2 -f ${DIRECTORY} -Vsmock_sproc:true -Vmenus:off < /tmp/f$$  2>/tmp/$$.error |tee -a /tmp/$$.error
	done
}

check_for_special () {
	MANUALDIST=${DISTRIBUTION}/$1
	if [ -d ${MANUALDIST} ]; then
		check_for_patch ${MANUALDIST}
		MANUALPATCHES=${PATCHES}
	fi
}

#
# Begin of the "Main"
#

#
# Prepare for distributions directory
#
DISTRIBUTION=${1:-.};
if [ $DISTRIBUTION = "KERNEL_CHECK_ONLY" ]; then
	kernel_check "Before installing anything"
	exit 0;
fi

DISTRIBUTION=${DISTRIBUTION}/Patches

#
# Check for NFS Package
#
REGDIST=${DISTRIBUTION}/normal;
NFS=`showprods -D 1 -s nfs`
if [ ${NFS:-""} = "" ]; then
	:
elif [ -d ${DISTRIBUTION}/nfs ]; then
        REGDIST="${REGDIST} ${DISTRIBUTION}/nfs"
fi

# Check if we have "Special care Patch to install"
check_for_special miniroot

# Check if we have Patch to install
check_for_patch "${REGDIST}"

# Check fo conflict
check_for_conflict

#
# Check if we have patches to instalL
#
if [ -z ${PATCHES:-""} ]; then
	RETURN_STATUS=1;
else 
	install_patches "${PATCHES_INDIR}"
	check_for_special miniroot
	kernel_check "After Installing Patches"
	RETURN_STATUS=0;
fi

#
# Check if we have some MANUALPATCH to install
#

if [ -n ${MANUALPATCHES:-""} ]; then
   echo "********************************************************"
	echo "The major part of the SGI PATCHES are installed"
	echo "However the following patches maybe also recommended:"
	echo "${MANUALPATCHES}, You will find theses in the"
	echo "${MANUALDIST} directory"
	echo "Please read the release notes carefully to"
	echo "know how to install these and verify if they patches apply to you"
	echo "The install program will continue Now..."
	echo "********************************************************"
	RETURN_STATUS=`expr $RETURN_STATUS + 4`
fi

exit ${RETURN_STATUS}
