#!/bin/bash
###
#  xbvset script
#  Copyright (C) Thomas "ShALLaX" Pedley (gentoox@shallax.com)
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
###

export ver="7.0"

###
# getDetails - Get the details about an entry in linuxboot.cfg
###
getDetails() {
	endoffile=`cat -n $cfgpath | awk '{ print $1 }' | tail -n1`
	start=`cat -n $cfgpath | grep -m1 "title $title" | awk '{ print $1 }'`
	found="false"
	initrdline=""
	kernelline=""
	appendline=""

	for i in `cat /proc/cmdline`; do
		if [ "$i" != "${i#installtype=}" ]; then
			INSTALLTYPE=`echo $i | sed "s/.*=//"`
		fi
	done

	if [ ! -z `echo $start | awk '{ print $1 }'` ]; then
		found="true"
		pointer=`expr $start + 1`
		line=""


		while [ $pointer -le $endoffile ]; do
			line=`cat -n $cfgpath | grep -m1 "   $pointer" | sed "s/[0-9].//" | sed "s/^[\t ]*//"`
			pointer=`expr $pointer + 1`
			cmd=`echo $line | awk '{ print $1 }'`
	
			if [ ! -z $cmd ]; then
				if [ $cmd == "title" ]; then
					break
				elif [ $cmd == "kernel" ]; then
					kernelline=$line
				elif [ $cmd == "initrd" ]; then
					initrdline=$line
				elif [ $cmd == "append" ]; then
					appendline=$line
				fi
			fi
			if [ ! -z `echo $kernelline | awk '{ print $1 }'` ] && [ ! -z `echo $initrdline | awk '{ print $1 }'` ] && [ ! -z `echo $appendline | awk '{ print $1 }'` ]; then
				break
			fi
		done
	fi

}

###
# updateDetails - Inserts details back into linuxboot.cfg
###
updateDetails() {
	echo "title $title" >> $cfgpath.tmp
	echo $kernelline >> $cfgpath.tmp
	echo $initrdline >> $cfgpath.tmp
	echo $appendline >> $cfgpath.tmp
}


###
# fbSetup -- setup the /etc/conf.d/fbset file properly
###

fbSetup() {
   grep -v "resolution" /etc/conf.d/fbset > /etc/conf.d/fbset.tmp
   mv /etc/conf.d/fbset.tmp /etc/conf.d/fbset
   echo "resolution=\"$fbset\"" >> /etc/conf.d/fbset
   chown root:root /etc/conf.d/fbset
   chmod 744 /etc/conf.d/fbset
   rc-update del fbset default 1> /dev/null 2> /dev/null
   rc-update add fbset default 1> /dev/null 2> /dev/null
   echo -en "     fbset: "
   sleep 1
   echo -e "fbset -a $fbset"
   sleep 1
}


###
# xfce4rc - Make sure that the xfce menu is always central
###

xfce4rc() {
   export patchname="xfce-patch3" && queryDatabase
   if [ $patchinstalled == "true" ]; then
      if [ -f /home/gentoox/.xfce4/xfce4rc ]; then
         sed -r "s/<Position x=\"[0-9]*\" y=\"[0-9]*\" screenwidth=\"[0-9]*\" screenheight=\"[0-9]*\"\/>/<Position x=\"$newxpos\" y=\"$newypos\" screenwidth=\"$newwidth\" screenheight=\"$newheight\"\/>/" /home/gentoox/.xfce4/xfce4rc > /home/gentoox/.xfce4/xfce4rc.2 2> /dev/null
         mv /home/gentoox/.xfce4/xfce4rc.2 /home/gentoox/.xfce4/xfce4rc 2> /dev/null
         chown gentoox:wheel /home/gentoox/.xfce4/xfce4rc 2> /dev/null
         chmod 764 /home/gentoox/.xfce4/xfce4rc 2> /dev/null
      fi
      if [ -f /root/.xfce4/xfce4rc ]; then
         sed -r "s/<Position x=\"[0-9]*\" y=\"[0-9]*\" screenwidth=\"[0-9]*\" screenheight=\"[0-9]*\"\/>/<Position x=\"$newxpos\" y=\"$newypos\" screenwidth=\"$newwidth\" screenheight=\"$newheight\"\/>/" /root/.xfce4/xfce4rc > /root/.xfce4/xfce4rc.2 2> /dev/null
         mv /root/.xfce4/xfce4rc.2 /root/.xfce4/xfce4rc 2> /dev/null
         chown gentoox:wheel /root/.xfce4/xfce4rc 2> /dev/null
         chmod 764 /root/.xfce4/xfce4rc 2> /dev/null
      fi
      if [ -f /etc/skel/.xfce4/xfce4rc ]; then
         sed -r "s/<Position x=\"[0-9]*\" y=\"[0-9]*\" screenwidth=\"[0-9]*\" screenheight=\"[0-9]*\"\/>/<Position x=\"$newxpos\" y=\"$newypos\" screenwidth=\"$newwidth\" screenheight=\"$newheight\"\/>/" /etc/skel/.xfce4/xfce4rc > /etc/skel/.xfce4/xfce4rc.2 2> /dev/null
         mv /etc/skel/.xfce4/xfce4rc.2 /etc/skel/.xfce4/xfce4rc 2> /dev/null
         chown gentoox:wheel /etc/skel/.xfce4/xfce4rc 2> /dev/null
         chmod 764 /etc/skel/.xfce4/xfce4rc 2> /dev/null
      fi
   fi
}

###
# xvkbdSet - Sets the correct resolution modes for XVKBD
###

xvkbdSet() {
   ###
   # GDM keyboard
   ###
   export patchname="gdm-patch" && queryDatabase
   if [ $patchinstalled == "true" ]; then
		mkdir -p /usr/share/gentoox 1> /dev/null 2> /dev/null
		echo "#!/bin/bash" > /usr/share/gentoox/loginkbd
		echo "killall xvkbd" >> /usr/share/gentoox/loginkbd
		echo $gdmxvkbdmode >> /usr/share/gentoox/loginkbd
   fi

   ###
   # User keyboards
   ###
   grep -v /usr/bin/X11/xvkbd /home/gentoox/.xinitrc > /home/gentoox/.xinitrc.tmp
   grep -v /usr/bin/X11/xvkbd /root/.xinitrc > /root/.xinitrc.tmp
   grep -v /usr/bin/X11/xvkbd /etc/skel/.xinitrc > /etc/skel/.xinitrc.tmp

   rm -rf /home/gentoox/.xinitrc
   rm -rf /root/.xinitrc
   rm -rf /etc/skel/.xinitrc

   export patchname="onxvkbd1" && queryDatabase
   if [ $patchinstalled == "false" ]; then
      echo -en "#" > /home/gentoox/.xinitrc
      echo -en "#" > /root/.xinitrc
      echo -en "#" > /etc/skel/.xinitrc
   fi

   echo -e $xvkbdmode >> /home/gentoox/.xinitrc
   cat /home/gentoox/.xinitrc.tmp >> /home/gentoox/.xinitrc 2> /dev/null
   rm -rf /home/gentoox/.xinitrc.tmp

   echo -e $xvkbdmode >> /root/.xinitrc
   cat /root/.xinitrc.tmp >> /root/.xinitrc 2> /dev/null
   rm -rf /root/.xinitrc.tmp

   echo -e $xvkbdmode >> /etc/skel/.xinitrc
   cat /etc/skel/.xinitrc.tmp >> /etc/skel/.xinitrc 2> /dev/null
   rm -rf /etc/skel/.xinitrc.tmp

   chmod 755 /home/gentoox/.xinitrc
   chmod 755 /etc/skel/.xinitrc
   chmod 755 /root/.xinitrc

   chown gentoox:users /home/gentoox/.xinitrc
   chown root:root /root/.xinitrc
   chown root:root /etc/skel/.xinitrc
}


###
# queryDatabase function - checks to see which patches are installed
###

queryDatabase() {
   if [ ! -z `grep -m1 -i ${patchname} /etc/conf.d/magic-database 2> /dev/null` ]; then
      export patchinstalled="true"
   else
      export patchinstalled="false"
   fi
   chown root:root /etc/conf.d/magic-database
   chmod 600 /etc/conf.d/magic-database
}

###
# Invalid selection
###

invalidselection() {
   header
   echo "     Invalid Selection, aborting."
   ding
   echo ""
   exit 1
}

###
# Countdown
###

countdown() {
   sleep 1
   echo -en "     Rebooting in ... "
   sleep 1
   for i in 10 9 8 7 6 5 4 3 2 1; do
      echo -en "${redtxt}${i} "
      sleep 1
   done
   echo -e "${bluetxt}"
   echo -e "     Continuing...${normaltxt}"
   /sbin/reboot 
   exit 0
}

###
# Ding-ding .. ding-ding .. ding-ding .. ding-ding .. ding-ding
###

ding() {
   export dingcount="0"
   while [ $dingcount -ne 5 ]; do
      echo -en "\a"
      sleep 0.1
      echo -en "\a"
      sleep 1
      export dingcount=`expr $dingcount + 1`
   done
}

header() {
   clear
   echo -e "\n\n\n${redtxt}            XBVSET ver${ver}       (c) ShALLaX ${normaltxt}(${bluetxt}gentoox@shallax.com${normaltxt})"
   echo -e "${normaltxt}      Be sure to check ${bluetxt}http://gentoox.shallax.com${normaltxt} regularly for updates!"
   echo -e "     --------------------------------------------------------------------"
   echo -en "${redtxt}"
   echo -e "                             Welcome to XBVSET v${ver}                      "
   echo -e "            This tool is designed to help you set your video mode.       "
   echo -en "${normaltxt}"
   echo -e "     --------------------------------------------------------------------"
   echo -e
   echo -e "     --------------------------------------------------------------------"
   echo -e "           Gentoox is distributed under the GNU General Public License.  "
   echo -e "     --------------------------------------------------------------------"
   echo -e ""
}

###
# Set the colours for text here
###
export bluetxt="\033[01;34m"
export redtxt="\033[01;31m"
export redblock="\033[41m\033[31m"
export normaltxt="\033[00m"
export greentxt="\033[01;32m"

if [ `whoami` != "root" ]; then
   header
   echo "     You must be root to use this utility"
   ding
   exit 1
fi

export encoder=`/bin/video`
if [ $encoder != "Conexant" ] && [ $encoder != "Focus" ] && [ $encoder != "Xcalibur" ]; then
   header
   echo "     Unsupported video encoder, aborting."
   ding
   echo ""
   exit 1
fi

export patchname="xserver-patch" && queryDatabase
header
echo -e "     You have a $encoder encoder which gives you the following options: "
echo -e "     (Note: Composite also works with SCART)"
echo -e ""
echo -e "     Composite"
if [ $encoder != "Xcalibur" ]; then
   echo -e "     X2VGA"
fi
echo -e "     HDTV"
echo -e "     "
echo -en "     Input: "
read overallmode
if [ -z $overallmode ]; then
   invalidselection
fi

if [ `echo $overallmode | tr A-Z a-z` != "composite" ] && [ `echo $overallmode | tr A-Z a-z` != "x2vga" ] && [ `echo $overallmode | tr A-Z a-z` != "hdtv" ]; then
   invalidselection
fi

if [ $encoder == "Xcalibur" ]; then
   if [ `echo $overallmode | tr A-Z a-z` == "x2vga" ]; then
      invalidselection
   fi
fi

if [ `echo $overallmode | tr A-Z a-z` == "composite" ]; then
   header
   echo -e "     What TV display mode would you like to use?\n"
   echo -e "     640x480"
   if [ $encoder != "Xcalibur" ]; then
      echo -e "     800x600"
   fi
   echo -e ""
   echo -en "     Input: "
   read tvmode
   if [ -z $tvmode ]; then
      invalidselection
   fi
   if [ $encoder == "Xcalibur" ] && [ `echo $tvmode | tr A-Z a-z` != "640x480" ]; then
      invalidselection
   fi
   if [ `echo $tvmode | tr A-Z a-z` != "640x480" ] && [ `echo $tvmode | tr A-Z a-z` != "800x600" ]; then
      invalidselection
   fi
   if [ $patchinstalled == "true" ]; then
      header
      echo -e "     What regional mode would you like to use?\n"
      echo -e "     PAL"
      echo -e "     NTSC"
      echo -e ""
      echo -en "     Input: "
      read region
      if [ -z $region ]; then
         invalidselection
      fi
      if [ `echo $region | tr A-Z a-z` != "pal" ] && [ `echo $region | tr A-Z a-z` != "ntsc" ]; then
         invalidselection
      fi
   fi

   ###
   # Xcalibur cannot do this
   ###
   if [ $encoder != "Xcalibur" ]; then
      header
      echo -e "     What HOC setting would you like to use?"
      echo -e "     HOC stands for 'Horizontal Overscan Correction' and is given"
      echo -e "     in '%'.  Basically, if your image is stretching out of the left and"
      echo -e "     right margins, this should fix it.  A higher number means more"
      echo -e "     compression adjustment.  Paste a number, then a 'return'. (13 is a"
      echo -e "     good value.)"
      if [ $encoder == "Focus" ]; then
         if [ `echo $tvmode | tr A-Z a-z` == "800x600" ]; then
            echo -e "                                                    13 14 15"
         else
            echo -e "                0  1  2  3  4  5  6  7  8  10 11 12 13 14 15"
         fi
      fi
      if [ $encoder == "Conexant" ]; then
         echo -e "                0  1  2  3  4  5  6  7  8  10 11 12 13 14 15"
      fi
      echo -e "                16 17 18 19 20 21 22 23 24 25 26 27 28 29 30"
      echo -e "                   Copy from the red block for 'return':${redblock} ${normaltxt}"
      echo -en "     Input: "
      read hoc
      if [ -z $hoc ]; then
         invalidselection
      fi
   
      if [ $encoder == "Focus" ] && [ $hoc -lt 13 ] && [ `echo $tvmode | tr A-Z a-z` == "800x600" ]; then
         invalidselection
      fi

      if [ $hoc -lt 0 ] || [ $hoc -gt 30 ]; then
         invalidselection
      fi

      header
      echo -e "     What VOC setting would you like to use?"
      echo -e "     VOC stands for 'Vertical Overscan Correction' and is given"
      echo -e "     in '%'.  Basically, if your image is stretching out of the top and"
      echo -e "     bottom margins, this should fix it.  A higher number means more"
      echo -e "     compression adjustment.  Paste a number, then a 'return'. (13 is a"
      echo -e "     good value)."
      echo -e "                0  1  2  3  4  5  6  7  8  10 11 12 13 14 15"
      echo -e "                16 17 18 19 20 21 22 23 24 25 26 27 28 29 30"
      echo -e "                   Copy from the red block for 'return':${redblock} ${normaltxt}"
      echo -en "     Input: "
      read voc
      if [ -z $voc ]; then
         invalidselection
      fi
      if [ $voc -lt 0 ] || [ $voc -gt 30 ]; then
         invalidselection
      fi
   else
      ###
      # If we have an Xcalibur, just set these to 0.
      ###
      hoc="0"
      voc="0"
   fi
fi


if [ `echo $overallmode | tr A-Z a-z` == "hdtv" ] || [ `echo $overallmode | tr A-Z a-z` == "x2vga" ]; then
   header
   echo -e "     What HDTV display mode would you like to use?\n"
   echo -e "     480p"
   if [ $encoder != "Xcalibur" ]; then
      echo -e "     720p"
   fi
   echo -e ""
   echo -en "     Input: "
   read hdtvmode
   if [ -z $hdtvmode ]; then
      invalidselection
   fi
   if [ $encoder == "Xcalibur" ]; then
      if [ `echo $hdtvmode | tr A-Z a-z` == "720p" ]; then
         invalidselection
      fi
   fi
   if [ `echo $hdtvmode | tr A-Z a-z` != "480p" ] && [ `echo $hdtvmode | tr A-Z a-z` != "720p" ]; then
      invalidselection
   fi
fi

if [ $patchinstalled == "true" ] && [ `echo $overallmode | tr A-Z a-z` == "x2vga" ]; then
   header
   echo -e "     What video mode would you like for X?\n"

   ###
   # ONLY 480p gives us 720x480
   ###
   if [ `echo $hdtvmode | tr A-Z a-z` == "480p" ]; then
      echo -e "     720x480"
   fi

   ###
   # Currently 1024x768 is NOT supported by Xcalibur or Focus
   ###
   if [ $encoder != "Xcalibur" ] && [ $encoder != "Focus" ]; then
      echo -e "     1024x768"
   fi

   ###
   # ONLY 720p gives us 1280x720
   ###
   if [ `echo $hdtvmode | tr A-Z a-z` == "720p" ]; then
      echo -e "     1280x720"
   fi

   echo -e ""
   echo -en "     Input: "
   read x2vgaxserver
   if [ -z $x2vgaxserver ]; then
      invalidselection
   fi

   ###
   # Only allow 480p -> 720x480/1024x768
   ###
   if [ `echo $hdtvmode | tr A-Z a-z` == "480p" ]; then
      if [ `echo $x2vgaxserver | tr A-Z a-z` != "1024x768" ] && [ `echo $x2vgaxserver | tr A-Z a-z` != "720x480" ]; then
         invalidselection
      fi
   fi

   ###
   # Only allow 780p -> 1024x768/1280x720
   ###
   if [ `echo $hdtvmode | tr A-Z a-z` == "720p" ]; then
      if [ `echo $x2vgaxserver | tr A-Z a-z` != "1280x720" ] && [ `echo $x2vgaxserver | tr A-Z a-z` != "1024x768" ]; then
         invalidselection
      fi
   fi

   ###
   # Do not allow overloading HDTV resolutions on Xcalibur or Focus
   ###
   if [ $encoder == "Xcalibur" ] || [ $encoder == "Focus" ]; then
      if [ `echo $x2vgaxserver | tr A-Z a-z` == "1024x768" ]; then
         invalidselection
      fi
   fi
fi
 
###
# Apply settings from here on!
###

###
# Xserver settings
###

header
if [ $patchinstalled == "true" ]; then
	###
	# Put files into place...
	###
   if [ ! -e /usr/lib/xorg/modules/drivers/nvxbox_drv.o ] || [ `md5sum /etc/nvxbox_drv.o.works | awk '{ print $1 }'` != `md5sum /usr/lib/xorg/modules/drivers/nvxbox_drv.o | awk '{ print $1 }'` ]; then
      cp /etc/nvxbox_drv.o.works /usr/lib/xorg/modules/drivers/nvxbox_drv.o 1> /dev/null 2> /dev/null
   fi

   if [ ! -f /usr/lib/xorg/modules/vnc.so ]; then
      cp /usr/lib/modules/vnc.so /usr/lib/xorg/modules/ 1> /dev/null 2> /dev/null
   fi
   cat /etc/xorg.conf.header > /etc/X11/xorg.conf

   if [ `echo $overallmode | tr A-Z a-z` == "composite" ]; then
      echo -en "     Changing Xserver video mode to: "
      sleep 1
		echo -e "Composite" `echo ${tvmode} | tr A-Z a-z` "-" `echo ${region} | tr a-z A-Z`
      if [ `echo $tvmode | tr A-Z a-z` == "640x480" ] && [ `echo $region | tr A-Z a-z` == "pal" ]; then
         echo "      Modes \"640x480-pal\"" >> /etc/X11/xorg.conf
         export newxpos="16"
         export newypos="434"
         export newwidth="640"
         export newheight="480"
         xfce4rc
      fi
      if [ `echo $tvmode | tr A-Z a-z` == "800x600" ] && [ `echo $region | tr A-Z a-z` == "pal" ]; then
         echo "      Modes \"800x600-pal\"" >> /etc/X11/xorg.conf
         export newxpos="96"
         export newypos="554"
         export newwidth="800"
         export newheight="600"
         xfce4rc
      fi
      if [ `echo $tvmode | tr A-Z a-z` == "640x480" ] && [ `echo $region | tr A-Z a-z` == "ntsc" ]; then
         echo "      Modes \"640x480-ntsc\"" >> /etc/X11/xorg.conf
         export newxpos="16"
         export newypos="434"
         export newwidth="640"
         export newheight="480"
         xfce4rc
      fi
      if [ `echo $tvmode | tr A-Z a-z` == "800x600" ] && [ `echo $region | tr A-Z a-z` == "ntsc" ]; then
         echo "      Modes \"800x600-ntsc\"" >> /etc/X11/xorg.conf
         export newxpos="96"
         export newypos="554"
         export newwidth="800"
         export newheight="600"
         xfce4rc
      fi
      sleep 1
   fi

   if [ `echo $overallmode | tr A-Z a-z` == "hdtv" ]; then
      echo -en "     Changing Xserver video mode to: "
      sleep 1
      echo -e "HDTV" `echo $hdtvmode | tr A-Z a-z` `echo $hdtvxserver | tr A-Z a-z`
      if [ `echo $hdtvmode | tr A-Z a-z` == "480p" ]; then
         echo "      Modes \"hdtv-480p\"" >> /etc/X11/xorg.conf
         export newxpos="51"
         export newypos="434"
         export newwidth="704"
         export newheight="480"
         xfce4rc
         export fbset="480p"
#         fbSetup
      fi
      if [ `echo $hdtvmode | tr A-Z a-z` == "720p" ]; then
         echo "      Modes \"hdtv-720p\"" >> /etc/X11/xorg.conf
         export newxpos="336"
         export newypos="674"
         export newwidth="1280"
         export newheight="720"
         xfce4rc
         export fbset="720p"
#         fbSetup
      fi
      sleep 1
   fi

   if [ `echo $overallmode | tr A-Z a-z` == "x2vga" ]; then
      echo -en "     Changing Xserver video mode to: "
      sleep 1
      echo -e "X2VGA" `echo $hdtvmode | tr A-Z a-z` `echo $x2vgaxserver | tr A-Z a-z`
      if [ `echo $x2vgaxserver | tr A-Z a-z` == "720x480" ]; then
         echo "      Modes \"x2vga-720x480\"" >> /etc/X11/xorg.conf
         export newxpos="56"
         export newypos="434"
         export newwidth="720"
         export newheight="480"
         xfce4rc
         export fbset="x2-800x600"
#         fbSetup
      fi
      if [ `echo $x2vgaxserver | tr A-Z a-z` == "1024x768" ]; then
         echo "      Modes \"x2vga-1024x768\"" >> /etc/X11/xorg.conf
         export newxpos="208"
         export newypos="724"
         export newwidth="1024"
         export newheight="768"
         xfce4rc
         export fbset="x2-1024x768"
#         fbSetup
      fi
      if [ `echo $x2vgaxserver | tr A-Z a-z` == "1280x720" ]; then
         echo "      Modes \"x2vga-1280x720\"" >> /etc/X11/xorg.conf
         export newxpos="336"
         export newypos="674"
         export newwidth="1280"
         export newheight="720"
         xfce4rc
         export fbset="x2-1024x768"
#         fbSetup
      fi
      sleep 1
   fi
   cat /etc/xorg.conf.footer >> /etc/X11/xorg.conf
fi

###
# Bootloader settings
###

mount /dev/hda50 /mnt/fatx/e 1> /dev/null 2> /dev/null
mount -o remount,rw /mnt/fatx/e 1> /dev/null 2> /dev/null

cfgpath="/mnt/fatx/e/linuxboot.cfg"

###
# Rebuild the linuxboot.cfg
#
# Store as much of the old information as possible, but also use fallbacks
# incase some information is missing.
###
if [ ! -f $cfgpath ]; then
	touch $cfgpath
fi

rm -rf $cfgpath.tmp 1> /dev/null 2> /dev/null
touch $cfgpath.tmp

title="Gentoox"
getDetails

if [ -z `echo $kernelline | awk '{ print $1 }'` ]; then
   kernelline="kernel vmlinuz"
fi

if [ -z `echo $initrdline | awk '{ print $1 }'` ]; then
   initrdline="initrd initrd.gz"
fi
if [ -z `echo $appendline | grep "dontdeleteme" | awk '{ print $1 }'` ]; then
	appendline="append init=/linuxrc installtype=${INSTALLTYPE} root=/dev/ram0 kbd-reset"
else
   appendline=`echo $appendline | sed 's/video=.*oc=[0-9]*//'`
   appendline=`echo $appendline | sed 's/dontdeleteme//g'`
   appendline="$appendline dontdeleteme"
fi

if [ `echo $overallmode | tr A-Z a-z` == "x2vga" ] || [ `echo $overallmode | tr A-Z a-z` == "hdtv" ]; then
   echo -en "     Updating bootloader to: "
   sleep 1
   echo -e "video=xbox:$hdtvmode,nohwcursor"
   appendline="$appendline video=xbox:$hdtvmode,nohwcursor"
else
   echo -en "     Updating bootloader to: "
   sleep 1
   echo -e "video=xbox:$tvmode,nohwcursor,hoc=$hoc,voc=${voc}"
   appendline="$appendline video=xbox:$tvmode,nohwcursor,hoc=$hoc,voc=${voc}"
fi

updateDetails

title="Sparkle"
getDetails
if [ -z `echo $kernelline | awk '{ print $1 }'` ] || [ `echo $kernelline | awk '{ print $2 }'` == "sparkle" ]; then
   kernelline="kernel vmlinuz"
fi

if [ -z `echo $initrdline | awk '{ print $1 }'` ]; then
   initrdline="initrd sprkrd.gz"
fi

if [ -z `echo $appendline | awk '{ print $1 }'` ]; then
   appendline="append load_ramdisk=1 prompt_ramdisk=0 ramdisk_size=26215 rw root=/dev/ram kbd-reset"
fi

updateDetails

mv $cfgpath.tmp $cfgpath

sleep 1

###
# xbvinit settings
###

###
# Now deprecated since xbv doesnt work on focus
###
echo "" > /etc/conf.d/xbvinit

###
# XVKBD settings
###

export patchname="xvkbd-patch" && queryDatabase
if [ $patchinstalled == "true" ]; then
   if [ `echo $overallmode | tr A-Z a-z` == "composite" ]; then

      export patchname="onxfce1" && queryDatabase
      if [ $patchinstalled == "true" ]; then
         if [ `echo $tvmode | tr A-Z a-z` == "640x480" ]; then
            export xvkbdmode="/usr/bin/X11/xvkbd -geometry 632x130+0-74 &"
            export gdmxvkbdmode="/usr/bin/X11/xvkbd -geometry 460x130+0-0 -no-keypad &"
         elif [ `echo $tvmode | tr A-Z a-z` == "800x600" ]; then
            export xvkbdmode="/usr/bin/X11/xvkbd -geometry 792x130+0-74 &"
            export gdmxvkbdmode="/usr/bin/X11/xvkbd -geometry 620x130+0-0 -no-keypad &"
         fi
      fi

   elif [ `echo $overallmode | tr A-Z a-z` == "hdtv" ]; then

      export patchname="onxfce1" && queryDatabase
      if [ $patchinstalled == "true" ]; then
         if [ `echo $hdtvmode | tr A-Z a-z` == "480p" ]; then
            export xvkbdmode="/usr/bin/X11/xvkbd -geometry 696x130+0-74 &"
            export gdmxvkbdmode="/usr/bin/X11/xvkbd -geometry 524x130+0-0 -no-keypad &"
         elif [ `echo $hdtvmode | tr A-Z a-z` == "720p" ]; then
            export xvkbdmode="/usr/bin/X11/xvkbd -geometry 1272x130+0-74 &"
            export gdmxvkbdmode="/usr/bin/X11/xvkbd -geometry 1100x130+0-0 -no-keypad &"
         fi
      fi

   elif [ `echo $overallmode | tr A-Z a-z` == "x2vga" ]; then

      export patchname="onxfce1" && queryDatabase
      if [ $patchinstalled == "true" ]; then
         if [ `echo $x2vgaxserver | tr A-Z a-z` == "720x480" ]; then
            export xvkbdmode="/usr/bin/X11/xvkbd -geometry 712x130+0-74 &"
            export gdmxvkbdmode="/usr/bin/X11/xvkbd -geometry 540x130+0-0 -no-keypad &"
         elif [ `echo $x2vgaxserver | tr A-Z a-z` == "1024x768" ]; then
            export xvkbdmode="/usr/bin/X11/xvkbd -geometry 1016x130+0-74 &"
            export gdmxvkbdmode="/usr/bin/X11/xvkbd -geometry 844x130+0-0 -no-keypad &"
         elif [ `echo $x2vgaxserver | tr A-Z a-z` == "1280x720" ]; then
            export xvkbdmode="/usr/bin/X11/xvkbd -geometry 1272x130+0-74 &"
            export gdmxvkbdmode="/usr/bin/X11/xvkbd -geometry 1100x130+0-0 -no-keypad &"
         fi
      fi

   fi
   echo -en "     Setting XVKBD to: "
   sleep 1
   xvkbdSet
   grep -m1 -i "xvkbd" /home/gentoox/.xinitrc
fi
countdown
