#!/bin/sh
#vfixmbr a small script setup to rewrite mbr came about from many people that cant get rid 
#of GRUB (never happen to me)
#we use the Linux utility ms-sys here from ms-sys.sourceforge.net
#
# Written By Uel Archuletta for VLocityLinux


vdir=$(dirname $0)
. $vdir/vasm-functions

check_root

TMP=/var/log/setup/tmp
mkdir -p $TMP

# VARIABLES
TP_X=${1:-/}
if [ "$TP_X" = "/" ]; then
    ROOT_DIR=""
else
    ROOT_DIR=$TP_X
fi

menu_fix_mbr()
{
##check to see if more than 1 mbr is available if only 1 use it
HardDisk=`fdisk -l | grep Disk | cut -d " " -f 2 | cut -c 1-8`
x=`echo $HardDisk | wc -w`
   if [ $x != 1 ]; then 
     j="0"
     for i in $HardDisk; do
     hd[$j]="$i"
     j=`expr $j + 1`
     done
     $DCMD --backtitle "$BACKTITLE" --title "MBR CLEAN" --menu "\n
     MS-SYS rewrite your  mbr \n
     you chose to Rewrite your MBR, and \n
     we need to know which mbr to overwrite \n
     Choose which hard disk you would like to use" \
     20 74 4 \
     ${hd[0]} ${hd[0]} \
     ${hd[1]} ${hd[1]} \
     ${hd[2]} ${hd[2]} \
     ${hd[3]} ${hd[3]} \
     ${hd[4]} ${hd[4]} \
     ${hd[5]} ${hd[5]} \
     2> $freply
     result=$?
     [ $result != 0 ] && return $result
     TARGET="`cat $freply`"
   else
     TARGET="$HardDisk"
   fi
 $DCMD --backtitle "$BACKTITLE" --title "---MS-SYS---" --menu "\n
This is a Linux program for writing Microsoft compatible boot records. 
The program does the same as Microsoft \"fdisk /mbr\" to a hard disk\n
You should know what program partitioned your disk first\n
if you don't this program could cause problems with your partitions\n
The first option should be the safest\n
The last option should only be used as the last resort \n
Which option would you like?" 20 74 6 \
 "linux"     "Write a public domain syslinux MBR to device" \
"dos"       "Write a DOS/Windows NT MBR to device" \
"95b"       "Write a Windows 95B/98/98SE/ME MBR to device" \
"2k-XP"     "Write a Windows 2000/XP/2003 MBR to device" \
"zero"      "Write an empty (zeroed) MBR to device" \
  2> $freply

 result=$?
 [ $result != 0 ] && return $result

reply="`cat $freply`"
case "$reply" in
  linux)
    /usr/bin/ms-sys --mbrsyslinux $TARGET > $freply
    reply="`cat $freply`"
    infobox "$reply \n now lets install lilo"
    sleep 4
    vliloconf
  ;;
 dos)
  /usr/bin/ms-sys --mbrdos $TARGET > $freply
    reply="`cat $freply`"
    infobox "$reply \n now lets install lilo"
    sleep 4
    vliloconf
  ;;
 95b)
  /usr/bin/ms-sys --mbr95b $TARGET > $freply
    reply="`cat $freply`"
    infobox "$reply \n now lets install lilo"
    sleep 4
    vliloconf
  ;;
  2k-XP)
  /usr/bin/ms-sys --mbr $TARGET > $freply
    reply="`cat $freply`"
    infobox "$reply \n now lets install lilo"
    sleep 4
    vliloconf
  ;;
zero)
  /usr/bin/ms-sys --mbrzero $TARGET > $freply
    reply="`cat $freply`"
    infobox "$reply \n now lets install lilo"
    sleep 4
    vliloconf
  ;;
esac 
exit
}

############################
# MAIN MENU
wizard menu_fix_mbr

