#!/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=1000

# Standard Partitionsgroesse
BOOT_PART_DEFSIZE=25
SYSTEM_PART_DEFSIZE=6000
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=""

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

INST_DISK=""
ROOT_DISK=""

ALL_PARTS=""
AUTO_PART_DONE=0

#fuer die Optik
NUM_PACKAGES=0
ACT_PACKAGE=0
CHOOSEN_PARTS=" "
PARTITION_VALID=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"
   echo "<$1>" >> $LOG_FILE
   if [ "$MP_AUTO_INST" != "1" ] ; then
      read xx
   else
      sleep 2
   fi
}

msgInfo() {
   echo ""
   echo -e "\007"
   echo -e "\033[44;37m$1 ...\033[0m"
   echo "<$1>" >> $LOG_FILE
   sleep 2
}


execCmd() {
   if [ "$2" = "" ] ; then
      echo "Exec <$1>" | tee -a $LOG_FILE
      $1 2>&1 | tee -a $LOG_FILE
   else
      echo "Exec <$1 | $2>" | tee -a $LOG_FILE
      $1 | $2 2>&1 | tee -a $LOG_FILE
   fi      
   EXEC_RC=$?
   [ "$EXEC_RC" != "0" ] && echo "RC <$EXEC_RC>" | tee -a $LOG_FILE
   sleep 1
}


unpackArchive() {
   ACT_PACKAGE=$(($ACT_PACKAGE+1))
   msgInfo "$1 entpacken (Paket $ACT_PACKAGE von $NUM_PACKAGES)"
   BN="$(basename $1)"
   lzma d $1 -so | tar -C /mnt -xvf - >> /mnt/mnt/data/$BN.log 2>&1
   EXEC_RC=$?
   [ "$EXEC_RC" != "0" ] && echo "RC <$EXEC_RC>" | tee -a $LOG_FILE
   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
   
   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"
   
   msgInfo "Partitionen wurden angelegt"
   parted --script $HD_DEV print > $PARTED_OUT
   
   export AUTO_PART_DONE=1
}


checkPart() {
   partNum=$1; partSize=$2; partName=$3
   if [ $1 -eq 0 ] ; then
      msgKey "$partName - Ungueltige PartNum $partNum"
      return 0
   fi
   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`
      fstype=`echo $field | cut -d ' ' -f5`
      psize=`expr $end - $start`
      echo "$partName: $HD_DEV$partNum: $psize MB - $fstype"
      if [ $partSize -gt $psize ]; then
         msgKey  "$partName $HD_DEV$partNum zu klein - Minimum $partSize MB"
         cpRc=0
      fi
   else
      msgKey "$partName $HD_DEV$partNum nicht vorhanden"
      cpRc=0
   fi
   
   return $cpRc
}

choosePartition() {
   rm -rf /tmp/ip.* > /dev/null 2>&1
   NP=0
   CPN=0
   echo "dialog --backtitle \"VDR Installationsprogramm\" --title \"$1-Partition waehlen\" --menu \\" > /tmp/ip.tmp
   if [ "$1" = "Boot" ] ; then
      echo "\"Boot und SystemPartition koennen dieselbe sein" >> /tmp/ip.tmp
   else
      echo "\"" >> /tmp/ip.tmp
   fi
   [ -f /tmp/ip.tmp1 ] && rm /tmp/ip.tmp1
   for i in $ALL_PARTS ; do
      PN=$(echo $i | cut -f 1 -d "_")
      PS=$(echo $i | cut -f 2 -d "_")
      if [ $PS -ge $2 ] ; then
         if [ "$1" = "Boot" ] && [ $PN -ne $SYSTEM_PART_NUM ] && [ $PN -ne 1 ] ; then
            continue;
         fi
         if [ "$1" != "Boot" ] && [ "$(echo " $CHOOSEN_PARTS " | grep " $PN ")" != "" ] ; then
            continue
         fi
         CPN=$PN
         NP=$(($NP + 1))
         printf "\"${INST_DISK}${PN}\" \"%8d MB\" " $PS >> /tmp/ip.tmp1
      fi
   done
   [ $NP -lt 1 ] && return $CPN
   echo "Mindestgroesse fuer die $1-Partition: $2 MB\" $(($NP + 8)) 58 $NP \\" >> /tmp/ip.tmp
   cat /tmp/ip.tmp1 >> /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

   clear
   rm -f /tmp/menu.tmp
   bash /tmp/ip.tmp
    
   if [ -s /tmp/menu.tmp ] ; then
      PN=$(cat /tmp/menu.tmp | sed -e "s/$INST_DISK//")
      CHOOSEN_PARTS="${CHOOSEN_PARTS} ${PN}"
      return $PN
   fi
   return 0
}

getAllParts() {
   AP=$(cat $PARTED_OUT | grep "^[1-9]" | grep -v " extended " | cut -f 1 -d " ")
   ALL_PARTS=""
   for i in $AP ; do
      field=$(grep "^$i " $PARTED_OUT | tr -s " ")
      start=$(echo $field | cut -d ' ' -f2 | cut -d '.' -f1)
      if [ $start ]; then
         end=$(echo $field | cut -d ' ' -f3 | cut -d '.' -f1)
         size=$(($end - $start))
         ALL_PARTS="$ALL_PARTS ${i}_${size}"
      fi
   done 
   CHOOSEN_PARTS=" "
   choosePartition "System" $SYSTEM_PART_MINSIZE
   SYSTEM_PART_NUM=$?
   choosePartition "Boot" $BOOT_PART_MINSIZE
   BOOT_PART_NUM=$?
   choosePartition "Swap" $SWAP_PART_MINSIZE
   SWAP_PART_NUM=$?
   choosePartition "Video" $VIDEO_PART_MINSIZE
   VIDEO_PART_NUM=$?
   PARTITION_VALID=0
   if checkAllParts ; then
      dialog --defaultno --yesno "Partitionierung ungueltig - Abbrechen ?" 5 45
      return $?
   else
      PARTITION_VALID=1
      MENU_TEXT=""
      for i in $ALL_PARTS ; do
         PN=$(echo $i | cut -f 1 -d "_")
         PS=$(echo $i | cut -f 2 -d "_")
         if [ $PN -eq $BOOT_PART_NUM ] ; then
            if [ $PN -eq $SYSTEM_PART_NUM ] ; then
               PNAME="- Boot + System"
            else
               PNAME="- Boot"
            fi
         elif [ $PN -eq $SYSTEM_PART_NUM ] ; then
            PNAME="- System"
         elif [ $PN -eq $SWAP_PART_NUM ] ; then
            PNAME="- Swap"
         elif [ $PN -eq $VIDEO_PART_NUM ] ; then
            PNAME="- Video"
         else
            PNAME=""
         fi
         MENU_TEXT="$MENU_TEXT\\n${INST_DISK}${PN} $(printf %8d $PS) $PNAME"
      done
      dialog --title "   Partitionierung uebernehmen   " --yesno "$MENU_TEXT" 0 0
      return $?
   fi
   #echo "$SYS - $BOOT - $VID - $SWAP"
   #echo "$ALL_PARTS"
}

checkAllParts() {
   clear
   capRc=1
   
   #partNum=1; partSize=10; partSWP=0
   if checkPart $BOOT_PART_NUM $BOOT_PART_MINSIZE "Boot" ; then
      capRc=0
   fi
   
   #partNum=2; partSize=1500; partSWP=0
   if checkPart $SYSTEM_PART_NUM $SYSTEM_PART_MINSIZE "System" ; then
      capRc=0
   fi
   
   #partNum=3; partSize=120; partSWP=1
   if checkPart $SWAP_PART_NUM $SWAP_PART_MINSIZE "Swap" ; then
      capRc=0
   fi
   
   #partNum=5; partSize=100; partSWP=0
   if checkPart $VIDEO_PART_NUM $VIDEO_PART_MINSIZE "Video" ; 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" | sed -e "s/.* disk //"`
   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
   ROOT_DISK=$INST_DISK
   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
#   DO_BACKUP=0
   HD_DEV="/dev/$INST_DISK"
   return 1
}


doPart() {
   parted --script $HD_DEV print > $PARTED_OUT
   if [ "$MP_AUTO_INST" = "1" ] ; then
      if checkAllParts ; then
         autoPart
         if checkAllParts ; 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 >= $BOOT_PART_MINSIZE MB ( Default: $BOOT_PART_DEFSIZE )
 2: primaer /      Type83 >= $SYSTEM_PART_MINSIZE MB ( $SYSTEM_PART_DEFSIZE )
 3: primaer /swap  Type82 >= $SWAP_PART_MINSIZE MB ( $SWAP_PART_DEFSIZE )
 5: logisch /video Type83 >= $VIDEO_PART_MINSIZE MB ( Restliche Platte )" \
       19 68 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
      elif [ "$menu" = "CFDSK" ]; then
         cfdisk $HD_DEV
      elif [ "$menu" = "EXIT" ]; then
         return 0
      fi
      parted --script $HD_DEV print > $PARTED_OUT
      
      if getAllParts ; then
         return $PARTITION_VALID
      fi
   done
}


doFormat() {
   if [ "$AUTO_PART_DONE" = "0" ] && [ "$DO_BACKUP" = "1" ] ; then
      execCmd "mount $HD_DEV$SYSTEM_PART_NUM /mnt"
      if [ -d /mnt/etc ] ; then
         mkdir /bkp
         execCmd "mount $HD_DEV$VIDEO_PART_NUM /bkp"
         if [ -d /bkp/video ] ; then
            if [ "$MP_AUTO_INST" != "1" ] ; then
               dialog --yesno "Soll die alte Konfiguration gesichert werden ?" 5 55
               if [ $? != 0 ] ; then
                  DO_BACKUP=0
               fi
            fi
            if [ "$DO_BACKUP" = "1" ] ; then
               if [ -d /bkp/g2v-backup ] ; then
                  execCmd "rm -rf /bkp/g2v-backup/*"
               else
                  mkdir /bkp/g2v-backup
               fi
               msgInfo "Sichere /etc /_config /home und /root nach /g2v-backup auf $HD_DEV$VIDEO_PART_NUM"
           
               if [ ! -d /bkp/g2v-backup/etc ] ; then
                  execCmd "cp -af /mnt/etc /bkp/g2v-backup"
               fi
               if [ ! -d /bkp/g2v-backup/_config ] ; then
                  execCmd "cp -af /mnt/_config /bkp/g2v-backup"
               fi
               if [ ! -d /bkp/g2v-backup/home ] ; then
                  execCmd "cp -af /mnt/home /bkp/g2v-backup"
               fi
               if [ ! -d /bkp/g2v-backup/root ] ; then
                  execCmd "cp -af /mnt/root /bkp/g2v-backup"
               fi
            fi
         fi
         execCmd "umount $HD_DEV$VIDEO_PART_NUM"
      fi   
      execCmd "umount $HD_DEV$SYSTEM_PART_NUM"
   fi
   
   
   if [ "$BOOT_PART_NUM" != "$SYSTEM_PART_NUM" ] ; then
      msgInfo "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'
      elif [ "$menu" = "XFS" ]; then
         FS_ROOT='xfs'
      elif [ "$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"
   elif [ "$FS_ROOT" = "xfs" ]; then
      execCmd "mkfs.xfs -f $HD_DEV$SYSTEM_PART_NUM"
   elif [ "$FS_ROOT" = "reiserfs" ]; then
      echo "mkreiserfs -f $HD_DEV$SYSTEM_PART_NUM"
      echo y |mkreiserfs -f $HD_DEV$SYSTEM_PART_NUM
   fi
   
   sleep 3
   
   msgInfo "Ernenne $HD_DEV$SWAP_PART_NUM zur Swap-Partition"
   execCmd "mkswap $HD_DEV$SWAP_PART_NUM"
   sleep 3
   
   execCmd "mount $HD_DEV$VIDEO_PART_NUM /mnt"
   FS_VIDEO=`mount | grep "$HD_DEV$VIDEO_PART_NUM" | cut -f 5 -d " "`   
   umount /mnt >/dev/null 2>&1
   if [ "$MP_AUTO_INST" != "1" ] ; then
      if [ "$FS_VIDEO" != "" ] ; then
         KEEP_FS1='KEEP'
         KEEP_FS2='Belassen'
      else
         KEEP_FS1=''
         KEEP_FS2=''
      fi
      clear
      dialog --backtitle "VDR Installationsprogramm" --title "Video-Partition formatieren" --menu \
      "Waehle das Datei-System fuer die Video-Partition" \
      11 65 4 \
      $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" = "EXT3" ]; then
         FS_VIDEO='ext3'
      elif [ "$menu" = "XFS" ]; then
         FS_VIDEO='xfs'
      elif [ "$menu" = "REISERFS" ]; then
         FS_VIDEO='reiserfs'
      else
         return 1
      fi
   else
      if [ "$FS_VIDEO" != "" ] ; then
         return 1
      fi
      FS_VIDEO=$FS_VIDEO_DEFAULT
   fi
   echo "Formatiere Video-Partition $HD_DEV$VIDEO_PART_NUM mit $FS_VIDEO ..."
  
   if [ "$FS_VIDEO" = "ext3" ]; then
      execCmd "mke2fs -j $HD_DEV$VIDEO_PART_NUM"
      execCmd "tune2fs -i 0 -c 0 $HD_DEV$VIDEO_PART_NUM"
   elif [ "$FS_VIDEO" = "xfs" ]; then
      execCmd "mkfs.xfs -f $HD_DEV$VIDEO_PART_NUM"
   elif [ "$FS_VIDEO" = "reiserfs" ]; then
      echo "mkreiserfs -f $HD_DEV$VIDEO_PART_NUM"
      echo y |mkreiserfs -f $HD_DEV$VIDEO_PART_NUM
   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"
   execCmd "mkdir /mnt/mnt"
   execCmd "mkdir /mnt/mnt/data"   
   
   if [ "$BOOT_PART_NUM" != "$SYSTEM_PART_NUM" ] ; then
      execCmd "mount -t ext2 $HD_DEV$BOOT_PART_NUM /mnt/boot"
   fi
   execCmd "mount -t $FS_VIDEO $HD_DEV$VIDEO_PART_NUM /mnt/mnt/data"
   
   DT=`date +%Y%m%d`
   cp -f $INSTALL_DIR/HISTORY /mnt/HISTORY.$DT
   cp -f $INSTALL_DIR/README /mnt/README.$DT
   
   MM_DIRS="video audio pictures film games"
   SYS_DIRS="tmp log"
   DEF_DIRS="$MM_DIRS $SYS_DIRS"
   
   rm -rf /mnt/tmpfs/* > /dev/null 2>&1
   mkdir /mnt/usr
   
   for i in $DEF_DIRS ; do
      mkdir /mnt/mnt/data/${i}
      rm /mnt/${i} >/dev/null 2>&1
      ln -s mnt/data/${i} /mnt/${i} >/dev/null 2>&1
   done
   for i in $MM_DIRS ; do
      rm -f /mnt/mnt/data/${i}/DISC >/dev/null 2>&1
      [ "$i" != "video" ] && ln -s /mnt/cdrom /mnt/mnt/data/${i}/DISC >/dev/null 2>&1
      rm -f /mnt/mnt/data/${i}/USB >/dev/null 2>&1
      rm -f /mnt/mnt/data/${i}/MEDIA >/dev/null 2>&1
      [ "$i" != "video" ] && ln -s /media /mnt/mnt/data/${i}/MEDIA >/dev/null 2>&1
   done
   [ -f /mnt/usr/portage ] && rm -f /mnt/usr/portage
   ln -s ../mnt/data/portage /mnt/usr/
   mkdir /mnt/mnt/data/portage > /dev/null 2>&1
   mkdir /mnt/audio/images > /dev/null 2>&1
   mkdir /mnt/audio/covers > /dev/null 2>&1
   mkdir /mnt/video/epgimages > /dev/null 2>&1
   mkdir /mnt/var > /dev/null 2>&1
   rm -rf /mnt/mnt/data/log/* > /dev/null 2>&1
   rm -rf /mnt/mnt/data/tmp/* > /dev/null 2>&1
   ln -s ../log /mnt/var/
   ln -s ../tmp /mnt/var/
   ln -s everything/current /mnt/mnt/data/log/messages
   touch /mnt/mnt/data/log/lastlog
   mkdir /mnt/mnt/data/log/mysql > /dev/null 2>&1
   mkdir /mnt/mnt/data/log/freevo >/dev/null 2>&1
   mkdir /mnt/proc >/dev/null 2>&1
   
   cp -a /dev /mnt/
   # ins gemountete Verzeichnis wechseln:
   cd /mnt
   
   while [ ! -f $INSTALL_DIR/g2v_base_usr.tar.lzma ] ; do
      umount $CD_DEVICE > /dev/null 2>&1
      eject $CD_DEVICE
      msgInfo "Installations-CD wieder einlegen"
      if [ "$MP_AUTO_INST" = "1" ] ; then
         sleep 10
      fi
      mount -t iso9660 $CD_DEVICE $INSTALL_DIR 2>/dev/null
   done
   
   NUM_PACKAGES=$(($(ls $INSTALL_DIR/g2v_*.tar.lzma | wc -w)))
   for i in $INSTALL_DIR/g2v_base*.tar.lzma ; do
      unpackArchive $i
   done
   
   # ein bischen warten ...
   
   if [ "$(ls $INSTALL_DIR/g2v_add*.tar.lzma 2>/dev/null)" != "" ] ; then 
      for i in $INSTALL_DIR/g2v_add*.tar.lzma ; do
         unpackArchive $i
      done
   fi

   # ein bischen warten ...
   
   if [ "$(ls $INSTALL_DIR/g2v_opt*.tar.lzma 2>/dev/null)" != "" ] ; then 
      if [ ! -d /mnt/mnt/data ] ; then
         mkdir /mnt/mnt/data
      fi 

      for i in $INSTALL_DIR/g2v_opt*.tar.lzma ; do
         unpackArchive $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
      msgInfo "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}   /   ${FS_ROOT}   noatime 1 1"
      echo "$NLINE" >> /mnt/etc/fstab
      NLINE="${HD_DEV}${VIDEO_PART_NUM}   /mnt/data   ${FS_VIDEO}   noatime 1 2"
      echo "$NLINE" >> /mnt/etc/fstab
      NLINE="${HD_DEV}${SWAP_PART_NUM}   none   swap   sw 0 0"
      echo "$NLINE" >> /mnt/etc/fstab
      if [ "$BOOT_PART_NUM" != "$SYSTEM_PART_NUM" ] ; then
         NLINE="${HD_DEV}${BOOT_PART_NUM}   /boot   ext2   noatime 1 2"
         echo "$NLINE" >> /mnt/etc/fstab
      fi
      cp -af /mnt/_config/install/vdr/* /mnt/etc/vdr/
   fi
   
   
   if [ -d ${INSTALL_DIR}/g2v_inst ] ; then 
      msgInfo "Installationsdateien kopieren"     
      execCmd "cp -af ${INSTALL_DIR}/g2v_inst/* /mnt/"
      cd /mnt
      echo "#!/bin/sh" >  ~extract.sh
      
      for i in *.tar.bz2 ; do
         if [ -f $i ] ; then
            echo "echo \"Entpacke <$i>\"" >> ~extract.sh
            echo "tar -C / -xjf $i" >> ~extract.sh
	 fi    
      done
      
      # updates entpacken
      MD5_FILE="/mnt/_config/update/g2v_md5_fixes"
      if [ -f $MD5_FILE ] ; then
         for i in $(grep "\.tar\.bz2" $MD5_FILE | cut -f 3 -d " ") ; do
            echo "echo \"Entpacke <$i>\"" >> ~extract.sh
            echo "tar -C / -xjf /_config/update/$i" >> ~extract.sh
         done
      fi         
      chmod +x ~extract.sh
      chroot /mnt /~extract.sh
      cat /mnt/~extract.sh >> $LOG_FILE
      rm -f /mnt/*.tar.bz2 /mnt/~extract.sh > /dev/null 2>&1
   fi
   if [ -d /mnt/mnt/data/g2v-backup ] && [ "$DO_BACKUP" != "1" ] ; then
      execCmd "rm -rf /mnt/mnt/data/g2v-backup/*"
   fi
   
   umount /mnt/mnt/data
   
   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 | tee -a $LOG_FILE 2>&1"  > /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
LPJ=$(dmesg | grep "lpj=" | sed -e "s/.*lpj=//" -e "s/)//")
if [ "$LPJ" != "" ] ; then
   msgInfo "Setting LPJ to $LPJ"
   if [ "$(grep "lpj=" $LILOCONF.org)" = "" ] ; then
      sed -i $LILOCONF.org -e "s/video=/lpj=$LPJ video=/"
   else
      sed -i $LILOCONF.org -e "s/lpj=[0-9]*/lpj=$LPJ/"         
   fi
else
   msgInfo "Resetting LPJ"
   sed -i $LILOCONF.org -e "s/lpj=[0-9]*//"               
fi


cat $LILOCONF.org | sed "s/^default = .*$/default = 2.6/" | \
                    sed "s%^root.*/dev/[hs]d.*%root = ${HD_DEV}${SYSTEM_PART_NUM}%" | \
                    sed "s%^boot.*%boot = /dev/$ROOT_DISK%" > $LILOCONF

ln -s /mnt/boot /
ln -s /mnt/_config /

msgInfo "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
export CDROM=$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
cdctl -c
sleep 2
NO_CD=$(cdctl -g | grep "There is no") 

#echo "mount -t iso9660 $CD_DEVICE $INSTALL_DIR"  >> $LOG_FILE
#mount -t iso9660 $CD_DEVICE $INSTALL_DIR 2>/dev/null
if [ "$NO_CD" = "" ] ; then
   eject $CD_DEVICE
   msgKey "Das System wird nun neu gestartet - CD entfernen"
   if [ "$MP_AUTO_INST" = "1" ] ; then
      sleep 10
   fi
   cdctl -c
   sleep 2
   NO_CD=$(cdctl -g| grep "There is no") 
fi
cp -f $LOG_FILE /mnt/ >/dev/null 2>&1
if [ "$NO_CD" = "" ] ; then
   eject $CD_DEVICE
   msgInfo "Bitte CD entfernen und neustarten oder Enter"
   read xx
fi
echo "Rebooting" >> /mnt/$LOG_FILE
reboot
