#!/bin/sh
# Startup script, used to set framebuffer device mode (video mode).
#
# bugs:
# 1) Should run before keytable setting to avoid font reset.
# Made by Light Olli: olli@digger.org.ru. License is current GNU GPL.

fbutil=`which fbset`
best=800x600-100
#best=640x480-100
safe=640x480-60
dbfile=/etc/fb.modes
# after screen resising gpm & keytable may misbehave. Here're utils & 
# thier options that whould run to fix this potential situation.
gpmconf=/etc/rc.d/init.d/gpm
gpmconfopt=restart
keyconf=/etc/rc.d/init.d/keytable
keyconfopt=restart
sleept=2
# Source function library.
. /etc/rc.d/init.d/functions

RETVAL=0

if [ ! -x $fbutil ]; then
 echo "Warning: Can't execute $fbutil , thus CAN'T SWITCH FRAMEBUFFER MODES!"
 exit 0
fi

case "$1" in
    start|set)
		if [ "$LANG" = "ru_RU.KOI8-R" ]; then
		echo -e "\nО конфигурации $0 смотрите $dbfile и '$fbutil --help' ."
		echo "Переключаем framebuffer устройство (видео-карту) в режим $best .."
		# needed to be able to read previously echoed notes.
		sleep $sleept
		else
		echo -e "\nSee $dbfile & '$fbutil --help' for details on configuring $0 ."
		echo "Switching framebuffer device (video card) to mode $best .."
		fi
		# need to have initialised consoles at the start time.
		for file in /dev/tty? /dev/tty1?; do 
		open $file; echo -ne "\033(K" > $file; echo -e " \n" > $file
		done
		
		case "$2" in
		    640x480-60)
			$fbutil -db $dbfile -a -v 640x480-60
			RETVAL=$?
			sleep $sleept
			$gpmconf $gpmconfopt
			$keyconf $keyconfopt
			RETVAL=$?
			;;
		    640x480-72)
			$fbutil -db $dbfile -a -v 640x480-72
			RETVAL=$?
			sleep $sleept
			$gpmconf $gpmconfopt
			$keyconf $keyconfopt
			;;
		    640x480-75)
			$fbutil -db $dbfile -a -v 640x480-75
			RETVAL=$?
			sleep $sleept
			$gpmconf $gpmconfopt
			$keyconf $keyconfopt
			;;
		    640x480-90)
			$fbutil -db $dbfile -a -v 640x480-90
			RETVAL=$?
			sleep $sleept
			$gpmconf $gpmconfopt
			$keyconf $keyconfopt
			;;
		    640x480-100)
			$fbutil -db $dbfile -a -v 640x480-100
			RETVAL=$?
			sleep $sleept
			$gpmconf $gpmconfopt
			$keyconf $keyconfopt
			;;
		    800x600-56)
			$fbutil -db $dbfile -a -v 800x600-56
			RETVAL=$?
			sleep $sleept
			$gpmconf $gpmconfopt
			$keyconf $keyconfopt
			;;
		    800x600-60)
			$fbutil -db $dbfile -a -v 800x600-60
			RETVAL=$?
			sleep $sleept
			$gpmconf $gpmconfopt
			$keyconf $keyconfopt
			;;
		    800x600-70)
			$fbutil -db $dbfile -a -v 800x600-70
			RETVAL=$?
			sleep $sleept
			$gpmconf $gpmconfopt
			$keyconf $keyconfopt
			;;
		    800x600-72)
			$fbutil -db $dbfile -a -v 800x600-72
			RETVAL=$?
			sleep $sleept
			$gpmconf $gpmconfopt
			$keyconf $keyconfopt
			;;
		    800x600-75)
			$fbutil -db $dbfile -a -v 800x600-75
			RETVAL=$?
			sleep $sleept
			$gpmconf $gpmconfopt
			$keyconf $keyconfopt
			;;
		    800x600-90)
			$fbutil -db $dbfile -a -v 800x600-90
			RETVAL=$?
			sleep $sleept
			$gpmconf $gpmconfopt
			$keyconf $keyconfopt
			;;
		    800x600-100)
			$fbutil -db $dbfile -a -v 800x600-100
			RETVAL=$?
			sleep $sleept
			$gpmconf $gpmconfopt
			$keyconf $keyconfopt
			;;
		    1024x768-60)
			$fbutil -db $dbfile -a -v 1024x768-60
			RETVAL=$?
			sleep $sleept
			$gpmconf $gpmconfopt
			$keyconf $keyconfopt
			;;
		    1024x768-70)
			$fbutil -db $dbfile -a -v 1024x768-70
			RETVAL=$?
			sleep $sleept
			$gpmconf $gpmconfopt
			$keyconf $keyconfopt
			;;
		    1024x768-72)
			$fbutil -db $dbfile -a -v 1024x768-72
			RETVAL=$?
			sleep $sleept
			$gpmconf $gpmconfopt
			$keyconf $keyconfopt
			;;
		    1024x768-75)
			$fbutil -db $dbfile -a -v 1024x768-75
			RETVAL=$?
			sleep $sleept
			$gpmconf $gpmconfopt
			$keyconf $keyconfopt
			;;
		    1024x768-90)
			$fbutil -db $dbfile -a -v 1024x768-90
			RETVAL=$?
			sleep $sleept
			$gpmconf $gpmconfopt
			$keyconf $keyconfopt
			;;
		    1024x768-100)
			$fbutil -db $dbfile -a -v 1024x768-100
			RETVAL=$?
			sleep $sleept
			$gpmconf $gpmconfopt
			$keyconf $keyconfopt
			;;
		    1152x864-60)
			$fbutil -db $dbfile -a -v 1152x864-60
			RETVAL=$?
			sleep $sleept
			$gpmconf $gpmconfopt
			$keyconf $keyconfopt
			;;
		    1152x864-70)
			$fbutil -db $dbfile -a -v 1152x864-70
			RETVAL=$?
			sleep $sleept
			$gpmconf $gpmconfopt
			$keyconf $keyconfopt
			;;
		    1152x864-75)
			$fbutil -db $dbfile -a -v 1152x864-75
			RETVAL=$?
			sleep $sleept
			$gpmconf $gpmconfopt
			$keyconf $keyconfopt
			;;
		    1152x864-80)
			$fbutil -db $dbfile -a -v 1152x864-80
			RETVAL=$?
			sleep $sleept
			$gpmconf $gpmconfopt
			$keyconf $keyconfopt
			;;
		    1280x1024-60)
			$fbutil -db $dbfile -a -v 1280x1024-60
			RETVAL=$?
			sleep $sleept
			$gpmconf $gpmconfopt
			$keyconf $keyconfopt
			;;
		    1280x1024-70)
			$fbutil -db $dbfile -a -v 1280x1024-70
			RETVAL=$?
			sleep $sleept
			$gpmconf $gpmconfopt
			$keyconf $keyconfopt
			;;
		    1280x1024-74)
			$fbutil -db $dbfile -a -v 1280x1024-74
			RETVAL=$?
			sleep $sleept
			$gpmconf $gpmconfopt
			$keyconf $keyconfopt
			;;
		    1280x1024-75)
			$fbutil -db $dbfile -a -v 1280x1024-75
			RETVAL=$?
			sleep $sleept
			$gpmconf $gpmconfopt
			$keyconf $keyconfopt
			;;
		    1600x1200-60)
			$fbutil -db $dbfile -a -v 1600x1200-60
			RETVAL=$?
			sleep $sleept
			$gpmconf $gpmconfopt
			$keyconf $keyconfopt
			;;
		    1600x1200-66)
			$fbutil -db $dbfile -a -v 1600x1200-66
			RETVAL=$?
			sleep $sleept
			$gpmconf $gpmconfopt
			$keyconf $keyconfopt
			;;
		    1600x1200-76)
			$fbutil -db $dbfile -a -v 1600x1200-76
			RETVAL=$?
			sleep $sleept
			$gpmconf $gpmconfopt
			$keyconf $keyconfopt
			;;
		    *)
			$fbutil -db $dbfile -a -v $best
			RETVAL=$?
			sleep $sleept
			$gpmconf $gpmconfopt
			$keyconf $keyconfopt
    		        ;;
		esac
		# needed to be able to read previously echoed notes.
		sleep $sleept
		;;
    stop|reset|safe)
		if [ "$LANG" = "ru_RU.KOI8-R" ]; then
		echo -e "\nПереключаем framebuffer устройство (видео-карту) в наиболее безопасный режим $best .."
		else
		echo -e "\nSwitching framebuffer device (video card) to safest mode 640x480,60Hz.."
		fi
		$fbutil -db $dbfile -a -v $safe
		sleep $sleept
		RETVAL=$?
		;;
    restart)
		$0 stop
		$0 start
		RETVAL=$?
		;;
    *)
		if [ "$LANG" = "ru_RU.KOI8-R" ]; then
		echo "Использование: $0 {set|reset|restart}"
		else
		echo "Usage: $0 {set|safe|restart}"
		fi
		exit 0
                ;;
esac

if [ "$RETVAL" = "0" ]; then
 success "$0 $1"
else
 failure "$0 $1"
fi;
echo
exit $RETVAL
