. /etc/rc.d/init.d/functions

usage (){
echo "usage:	fb_lilo <kernel-image> <resolution|remove> <color-depth>"
echo "	(ex. fb_lilo linux 1024x768 16)"
}
color (){
echo ""
echo "	The currently supported color depths are:"
echo "	8 (256 Colors) 15 (32K Colors) 16 (64K Colors)"
echo "	24 (16.7 Million Colors)"
}

echo -n "Searching for image labels."
images=`cat -n /etc/lilo.conf | grep -v \# | grep image | awk ' { print $1; } '`
COUNT=2
labels=""
for i in $images
do
	echo -n "."
	NEXT=`echo $images | awk ' { print $'$COUNT'; } '`
	COUNT=`expr $COUNT + 1`
	if [ "$NEXT" = "" ] ; then
		STOP=",NR==2048"
	else
		NEXT=`expr $NEXT - 1`
		STOP=",NR=="$NEXT
	fi
	newlabel=`cat -n /etc/lilo.conf | grep -v \# | awk ' NR=='$i''$STOP' { print $0; } ' | awk -F\= ' BEGIN { done=0; } done==0 && /label=/ { print $2; done=1; } '`
	label=`echo $label $newlabel`
done

if [ -n "$label" ]; then
	echo " done"
fi

if [ $1 ]; then
	if [ -n "`(echo $label | grep $1)`" ]; then
		labelreal="yes"
	else
		labelreal="no"
	fi

	if [ $labelreal = "yes" ]; then
		if [ $2 ]; then
		OK="yes"
		case $2 in
			640x480)
				if [ $3 ]; then
					case $3 in
						8) VGALINE="vga=769"
						;;
						15) VGALINE="vga=784"
						;;
						16) VGALINE="vga=785"
						;;
						24) VGALINE="vga=786"
						;;
						*)	echo "" 
							echo "	\""$3"\" does not appear to be a valid color depth"
							color
							OK="no"
						;;
					esac
				else 
					usage
					OK="no"
				fi
					;;

			800x600)
				if [ $3 ]; then
					case $3 in
						8) VGALINE="vga=771"
						;;
						15) VGALINE="vga=787"
						;;
						16) VGALINE="vga=788"
						;;
						24) VGALINE="vga=789"
						;;
						*)	echo "" 
							echo "	\""$3"\" does not appear to be a valid color depth"
							color
							OK="no"
						;;
					esac
				else 
					usage
					OK="no"
				fi
					;;

			1024x768)	
				if [ $3 ]; then
					case $3 in
						8) VGALINE="vga=773"
						;;
						15) VGALINE="vga=790"
						;;
						16) VGALINE="vga=791"
						;;
						24) VGALINE="vga=792"
						;;
						*)	echo "" 
							echo "	\""$3"\" does not appear to be a valid color depth"
							color
							OK="no"
						;;
					esac
				else 
					usage
					OK="no"
				fi
					;;

			1280x1024)	
				if [ $3 ]; then
					case $3 in
						8) VGALINE="vga=775"
						;;
						15) VGALINE="vga=793"
						;;
						16) VGALINE="vga=794"
						;;
						24) VGALINE="vga=795"
						;;
						*)	echo "" 
							echo "	\""$3"\" does not appear to be a valid color depth"
							color
							OK="no"
						;;
					esac
				else 
					usage
					OK="no"
				fi
					;;
			1600x1200)     
                                if [ $3 ]; then
                                        case $3 in
                                                8) VGALINE="vga=796"
                                                ;;
                                                15) VGALINE="vga=797"
                                                ;;
                                                16) VGALINE="vga=798"
                                                ;;
                                                24) VGALINE="vga=799"
                                                ;;
                                                *)      echo "" 
                                                        echo "  \""$3"\" does not appear to be a valid color depth"
                                                        color
                                                        OK="no"
                                                ;;
                                        esac
                                else
					usage
                                	OK="no"
                                fi
                                        ;;

			remove)		VGALINE=""
					;;

			*)		echo ""
							echo "	\""$2"\" does not appear to be a valid resolution."
							echo "	Currently supported resolutions are:"
echo "	<640x480> <800x600> <1024x768> <1280x1024> <1600x1200>"
							OK="no"
					;;	

		esac
		else
			usage
			OK="no"
		fi
		if [ $OK = "no" ]; then
			echo -n ""
		else
			cp /etc/lilo.conf /etc/lilo.conf.old
			echo -n "Updating lilo.conf entries."


			COUNT=2
			for i in $images
			do
			        echo -n "."
			        NEXT=`echo $images | awk ' { print $'$COUNT'; } '`
			        COUNT=`expr $COUNT + 1`
			        if [ "$NEXT" = "" ] ; then
			                STOP=",NR==2048"
		        	else
		                	NEXT=`expr $NEXT - 1`
		                	STOP=",NR=="$NEXT
		        	fi
				testlabel=`cat -n /etc/lilo.conf | grep -v \# | awk ' NR=='$i''$STOP' { print $0; } ' | awk -F\= ' /label=/ { print $2; } '`
				if [ $testlabel = $1 ]; then
			        	VGAEXISTS=`cat -n /etc/lilo.conf | grep -v \# | awk ' NR=='$i''$STOP' { print $0; } ' | grep vga | awk ' { print $1; } '`
					if [ -n "`(echo $VGAEXISTS)`" ]; then
						vga=yes
						line=$VGAEXISTS
					else
						vga=no
						line=`cat -n /etc/lilo.conf | awk -F\= ' /label='$1'$/ { print $1; } ' | awk ' {print $1;} '`
					fi
				fi
			done
			echo " done"
			echo -n "Updating boot record.."
			if [ $vga = "yes" ]; then
				cat /etc/lilo.conf | awk ' NR=='$line' {print "	'$VGALINE'"; next; } { print $0; } ' > /tmp/lilo.fb.tmp
			else
				cat /etc/lilo.conf | awk ' NR=='$line' {print $0 "\n	'$VGALINE'"; next; } { print $0; } ' > /tmp/lilo.fb.tmp
			fi
		mv /tmp/lilo.fb.tmp /etc/lilo.conf
		action ". done" /sbin/lilo 	
		if [ $? = 0 ]; then
			echo ""
			echo "	You will need to restart to enter the framebuffer console."
			echo ""
		fi
		fi

	else
		echo ""
		echo "	"\"$1\"" does not appear to be a valid Linux label."
		echo "	Please check /etc/lilo.conf for this label or select"
		echo "	a different label.  The valid labels found are:"
		echo ""
		echo "	"$label
	fi
else
	usage
fi
