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

execCmd() {
   echo "Exec <$1>" |tee -a /tmp/init.log
   $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
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
      rm -rf /tmp/fb.* > /dev/null 2>&1
      echo "dialog --backtitle 'VDR Installationsprogramm' --title 'Steuerung' \\" > /tmp/fb.tmp
      ALL_FB=""
      COUNT_FB=2
      ALL_CONF=$(ls /_config/install/lircd.conf.*| cut -f 3 -d ".")
      for i in $ALL_CONF ; do
         echo "\"$i\" \"\" \\" >>/tmp/fb.tm1
         COUNT_FB=$(($COUNT_FB + 1))
      done
      echo "\"Nexus\" \"Hauppauge Nexus FB\" \\" >>/tmp/fb.tm1
      echo "\"Other\" \"Eigene lircd.conf\" \\" >>/tmp/fb.tm1
      echo "  --menu '' $(($COUNT_FB+7)) 50 $COUNT_FB \\" >> /tmp/fb.tmp
      cat /tmp/fb.tm1 >> /tmp/fb.tmp
      echo " 2>/tmp/menu.tmp" >>/tmp/fb.tmp
      echo "if [ "$?" != "0" ] ; then" >>/tmp/fb.tmp
      echo "   rm -f /tmp/menu.tmp" >>/tmp/fb.tmp
      echo "fi" >>/tmp/fb.tmp
      rm -f /tmp/menu.tmp
      bash /tmp/fb.tmp
  
      MENU=`cat /tmp/menu.tmp`
      rm -f /tmp/menu.tmp
   fi
   $SETVAL LIRC $MENU
fi
return 1
}


export INST_DIR=""
export MP_AUTO_INST=$1

killall -9 vdr runvdr > /dev/null 2>&1 

# Stop running processes
SVC_STOP="vdr alsasound lircd freevo net.eth0"
for i in $SVC_STOP ; do
  /etc/init.d/${i} stop
done


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

# Initialize Konfiguration
execCmd "sh /_config/bin/default_cfg.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_dvb.sh"
execCmd "sh /_config/bin/detect_net.sh"

#set -x
if [ -s /mnt/hda5/g2v-backup/etc/conf.d/vdr ] ; then
   # Migrate old config
   execCmd "sh /_config/bin/g2v_migrate.sh"
else
   if instRec; then
      exit 1
   fi
   if instCtrl; then
      exit 1
   fi
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/mysql >/dev/null 2>&1
mkdir /var/log/freevo >/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

VDRUSER="root"

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

echo "Einstellungen konfigurieren ..."
rm /etc/vdr/plugins/admin/admin.conf.save
sh /etc/vdr/plugins/admin/admin_changes.sh -f

ln -s everything/current /var/log/messages >/dev/null 2>&1

rm -f /_config/status/* > /dev/null 2>&1

echo "System initialisieren ..."

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

