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

REFRESH_RATE=5
#CD_DEV=`ls -al /dev/cdrom | awk '{print $NF}'`
CD_DEV="/dev/cdrom"
OLD_CD_TYPE=99
SLOW_DOWN_CMD="/usr/bin/speedcontrol -x 16 /dev/hdc;hdparm -E 10 /dev/hdc"
#SLOW_DOWN_CMD="hdparm -E 10 /dev/hdc"
#SLOW_DOWN_CMD=""

version="0.0.0 (alpha)"
publishyear="2003"
author="Helmut Auer"

banner() {
    echo >&2  \
	"CD-Automounter $version - Copyright (C) $publishyear $author"
}

audio_start() {
   if [ "$AUTORUN" = "1" ] ; then
      rm -f /tmp/create-new /tmp/audio_cd
      mp3c -i /_config/bin/.mp3crc -b /tmp/create-new
      /_config/bin/cda_info.sh > /tmp/audio_cd
      if [ "$OLD_CD_TYPE" != 99 ] ; then
         svdrpsend.pl HITK USER1
   #      svdrpsend.pl HITK USER2
      fi
   fi
}

audio_stop() {
   if [ "$AUTORUN" = "1" ] ; then
      rm -f /tmp/create-new /tmp/audio_cd
      if [ "`lsof | grep '^vdr' | grep '/cd$'`" != "" ] ; then
         svdrpsend.pl HITK BACK
      fi
   fi
}

data_start() {
   if [ "$AUTORUN" = "1" ] ; then
      if [ "$OLD_CD_TYPE" != 99 ] ; then
         svdrpsend.pl HITK USER1
      fi
   fi
}

data_stop() {
   if [ "$AUTORUN" = "1" ] ; then
      svdrpsend.pl HITK BACK
      if [ "`lsof | grep ' /mnt/cd'`" != "" ] ; then
         svdrpsend.pl HITK BACK
      fi
   fi
}

logger -s "cdmount: service started"
rm -f /_config/status/cdmount
umount -l $CD_DEV
umount -f $CD_DEV >/dev/null 2>&1
banner
while [ ! -e /_config/status/cdmount ] ; do
   CHECKCD=`cdctl -g`
   CD_TYPE=99
   if (echo "$CHECKCD" | grep " no ") then
      CD_TYPE=0
   elif (echo "$CHECKCD" | grep "type 1") then
      CD_TYPE=1
   elif (echo "$CHECKCD" / | grep "audio") then
      CD_TYPE=2
   else
      CD_TYPE=1
   fi
   if [ "$CD_TYPE" != "$OLD_CD_TYPE" ] ; then
      if [ "$CD_TYPE" = "0" ] ; then
         if [ "$OLD_CD_TYPE" = "1" ] ; then
            data_stop
         elif [ "$OLD_CD_TYPE" = "2" ] ; then
            audio_stop
         fi
         TSTMNT=`mount | grep /mnt/cd`
	 if [ "$TSTMNT" != "" ] ; then
            umount -l $CD_DEV
            logger -s "cdmount: unmounting"
         fi
      else
         TSTMNT=`mount | grep /mnt/cd`
	 if [ "$TSTMNT" = "" ] ; then
            if [ "$CD_TYPE" = "1" ] ; then
               logger -s "cdmount: mounting $CD_DEV /mnt/cdrom"
               mount $CD_DEV /mnt/cdrom
               if [ "$?" = 0 ] ; then
                  cdctl -o0
                  data_start
               else
                  logger -s "Mounting failed"
                  CD_TYPE=0
               fi
            else
               mount -t cdfs $CD_DEV /mnt/cdfs
               if [ "$?" = 0 ] ; then
                  cdctl -o0
                  audio_start
               else
                  logger -s "Mounting failed"
                  CD_TYPE=0
               fi
            fi
            if [ "$CD_TYPE" != "0" ] && [ "$SLOW_DOWN_CMD" != "" ] ; then
               $SLOW_DOWN_CMD
	    fi
         else
            logger -s "cdmount: mounting not possible - already mounted"
            umount -f $CD_DEV >/dev/null 2>&1
            CD_TYPE=0
         fi
      fi
      OLD_CD_TYPE=$CD_TYPE
   else
      if [ "$CD_TYPE" != "0" ] ; then
         cdctl -o0
      fi	
   fi
   sleep $REFRESH_RATE
done
rm -f /_config/status/cdmount
logger -s "cdmount: service stopped"
exit 0

