#!/bin/sh
# @vasm: BOOT
# @level: root
# @description: select default run level for booting
# 
# (c) Kocil, 2003
# (c) Vector Linux, 2003

if [ ! $DISPLAY = "" ]; then
  DCMD="Xdialog --wrap" 
  CMD="rxvt -e "
  BACKTITLE=''  
else
  DCMD="dialog"
  CMD=""
  BACKTITLE='Vector Linux ... at the speed of light'
fi

# must be root !
if [ ! "$UID" = "0" ]; then
$DCMD --backtitle "$BACKTITLE" --title " SORRY " --msgbox \
"\nThis is a root only configurator. 
Please login as root, or use su to become a root first" 0 0
  exit
fi

# just a debugging aid.
dbug() {
  echo $* > /dev/null
}

###################################################
# Change default runlevel
set_runlevel()
{
    RUNLEVEL=$1
    cat /etc/inittab | while read LINE; do
      if echo $LINE | grep initdefault 1> /dev/null ; then
        echo "id:$RUNLEVEL:initdefault:" >> /etc/inittab.new
      else
        echo $LINE >> /etc/inittab.new
      fi
    done
    mv /etc/inittab.new /etc/inittab
}


###################################################                                                                                            
# set tmp files                                                                                                                                
fmenu=`mktemp -q /tmp/menu.XXXXXX` && freply=`mktemp -q /tmp/reply.XXXXXX`
if [ $? != 0 ]; then
  echo Can not create temporary file
  exit 1                                                                                                                                      
fi
   
###################################################
select_runlevel()
{
TITLE="BOOT CONFIGURATOR"
TEXT="Please select default run-level for booting the system"
DIMENSION="12 60 4"
[ "$CMD" ] && DIMENSION="12 60 4"

$DCMD --backtitle "$BACKTITLE" --title "$TITLE" --menu \
"$TEXT" $DIMENSION \
2 "Text user interface desktop" \
3 "Text user interface server" \
4 "Graphical user interface desktop" \
5 "Graphical user interface server" 2> $freply
[ ! $? = 0 ] && return 1

RUNLEVEL=`cat $freply`

case $RUNLEVEL in
2|3|4|5)
  set_runlevel $RUNLEVEL
  $DCMD --backtitle "$BACKTITLE" --title "$TITLE" --msgbox \
  "The System will run level $RUNLEVEL on the next boot." 0 0
  ;;  
esac
}

##################################################
# main program

select_runlevel

rm -f $fmenu
rm -f $freply

