#!/bin/sh
source /etc/conf.d/vdr.admin.cfg

execCmd() {
   echo "Exec <$1>" >> /tmp/init.log
   $1
}

instNet() {

execCmd "sh /_config/bin/detect_net.sh"
if [ "$MP_AUTO_INST" != "1" ] ; then
   dialog --backtitle "VDR Installationsprogramm" --title "Netzwerk" --yesno \
   "Soll das Netzwerk aktiviert werden ?" 5 43
   case $? in
      0)
        $SETVAL NET 1
#        msgKey "Nach dem Reboot mit /_config/install/net_cfg.sh konfigurieren !"
        ;;
      1)
        $SETVAL NET 0 
        ;;
      255)
        return 0
   esac
fi
execCmd "sh /etc/vdr/plugins/admin/cfgnet.sh"
return 1
}


instRec() {
if [ "$MP_AUTO_INST" != "1" ] ; then
   dialog --backtitle "VDR Installationsprogramm" --title "Empfangs-Typ" --menu \
   "Waehle das Empfangssystem" 10 32 3 \
   "SAT" "" \
   "Kabel" "" \
   "DVB-T" ""  2> /tmp/menu.tmp

   case $? in
      0)
        ;;
      1)
        return 0
        ;;
      255)
        return 0
        ;;
   esac
   clear
   MENU=`cat /tmp/menu.tmp`
   $SETVAL RCV $MENU
   rm -f /tmp/menu.tmp
fi
execCmd "sh /etc/vdr/plugins/admin/cfgrcv.sh"
return 1
}


instCtrl() {
if [ "$MP_AUTO_INST" != "1" ] ; then
   dialog --backtitle "VDR Installationsprogramm" --title "Steuerung" --menu \
   "Waehle die VDR-Bedienung" 10 67 3 \
   "Tastatur" "Steuerung per Tastatur oder original MP-FB" \
   "Nexus" "Fernbedienung der Nexus DVB Karte" \
   "LIRC" "Steuerung per LIRC-Fernbedienung" 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" = "LIRC" ]; then
      dialog --backtitle "VDR Installationsprogramm" --title "Steuerung" --menu \
      "Waehle die Fernbedienung" 17 67 8 \
      "ALDI4869-336" "" \
      "ALDI6461-327" "" \
      "ALDI6461-376" "" \
      "ALDI4688-065-260" "" \
      "Kingman-120" "" \
      "Kingman-175" "" \
      "Kingman-336" "" \
      "LIDL-001" "" \
      "Nokia-MB" "" \
      "RR801-HA" "" \
      "Other" "Irgend was anderes" 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
   fi
   $SETVAL LIRC $MENU
fi
execCmd "sh /etc/vdr/plugins/admin/cfgctrl.sh"
return 1
}


export INST_DIR=""
export MP_AUTO_INST=$1

touch /_config/status/~booting > /dev/null 2>&1
killall -9 vdr runvdr > /dev/null 2>&1 

DEF_DIRS="video audio pictures games film tmp"
for i in $DEF_DIRS ; do
   mkdir /mnt/hda5/${i}
   rm /${i} >/dev/null 2>&1
   ln -s /mnt/hda5/${i} / >/dev/null 2>&1
   rm /mnt/hda5/${i}/DISC >/dev/null 2>&1
   ln -s /mnt/cdrom /mnt/hda5/${i}/DISC >/dev/null 2>&1
   rm /mnt/hda5/${i}/USB >/dev/null 2>&1
   ln -s /mnt/usb /mnt/hda5/${i}/USB >/dev/null 2>&1
done

mkdir /audio/images
cp -r /etc/vdr/plugins/mp3/* /audio/images/

rm -f /autorun* > /dev/null 2>&1

sh /_config/bin/g2v_migrate.sh

source /etc/conf.d/vdr

cp -f /_config/install/vdr/setup.conf /etc/vdr/ >/dev/null 2>&1

#execCmd "sh /_config/bin/detect_modules.sh"
execCmd "sh /_config/bin/detect_cpu.sh"
execCmd "sh /_config/bin/detect_board.sh"

if instNet; then
   exit 1
fi
if instRec; then
   exit 1
fi
if instCtrl; then
   exit 1
fi

#cd /usr/local/src
#rm VDR
#execCmd "ln -s vdr-1.2.6 VDR"
#echo "sh /_config/bin/makedvb.sh" > /autorun.vdr
mkdir /var/log >/dev/null 2>&1

mkdir /mnt/hda5/audio/images >/dev/null 2>&1
cp -f /etc/vdr/plugins/mp3/* /mnt/hda5/audio/images/ >/dev/null 2>&1

#source /etc/conf.d/vdr
#if [ "$VDRUSER" = "" ] ; then
   VDRUSER="root"
#fi   

mkdir /mnt/hda5/games/crafty >/dev/null 2>&1
touch /mnt/hda5/games/crafty/crafty.crafty >/dev/null 2>&1
chown -R ${VDRUSER}:users /mnt/hda5
chmod -R 775 /mnt/hda5
chown -R ${VDRUSER} /etc/vdr

echo "Einstellungen konfigurieren ..."
rm -f /etc/vdr/plugins/admin/admin.conf.save > /dev/null 2>&1
sh /etc/vdr/plugins/admin/admin.sh
sh /etc/vdr/plugins/admin/~admexec
ln -s /var/log/everything/current /var/log/messages >/dev/null 2>&1

echo "System initialisieren ..."
rm -f /_config/status/* > /dev/null 2>&1
rm -f /_config/bin/insmod.sh > /dev/null 2>&1

if [ -d /mnt/hda5/g2v_install ] && [ ! -d "/mnt/hda5/g2v-backup" ] ; then 
   echo "Kopiere Default-Dateien"
   cp -rf * /mnt/hda5/g2v_install/*
   if [ -f /mnt/hda5/etc/conf.d/vdr ] ; then 
      sh /etc/vdr/plugins/admin/setadmin.sh
   fi
fi


depscan.sh >/dev/null 2>&1
ldconfig
depmod -a
modules-update
lilo
sync&&sync

echo "Weitere Anpassungen ueber VDR-Menu->Einstellungen->Plugins->Admin"
echo "Das Menu wird mit der Tab-Taste aufgerufen."
echo ""
echo "Das Passwort fuer den root-user lautet: <gen2vdr>"
echo "Das System wird nun neu gestartet"

reboot

exit 0

