#!/bin/sh
trap "exit 0" 2

######################################
# Installationsvorgaben		
######################################

# Installations-Platte
HD_DEV="/dev/hda"

# Minimal erlaubte Groesse der Platte
DISK_MINSIZE=5000

# Partitionsnummern ( Falls BOOT=0 wird keine separate BootPartition angelegt -> Dual-Boot)
BOOT_PART_NUM=1
SYSTEM_PART_NUM=2
SWAP_PART_NUM=3
VIDEO_PART_NUM=5

# Minimal erlaubte Partitionsgroesse
BOOT_PART_MINSIZE=15
SYSTEM_PART_MINSIZE=3700
SWAP_PART_MINSIZE=64
VIDEO_PART_MINSIZE=0

# Standard Partitionsgroesse
BOOT_PART_DEFSIZE=25
SYSTEM_PART_DEFSIZE=8000
SWAP_PART_DEFSIZE=260
VIDEO_PART_DEFSIZE=10

# Standard Filesystem ( ext3,xfs,reiserfs )
FS_ROOT_DEFAULT="ext3"
FS_VIDEO_DEFAULT="xfs"

# Sollen die vorhandenen Einstellungen des alten Systems uebernommen werden ?
DO_BACKUP=1

########################################
### ab hier nichts mehr aendern
########################################

FS_ROOT="auto"
FS_VIDEO="auto"

# Log-Datei
LOG_FILE="/install.log"
PARTED_OUT="/tmp/parted.out"
EXEC_RC=0
INST_DISK=""

TABC="	"
AUTO_PART_DONE=0

msgKey() {
   if [ "$MP_AUTO_INST" = "0" ] ; then
      APP_STR=" - Enter"
   else
      APP_STR=""
   fi
   echo ""
   echo -e "\007"
   echo -e "\033[44;37m$1$APP_STR\033[0m"
   if [ "$MP_AUTO_INST" != "1" ] ; then
      read xx
   else
      sleep 2
   fi
}


execCmd() {
   echo "Exec <$1>" | tee -a $LOG_FILE
   $1 2>&1 | tee -a $LOG_FILE
   EXEC_RC=$?
   sleep 1
}


autoPart() {
   clear
   echo ""
   echo -e "\033[44;37mCtrl + C bricht ab !\033[0m"
   echo ""
   
   i=10
   while [ $i != 0 ] ; do
      echo -e "\033[5;47;31mSaemtliche Partitionen auf $HD_DEV werden in $i Sekunde(n) neu angelegt !!!\033[0m"
      echo -e "\007"
      sleep 1
      i=`expr $i - 1`
   done
   
   
   ALL_PARTS=`cat $PARTED_OUT | cut -f 1 -d " " | grep "^[123456789]"`
   
   #if [ "$ALL_PARTS" != "" ]; then
   #   for i in $ALL_PARTS ; do
   #      execCmd "parted -s $HD_DEV rm ${i}"
   #   done
   #fi
   
   execCmd "dd if=/dev/zero of=$HD_DEV bs=512 count=1"
   execCmd "parted -s $HD_DEV mklabel msdos"
   
   parted --script $HD_DEV print > $PARTED_OUT
   execCmd "cat $PARTED_OUT"
   
   execCmd "parted -s $HD_DEV mkpart primary ext2 0 $BOOT_PART_DEFSIZE"
   P_END=$(($BOOT_PART_DEFSIZE + $SYSTEM_PART_DEFSIZE))
   execCmd "parted -s $HD_DEV mkpart primary ext2 $BOOT_PART_DEFSIZE $P_END"
   P_START=$P_END
   P_END=$(($P_START + $SWAP_PART_DEFSIZE))
   execCmd "parted -s $HD_DEV mkpart primary linux-swap $P_START $P_END"
   DSIZE=`cat $PARTED_OUT | grep geometry | sed 's/.*-//' | sed 's/ .*//'`
   execCmd "parted -s $HD_DEV mkpart extended $P_END $DSIZE"
   if [ ! "`tail -n 1 $LOG_FILE | grep \"^Error\"`" = "" ] ; then
      DS1=`echo "$DSIZE" | cut -d '.' -f1`
      DS2=`echo "$DSIZE" | cut -d '.' -f2`
      if [ "$DS2" = "000" ] ; then
         DS2=999
         DS1=$(($DS1 - 1))
      else
         DS2=$(($DS2 - 1))
      fi
   
      execCmd "parted -s $HD_DEV mkpart extended $P_END $DS1.$DS2"
      if [ ! "`tail -n 1 $LOG_FILE | grep \"^Error\"`" = "" ] ; then
         DS1=$(($DS1 - 1))
         execCmd "parted -s $HD_DEV mkpart extended $P_END $DS1.$DS2"
      fi
      DSIZE="$DS1.$DS2"
   fi
   execCmd "parted -s $HD_DEV mkpart logical ext2 $P_END $DSIZE"
   
   msgKey "Partitionen wurden angelegt"
   export AUTO_PART_DONE=1
}


checkPart() {
   partNum=$1; partSize=$2; partSWP=$3
   cpRc=1
   field=`grep ^$partNum $PARTED_OUT`
   start=`echo $field | cut -d ' ' -f2 | cut -d '.' -f1`
   if [ $start ]; then
      end=`echo $field | cut -d ' ' -f3 | cut -d '.' -f1`
   #   echo "<$field>"
      fstype=`echo $field | cut -d ' ' -f5`
   #   echo "<$fstype>"
      psize=`expr $end - $start`
   #   if [ $partSWP = 0 ] ; then
   #      if [ "$fstype" = "linux-swap" ] ; then
   #         echo "$HD_DEV$partNum ist eine Swap-Partition"
   #         cpRc=0
   #      fi
   #   fi
      echo "$HD_DEV$partNum: $psize MB - $fstype"
      if [ $partSize -gt $psize ]; then
         echo "$HD_DEV$partNum zu klein - Minimum $partSize MB"
         cpRc=0
      fi
   else
      echo "$HD_DEV$partNum nicht vorhanden"
      cpRc=0
   fi
   
   return $cpRc
}


checkAllPart() {
   clear
   capRc=1
   parted --script $HD_DEV print > $PARTED_OUT
   
   #partNum=1; partSize=10; partSWP=0
   if checkPart $BOOT_PART_NUM $BOOT_PART_MINSIZE 0 ; then
      capRc=0
   fi
   
   #partNum=2; partSize=1500; partSWP=0
   if checkPart $SYSTEM_PART_NUM $SYSTEM_PART_MINSIZE 0 ; then
      capRc=0
   fi
   
   #partNum=3; partSize=120; partSWP=1
   if checkPart $SWAP_PART_NUM $SWAP_PART_MINSIZE 1 ; then
      capRc=0
   fi
   
   #partNum=5; partSize=100; partSWP=0
   if checkPart $VIDEO_PART_NUM $VIDEO_PART_MINSIZE 0 ; then
      capRc=0
   fi
   
   DSIZE=`cat $PARTED_OUT | grep geometry | sed 's/.*-//' | sed 's/ .*//'`
   echo "DiskSize: $DSIZE MB"
   
   return $capRc
}

findDisks() {
   rm -rf /tmp/ip.* > /dev/null 2>&1
   echo "dialog --backtitle 'VDR Installationsprogramm' --title 'Installations-Platte auswaehlen' \\" > /tmp/ip.tmp
   ALL_DISKS=""
   COUNT_DISKS=0
   ATA_DISKS=`dmesg |grep " MB)"|grep "^hd"|cut -f 1 -d ":"`
   for i in $ATA_DISKS ; do
      FOUND=`echo "ALL_DISKS" | grep "${i}"`
      if [ "$FOUND" = "" ] ; then
         DS=`dmesg |grep " MB)"|grep "^${i}"|cut -f 2 -d "("|cut -f 1 -d " "`
         for j in $DS ; do
            DISK_SIZE=${j}
         done
         if [ $DISK_SIZE -ge $DISK_MINSIZE ] ; then
            if [ "$ALL_DISKS" = "" ] ; then
               ALL_DISKS="${i}"
               INST_DISK="${i}"
            else
               ALL_DISKS="$ALL_DISKS ${i}"
            fi
            echo "\"$i\" \"$DISK_SIZE MB\" \\" >>/tmp/ip.tm1
            COUNT_DISKS=$(($COUNT_DISKS + 1))
         else
            msgKey "Disk ${i} ist zu klein ($DISK_SIZE < $DISK_MINSIZE)"
         fi
      fi
   done
   SCSI_DISKS=`dmesg | grep "^Attached scsi disk" | cut -f 4 -d " "`
   for i in $SCSI_DISKS ; do
      FOUND=`echo "$ALL_DISKS" | grep "${i}"`
      if [ "$FOUND" = "" ] ; then
         DS=`dmesg |grep " MB)"|grep "${i}:"|cut -f 2 -d "("|cut -f 1 -d " "`
         for j in $DS ; do
            DISK_SIZE=${j}
         done
         if [ $DISK_SIZE -ge $DISK_MINSIZE ] ; then
            if [ "$ALL_DISKS" = "" ] ; then
               ALL_DISKS="${i}"
               INST_DISK="${i}"
            else
               ALL_DISKS="$ALL_DISKS ${i}"
            fi
            echo "\"$i\" \"$DISK_SIZE MB\" \\" >>/tmp/ip.tm1
            COUNT_DISKS=$(($COUNT_DISKS + 1))
         else
            msgKey "Disk ${i} ist zu klein ($DISK_SIZE < $DISK_MINSIZE)"
         fi
      fi
   done
   if [ $COUNT_DISKS -ge 2 ] && [ "$MP_AUTO_INST" = "0" ] ; then
      LN=$(($COUNT_DISKS + 6))

      echo "Disks: < $ALL_DISKS >"
      echo "  --menu '' $LN 40 $COUNT_DISKS \\" >> /tmp/ip.tmp
      cat /tmp/ip.tm1 >> /tmp/ip.tmp
      echo " 2>/tmp/menu.tmp" >>/tmp/ip.tmp
      echo "if [ "$?" != "0" ] ; then" >>/tmp/ip.tmp
      echo "   rm -f /tmp/menu.tmp" >>/tmp/ip.tmp
      echo "fi" >>/tmp/ip.tmp

      rm -f /tmp/menu.tmp
      bash /tmp/ip.tmp
    
      if [ -s /tmp/menu.tmp ] ; then
         INST_DISK=`cat /tmp/menu.tmp`
      fi
   fi
   if [ "$INST_DISK" = "" ] ; then
      return 0
   fi
   HD_DEV="/dev/$INST_DISK"
   return 1
}


doPart() {
   if [ "$MP_AUTO_INST" = "1" ] ; then
      if checkAllPart ; then
         autoPart
         if checkAllPart ; then
            msgKey "Partitionierung ungueltig"
            return 0
         fi
      fi
      return 1
   fi
   
   while [ 0 ] ; do
      clear
      dialog --backtitle "VDR Installationsprogramm" --title "PARTITIONS MENU" --menu \
      "AUTO  - macht die Standard-Partitionierung (empfohlen)
      KEEP  - belaesst die Partitionierung (Videos bleiben erhalten)
      CFDSK - Fuer Experten ( und fuer DualBoot )
      
       Es werden auf $HD_DEV folgende Partitionen benoetigt:
        1: primaer /boot  Type83 >=   15 MB ( Default: 25 )
        2: primaer /      Type83 >= 3700 MB ( 8000 )
        3: primaer /swap  Type82 >=   64 MB ( 260 )
        5: logisch /video Type83 >=   10 MB ( Restliche Platte )" \
       19 70 4 \
      "AUTO" "Automatische Partitionierung mit Standard-Groessen" \
      "KEEP" "Partitionierung belassen" \
      "CFDSK" "CFDISK fuer die manuelle Partitionierung starten" \
      "EXIT" "Installation beenden" 2> /tmp/menu.tmp
      
      case $? in
         0)
           ;;
         1)
           return 0
           ;;
         255)
           return 0
           ;;
      esac
      clear
      
      menu=`cat /tmp/menu.tmp`
      rm -f /tmp/menu.tmp
      
      if [ "$menu" = "AUTO" ]; then
         autoPart
      fi
      if [ "$menu" = "CFDSK" ]; then
         cfdisk $HD_DEV
      #reboot
      fi
      if [ "$menu" = "EXIT" ]; then
         return 0
      fi
      
      if checkAllPart ; then
         dialog --defaultno --yesno "Partitionierung ungueltig - Dennoch installieren ?" 5 65
         if [ $? = 0 ] ; then
            return 1
         fi
      else
         return 1
      fi
   done
}


doFormat() {
   execCmd "mount $HD_DEV$SYSTEM_PART_NUM /mnt"
   if [ -d /mnt/mnt/hda5 ] && [ "$DO_BACKUP" = "1" ] ; then
      execCmd "mount $HD_DEV$VIDEO_PART_NUM /mnt/mnt/hda5"
      if [ -d /mnt/mnt/hda5/video ] ; then
         msgKey "Sichere /etc und /_config nach /hda5/g2v-backup"
         if [ -d /mnt/mnt/hda5/g2v-backup/_config ] ; then
            if [ "$MP_AUTO_INST" != "1" ] ; then
               dialog --defaultyes --yesno "Soll der bereits vorhandene Backup ueberschrieben werden ?" 5 70
               if [ $? = 0 ] ; then
                  execCmd "rm -rf /mnt/mnt/hda5/g2v-backup/*"
               fi
            else
               execCmd "rm -rf /mnt/mnt/hda5/g2v-backup/*"
            fi
         else
            execCmd "mkdir /mnt/mnt/hda5/g2v-backup"
         fi
   
         if [ ! -d /mnt/mnt/hda5/g2v-backup/etc ] ; then
            execCmd "cp -af /mnt/etc /mnt/mnt/hda5/g2v-backup"
         fi
         if [ ! -d /mnt/mnt/hda5/g2v-backup/_config ] ; then
            execCmd "cp -af /mnt/_config /mnt/mnt/hda5/g2v-backup"
         fi
      fi
      execCmd "umount $HD_DEV$VIDEO_PART_NUM"
   fi
   execCmd "umount $HD_DEV$SYSTEM_PART_NUM"
   
   
   if [ "$MP_AUTO_INST" != "1" ] ; then
      dialog --defaultno --yesno "Soll Dual-Boot verwendet werden ?" 5 55
      if [ $? = 0 ] ; then
         BOOT_PART_NUM="0"
      fi
   fi
   
   if [ "$BOOT_PART_NUM" != "0" ] ; then
      msgKey "Formatiere Boot-Partition $HD_DEV$BOOT_PART_NUM mit ext2"
      execCmd "mke2fs $HD_DEV$BOOT_PART_NUM"
      execCmd "tune2fs -i 0 -c 0 $HD_DEV$BOOT_PART_NUM"
      sleep 3
   fi
   
   
   if [ "$MP_AUTO_INST" != "1" ] ; then
      clear
      dialog --backtitle "VDR Installationsprogramm" --title "System-Partition formatieren" --menu \
      "Waehle das Datei-System fuer die System-Partition $HD_DEV$SYSTEM_PART_NUM" \
      11 65 3 \
      "EXT3" "Sehr solide ( empfohlen )" \
      "XFS" "" \
      "REISERFS" "" 2> /tmp/menu.tmp
   
      case $? in
         0)
           ;;
         1)
           return 0
           ;;
         255)
           return 0
           ;;
      esac
      clear
      menu=`cat /tmp/menu.tmp`
      rm -f /tmp/menu.tmp
   
      echo ""
      echo "Formatiere System-Partition $HD_DEV$SYSTEM_PART_NUM mit $menu ..."
      echo ""
      if [ "$menu" = "EXT3" ]; then
         FS_ROOT='ext3'
      else if [ "$menu" = "XFS" ]; then
         FS_ROOT='xfs'
      else if [ "$menu" = "REISERFS" ]; then
         FS_ROOT='reiserfs'
      fi
   else
      echo ""
      echo "Formatiere System-Partition $HD_DEV$SYSTEM_PART_NUM mit EXT3 ..."
      echo ""
      FS_ROOT=$FS_ROOT_DEFAULT
   fi
   
   if [ "$FS_ROOT" = "ext3" ]; then
      execCmd "mke2fs -j $HD_DEV$SYSTEM_PART_NUM"
      execCmd "tune2fs -i 0 -c 0 $HD_DEV$SYSTEM_PART_NUM"
   else if [ "$FS_ROOT" = "xfs" ]; then
      execCmd "mkfs.xfs -f $HD_DEV$SYSTEM_PART_NUM"
   else if [ "$FS_ROOT" = "reiserfs" ]; then
      echo "mkreiserfs -f $HD_DEV$SYSTEM_PART_NUM"
      echo y |mkreiserfs -f $HD_DEV$SYSTEM_PART_NUM
   fi
   
   sleep 3
   
   msgKey "Ernenne $HD_DEV$SWAP_PART_NUM zur Swap-Partition"
   execCmd "mkswap $HD_DEV$SWAP_PART_NUM"
   sleep 3
   
   if [ "$MP_AUTO_INST" != "1" ] ; then
      execCmd "mount $HD_DEV$VIDEO_PART_NUM /mnt"
      if [ -d /mnt/video ] ; then
         KEEP_FS1='KEEP'
         KEEP_FS2='Belassen'
      else
         KEEP_FS1=''
         KEEP_FS2=''
      fi
      umount /mnt >/dev/null 2>&1
      clear
      dialog --backtitle "VDR Installationsprogramm" --title "Video-Partition formatieren" --menu \
      "Waehle das Datei-System fuer die Video-Partition" \
      11 65 3 \
      $KEEP_FS1 $KEEP_FS2 \
      "EXT3" "" \
      "XFS" "" \
      "REISERFS" "" 2> /tmp/menu.tmp
   
      case $? in
         0)
           ;;
         1)
           return 0
           ;;
         255)
           return 0
           ;;
      esac
      clear
      menu=`cat /tmp/menu.tmp`
      rm -f /tmp/menu.tmp
   
      if [ "$menu" != "KEEP" ]; then
         echo ""
         echo "Formatiere Video-Partition $HD_DEV$VIDEO_PART_NUM mit $menu ..."
         echo ""
         if [ "$menu" = "EXT3" ]; then
            FS_VIDEO='ext3'
            execCmd "mke2fs -j $HD_DEV$VIDEO_PART_NUM"
            execCmd "tune2fs -i 0 -c 0 $HD_DEV$VIDEO_PART_NUM"
         else if [ "$menu" = "XFS" ]; then
            FS_VIDEO='xfs'
            execCmd "mkfs.xfs -f $HD_DEV$VIDEO_PART_NUM"
         else if [ "$menu" = "REISERFS" ]; then
            FS_VIDEO='reiserfs'
            echo "mkreiserfs -f $HD_DEV$VIDEO_PART_NUM"
            echo y |mkreiserfs -f $HD_DEV$VIDEO_PART_NUM
         fi
      fi
   else
      if [ "$AUTO_PART_DONE" = "1" ]; then
         echo ""
         echo "Formatiere Video-Partition $HD_DEV$VIDEO_PART_NUM mit EXT3 ..."
         echo ""
         FS_VIDEO='ext3'
         execCmd "mke2fs -j $HD_DEV$VIDEO_PART_NUM"
         execCmd "tune2fs -i 0 -c 0 $HD_DEV$VIDEO_PART_NUM"
      fi
      sleep 3
   fi
   if [ "$FS_VIDEO" != "auto" ]; then
      if [ "$FS_VIDEO" = "ext3" ]; then
         execCmd "mke2fs -j $HD_DEV$VIDEO_PART_NUM"
         execCmd "tune2fs -i 0 -c 0 $HD_DEV$VIDEO_PART_NUM"
      else if [ "$FS_VIDEO" = "xfs" ]; then
         execCmd "mkfs.xfs -f $HD_DEV$VIDEO_PART_NUM"
      else if [ "$FS_VIDEO" = "reiserfs" ]; then
         echo "mkreiserfs -f $HD_DEV$VIDEO_PART_NUM"
         echo y |mkreiserfs -f $HD_DEV$VIDEO_PART_NUM
      fi
   fi
   return 1
}


doInstall() {
   if [ "$MP_AUTO_INST" != "1" ] ; then
      APP_STR=" - Enter"
   else
      APP_STR=""
   fi
   
   umount /mnt > /dev/null 2>&1
   
   umount /mnt >/dev/null 2>&1
   
   # System und Boot partition mounten:
   if [ -f /mnt/boot ] ; then
      execCmd "umount /mnt/boot >/dev/null 2>&1"
      execCmd "umount /mnt >/dev/null 2>&1"
   fi
   execCmd "mount -t $FS_ROOT $SYSTEM_PART $HD_DEV$SYSTEM_PART_NUM /mnt"
   execCmd "mkdir /mnt/boot"
   
   if [ "$BOOT_PART_NUM" != "0" ] ; then
      execCmd "mount -t ext2 $HD_DEV$BOOT_PART_NUM /mnt/boot"
   fi
   
   DT=`date +%Y%m%d`
   cp -f $INSTALL_DIR/HISTORY /mnt/HISTORY.$DT
   cp -f $INSTALL_DIR/README /mnt/README.$DT
   
   
   # ins gemountete Verzeichnis wechseln:
   cd /mnt
   
   while [ ! -f $INSTALL_DIR/g2v_base_usr.tar.bz2 ] ; do
      umount $CD_DEVICE > /dev/null 2>&1
      eject $CD_DEVICE
      msgKey "Installations-CD wieder einlegen"
      if [ "$MP_AUTO_INST" = "1" ] ; then
         sleep 10
      fi
      mount -t iso9660 $CD_DEVICE $INSTALL_DIR 2>/dev/null
   done
   
   for i in $INSTALL_DIR/g2v_base*.tar.bz2 ; do
      msgKey "$i entpacken"
      execCmd "tar -C /mnt -xjvf $i"
   done
   
   # ein bischen warten ...
   
   if [ "$(ls $INSTALL_DIR/g2v_add*.tar.bz2 2>/dev/null)" != "" ] ; then 
      for i in $INSTALL_DIR/g2v_add*.tar.bz2 ; do
         msgKey "$i entpacken"
         execCmd "tar -C /mnt -xjvf $i"
      done
   fi
   
   #fstab anpassen
   #cp -f /mnt/_config/etc/$fs1 /mnt/etc/fstab
   #cat /mnt/_config/etc/$fs2 >> /mnt/etc/fstab
   if [ "$INST_OPT" != "RESTORE" ] ; then
      msgKey "fstab anpassen"
      rm -f /mnt/etc/fstab
      cat /mnt/_config/install/fstab | sed "\%^/dev/hda[0-9]%d" > /mnt/etc/fstab
      NLINE="${HD_DEV}${SYSTEM_PART_NUM}${TABC}/${TABC}${FS_ROOT}${TABC}noatime 1 1"
      echo "$NLINE" >> /mnt/etc/fstab
      NLINE="${HD_DEV}${VIDEO_PART_NUM}${TABC}/mnt/hda5${TABC}${FS_VIDEO}${TABC}noatime 1 2"
      echo "$NLINE" >> /mnt/etc/fstab
      NLINE="${HD_DEV}${SWAP_PART_NUM}${TABC}none${TABC}swap${TABC}sw 0 0"
      echo "$NLINE" >> /mnt/etc/fstab
      if [ "$BOOT_PART_NUM" != "0" ] ; then
         NLINE="${HD_DEV}${BOOT_PART_NUM}${TABC}/boot${TABC}ext2${TABC}noatime 1 2"
         echo "$NLINE" >> /mnt/etc/fstab
      fi
      cp -Rf /mnt/_config/install/vdr/* /mnt/etc/vdr/
   fi
   
   msgKey "cdrom links anpassen"
   #rm -f /mnt/dev/dvd
   #rm -f /mnt/dev/cdrom
   #cd /mnt/dev
   #execCmd "ln -s $CD_DEVICE /mnt/dev/cdrom"
   #execCmd "ln -s $CD_DEVICE /mnt/dev/dvd"
   
   DEF_DIRS="video audio pictures games film tmp"
   for i in $DEF_DIRS ; do
      rm -rf /mnt/${i} > /dev/null 2>&1
      ln -s /mnt/hda5/${i} /mnt/ >/dev/null 2>&1
   done
   
   rm -rf /mnt/tmpfs/* > /dev/null 2>&1
   
   mount -t $FS_VIDEO $HD_DEV$VIDEO_PART_NUM /mnt/mnt/hda5
   
   for i in $DEF_DIRS ; do
      mkdir /mnt/mnt/hda5/${i}
      rm /mnt/${i} >/dev/null 2>&1
      ln -s /mnt/hda5/${i} /mnt/ >/dev/null 2>&1
      rm /mnt/mnt/hda5/${i}/DISC >/dev/null 2>&1
      ln -s /mnt/cdrom /mnt/mnt/hda5/${i}/DISC >/dev/null 2>&1
      rm /mnt/mnt/hda5/${i}/USB >/dev/null 2>&1
      ln -s /mnt/usb /mnt/mnt/hda5/${i}/USB >/dev/null 2>&1
   done
   if [ -d ${INSTALL_DIR}/g2v_inst ] ; then 
      rm -rf /mnt/hda5/g2v_inst  >/dev/null 2>&1
      msgKey "Installationsdateien kopieren"     
      execCmd "cp -rf ${INSTALL_DIR}/g2v_inst/* /mnt/"
   fi
   if [ -d /mnt/mnt/hda5/g2v-backup ] && [ "$DO_BACKUP" != "1" ] ; then
      execCmd "rm -rf /mnt/mnt/hda5/g2v-backup/*"
   fi
   
   umount /mnt/mnt/hda5
   
   return 1
}




#
#
#
date >>$LOG_FILE 2>&1

if [ "$CD_DEVICE" = "" ] ; then
   export CD_DEVICE="/dev/hdc"
fi

if [ "$INSTALL_DIR" = "" ] ; then
   export INSTALL_DIR="/SETUP"
fi


if [ "$MP_AUTO_INST" != "1" ] ; then
   dialog --backtitle "VDR Installationsprogramm" --title "Jetzt gehts los" --menu \
   "Wie haettest Du es denn gerne ?" \
    9 50 2 \
   "AUTO"  "Vollautomatisch von vorne bis hinten"  \
   "INTER" "Interaktiv" 2> /tmp/menu.tmp

   case $? in
      0)
        ;;
      1)
        exit
        ;;
      255)
        exit
        ;;
   esac
   clear

   menu=`cat /tmp/menu.tmp`
   rm -f /tmp/menu.tmp

   if [ "$menu" = "AUTO" ]; then
      export MP_AUTO_INST=1
   else
      export MP_AUTO_INST=0
   fi
fi


if findDisks ; then
   echo "Installation bei der Platten/CD Erkennung abgebrochen"
   exit
fi

if doPart ; then
   echo "Installation bei Partitionierung abgebrochen"
   exit
fi

if doFormat ; then
   echo "Installation bei Formatierung abgebrochen"
   exit
fi

if doInstall ; then
   execCmd "umount /mnt >/dev/null 2>&1"
   echo "Installation beim Kopieren abgebrochen"
   exit
fi

if [ "$INST_OPT" != "RESTORE" ] ; then
   echo "sh /_config/install/init.sh $MP_AUTO_INST" > /mnt/autorun.vdr
fi

umount $CD_DEVICE
cp -f $LOG_FILE /mnt/ >/dev/null 2>&1

LILOCONF="/mnt/etc/lilo.conf"
mv -f $LILOCONF $LILOCONF.org

cat $LILOCONF.org | sed "s/^default = .*$/default = 2.6/" | \
                    sed "s%^root.*%root = $HD_DEV""2%" | \
                    sed "s%^boot.*%boot = $HD_DEV%" > $LILOCONF

ln -s /mnt/boot /

msgKey "Lilo ausfuehren"
execCmd "lilo -C $LILOCONF"

echo ""
echo ""
if [ "$INST_OPT" != "RESTORE" ] ; then
   echo "Installation erfolgreich beendet !"
   echo "Nach dem Reboot gehts weiter mit Stufe 2"
else
   echo "Restore erfolgreich beendet !"
fi
eject $CD_DEVICE
echo ""
echo "Das Passwort fuer den root-user lautet: <gen2vdr>"
msgKey "Das System wird nun neu gestartet - CD entfernen"
if [ "$MP_AUTO_INST" = "1" ] ; then
   sleep 10
fi
mount -t iso9660 $CD_DEVICE $INSTALL_DIR 2>/dev/null
while [ "$?" = "0" ] ; do
   umount $CD_DEVICE
   eject $CD_DEVICE
   msgKey "Das System wird nun neu gestartet - CD entfernen"
   if [ "$MP_AUTO_INST" = "1" ] ; then
      sleep 10
   fi
   mount -t iso9660 $CD_DEVICE $INSTALL_DIR 2>/dev/null
done
reboot
