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

BOARD="Unknown"
BI="/tmp/~biosinfo"
SETVAL="sh /etc/vdr/plugins/admin/setadmval.sh"
KV="$(uname -r)"
KV_MAJOR=`uname -r | cut -f1 -d "."`
KV_MINOR=`uname -r | cut -f2 -d "."`

MODCONF="/etc/modules.autoload.d/kernel-${KV_MAJOR}.${KV_MINOR}"

set_xorg() {
   SCR_SET="    Screen "
   sed -i /etc/X11/xorg.conf -e "s/$SCR_SET.*/$SCR_SET \"$1\"/"
}

set_freevo() {
   sed -i /etc/freevo/freevo.conf -e "s/^geometry.*/geometry = $1/"
}

set_alsa() {
   ALSA_MOD="/etc/modules.d/alsa"
   sed -i $ALSA_MOD -e "/^alias snd-card-0.*/d"
   echo "" >> $ALSA_MOD
   echo "alias snd-card-0 $1" >> $ALSA_MOD
   echo "Updating modules ..."
   modules-update
   autoload $1
   sh /etc/vdr/plugins/admin/cfgalsa.sh 1
}

autoload() {
   LOADED=`cat $MODCONF | grep "^$*$"`
   if [ "$LOADED" = "" ] ; then
      echo "$*" >> $MODCONF
      echo "Adding $* to $MODCONF"
   else
      echo "$* is already in $MODCONF"
   fi
}

activate_plugin() {
   $SETVAL "PLG_$1" $2
}

test_directisa() {
   TEST_DATE="020200002002"
   hwclock --hctosys
   ACT_DATE=$(date +%m%d%H%M%Y.%S)
   date $TEST_DATE
   hwclock --systohc
   date $ACT_DATE
   hwclock --hctosys
   TD=$(date +%m%d%H%M%Y)
   date $ACT_DATE
   if [ "$TD" = "$TEST_DATE" ] ; then
      echo "RTC is working"
      DI=0
      hwclock --systohc
   else
      echo "directisa is necessary"   
      DI=1
   fi   
   $SETVAL "DIRECTISA" $DI
}

biosinfo > $BI
#IS_QDI=`cat $BI | grep -i i810-W83627HF`
#IS_QDI=1
if [ "$IS_QDI" = "" ] ; then
   IS_QDI=`cat $BI | grep -i "QDI Winnex9"`
fi
#set -x
if [ "$IS_QDI" = "" ] ; then
   IS_QDI=`lspci | grep "Multimedia audio controller: Ensoniq ES1371"`
   if [ "$IS_QDI" != "" ] ; then
      IS_QDI=`lspci | grep "PCI bridge: Intel Corp. 82801AA PCI Bridge"`
   fi
fi

rm -f /_config/bin/rvdrinit.sh

test_directisa
set_freevo "800x600"
sed -i $MODCONF -e "/^natsemi/d"

if [ "$IS_QDI" != "" ] ; then
   echo "MP QDI detected"
   BOARD="MP_QDI"
   set_alsa "snd-ens1371"
   sh /etc/vdr/plugins/admin/cfgacpi.sh MP_QDI
   $SETVAL WAKEUP_METHOD NVRAM
   $SETVAL NVRAM_BOARD MP_QDI
   echo "sh /_config/bin/mpkeys.sh" > /_config/bin/rvdrinit.sh
   chmod +x /_config/bin/rvdrinit.sh
   #$SETVAL "GUI" "FreeVo"
   set_xorg "ScrMP"
else
   IS_AVT=`cat $BI | grep -i AVT`
   if [ "$IS_AVT" = "" ] ; then
      IS_AVT=`cat $BI | grep -i "\"07/15/97"`
   fi
   if [ "$IS_AVT" = "" ] ; then
      IS_AVT=`cat $BI | grep -i "Instant810C"`
   fi
   if [ "$IS_AVT" != "" ] ; then
      echo "MP AVT detected"
      BOARD="MP_AVT"
      set_alsa "snd-es1938"
      IS_AVT121=`cat $BI | grep -i "1.21"`
      if [ "$IS_AVT121" = "" ] ; then
         IS_AVT121=`cat $BI | grep -i "ZIDA"`
      fi

      $SETVAL WAKEUP_METHOD NVRAM
      if [ "$IS_AVT121" = "" ] ; then
         $SETVAL NVRAM_BOARD MP_AVT122
      else
         $SETVAL NVRAM_BOARD MP_AVT121
      fi
      sh /etc/vdr/plugins/admin/cfgacpi.sh MP_AVT
      echo "sh /_config/bin/mpkeys.sh" > /_config/bin/rvdrinit.sh
      chmod +x /_config/bin/rvdrinit.sh
      #$SETVAL "GUI" "FreeVo"
      set_xorg "ScrMP"
   else
      IS_EPIAM=`cat $BI | grep -i "epia m"`
      if [ "$IS_EPIAM" = "" ] ; then
         IS_EPIAM=`cat $BI | grep -i "epia-m"`
         if [ "$IS_EPIAM" = "" ] ; then
            IS_EPIAM=`cat $BI | grep -i "c3m266"`
         fi
      fi
      if [ "$IS_EPIAM" != "" ] ; then
         echo "EPIA-M detected"
         BOARD="EPIA_M"
         set_alsa "snd-via82xx"
         $SETVAL WAKEUP_METHOD NVRAM
         sh /etc/vdr/plugins/admin/cfgacpi.sh EPIA_M
         set_xorg "ScrVia"
      else
         IS_ACTIVY=`cat $BI | grep -i "D1231"`
         if [ "$IS_ACTIVY" != "" ] ; then
            echo "ACTIVY detected"
            BOARD="ACTIVY"
            $SETVAL WAKEUP_METHOD ACPI
            sh /etc/vdr/plugins/admin/cfgacpi.sh ACTIVY
            echo "sh /_config/bin/activyrc2.sh" > /_config/bin/rvdrinit.sh
	           chmod +x /_config/bin/rvdrinit.sh
            cp /_config/install/vdr/remote.conf.kbd /_config/install/vdr/remote.conf.kbd.org
            cp /_config/install/vdr/remote.conf.activy /_config/install/vdr/remote.conf.kbd
            sh /etc/vdr/plugins/admin/cfgctrl.sh KBD
            autoload "i2c-dev"
            autoload "i2c-i801"
            autoload "natsemi"
	           modprobe natsemi
            set_alsa "snd-intel8x0"
            set_xorg "ScrActivy"
            set_freevo "720x576"
            activate_plugin "alcd" "01"
#            $SETVAL "GUI" "FreeVo"
         else
            echo "Unknown Board"
	           set_xorg "ScrVga"
         fi
      fi
   fi
fi
echo "$BOARD" > /_config/.board
