#!/bin/bash2
# PhionVersionString 3.2.0.1

set -f

LILOCONF=/etc/lilo.conf


function liloconfparse
	{
	local TMPLINE
	local TTMPLINE
	local DEFIMG=
	local EXPECTLABEL=0
	local DEFEXIST=0


	k=0	

	while read line; do
		TMPLINE=$(echo $line)
		TTMPLINE=${TMPLINE%'='*}
		TMPLINE=${TMPLINE#"$TTMPLINE"'='}
		
		if [ "_$TTMPLINE" = "_default" ]; then
			DEFAULT=$TMPLINE	
			if [ "_$DEFAULT" = "_" ]; then 
				DEFEXIST=0
			else
				DEFEXIST=1
				DEFIMG=$TMPLINE
			fi
		fi


		if [ "_$TTMPLINE" = "_image" -a $EXPECTLABEL = 0 ]; then
                        #IMAGE[$k]=$TMPLINE
			CURRIMAGE=$TMPLINE
			#let k=k+1
			EXPECTLABEL=1
		elif [ "_$TTMPLINE" = "_image" -a $EXPECTLABEL = 1 ]; then
			echo "ERROR: missing label key"
			exit 1
		elif [ "_$TTMPLINE" = "_label" -a $EXPECTLABEL = 1 ]; then
			IMAGE[$k]="$CURRIMAGE/$TMPLINE"
			EXPECTLABEL=0
			let k=k+1
		elif [ "_$TTMPLINE" = "_label" -a $EXPECTLABEL = 0 ]; then
			echo "ERROR: redundant label key"
			exit 1
                fi                                   


		
	done

	
	if [ $DEFEXIST = 1 ]; then
		
		for i in ${IMAGE[@]}; do
			TMPIMG=`basename $i`
			if [ `basename $i`  = $DEFIMG ]; then
				#echo label $DEFIMG 	
				#echo "image $(dirname $i)"
				echo $(dirname $i)
				break
			fi
		done
	else
		#echo label $(basename ${IMAGE[0]})
                #echo image $(dirname ${IMAGE[0]}) 
                echo $(dirname ${IMAGE[0]}) 
		
	fi
	}

if [ $# -gt 0 ]; then
	echo "lilo wrapper not option compatible: use /sbin/lilo.orig instead"
	exit 1
fi

IMG=$(liloconfparse < $LILOCONF)

if [ $? != 0 ]; then
	echo "$IMG"
	echo "check /etc/lilo.conf" 
	exit 1
else
	if [ -e $IMG ]; then
		RES=$(strings $IMG |grep -e 2.[24].*-[0-9,0-9])
		set -- $RES
		
		[ ! -d /lib/modules/"$1" ] && (echo "modules directory for $1 not found"; exit 1)
		[ ! -d /lib/modules/"$1"/net ] && (echo "network modules directory for $1 not found"; exit 1)
		[ ! -d /lib/modules/"$1"/scsi ] && (echo "scsi modules directory for $1 not found"; exit 1)

		echo "Implementing default boot image $IMG for kernel version $1"
		echo "Modules directory found, if unsure check contents before rebooting"

		/sbin/lilo -v	
			
		
	else
		echo $IMG not found
		exit 1
	fi
fi



exit 0
