#!/bin/sh

/bin/mount -n -t proc proc /proc
/bin/mount -n -t devpts devpts /dev/pts
export INSMOD="/sbin/insmod"
export MODDIR="/lib/modules/"$(uname -r)
#export TZ=CET-1CEST,M3.5.0/2,M10.5.0/3
/etc/init.d/init1

init=0
/bin/boot || init=1
if [ $init -eq 1 ] ; then
	/bin/eraseall /dev/mtd/1
fi

/bin/mount -t jffs2 /dev/mtdblock/1 /var
/bin/mount -t ramfs none /tmp

init=0

# check if /var/.init exist .. then no flash erase is needed .. this is new !
if [ -e /var/.init ]; then
    echo "no flash erase needed"
else
# new /var/.init not found.. check if old /var/tmp/init exist.. 
# then not flash erase ist needed
    if [ -e /var/tmp/init ]; then
# ok old /var/tmp/init exist.. no create the new flash_erase detection file..
	touch /var/.init
# delete /var/tmp folder now.. 	
	rm -rf /var/tmp
# create link to /tmp <- this is mounted to RAM 
	ln -sf /tmp /var/tmp
    else
# flash erase is needed ...     
	init=1
    fi
fi

if [ $init -eq 1 ] ; then 
	cp -a /var_init/* /var
	touch /var/.init
	sync
	umount /var
	/bin/mount -t jffs2 /dev/mtdblock/1 /var
fi

ln -sf sound/dsp /dev/dsp
ln -sf fb/0 /dev/fb0
ln -s /dev/input/mice /dev/psaux 
ln -s /dev/input/mice /dev/mouse 

/bin/hostname dreambox

/sbin/ifconfig lo 127.0.0.1 netmask 255.0.0.0 up

/sbin/inetd &

. /proc/bus/dbox.sh
#change into folder in RAM ( ramfs )
cd /tmp
mount /dev/ide/host0/bus0/target0/lun0/part1 /hdd 
mount /dev/scsi/host0/bus0/target0/lun0/part1 /mnt/usb
# enable corefiles (for debugging)
echo "/hdd/%N-%U.core" > /proc/sys/kernel/core_name_format
ulimit -c unlimited
# sleep after 2min
hdparm -S 120 /dev/ide/host0/bus0/target0/lun0/disc
# accustic management
hdparm -M 128 /dev/ide/host0/bus0/target0/lun0/disc

/bin/smbd -D 
/bin/nmbd -D 

export LD_LIBRARY_PATH=/tmp:/var/lib:/lib:$LD_LIBRARY_PATH
export PATH=/tmp:/var/bin:$PATH

mount /dev/ide/host0/bus0/target0/lun0/part1 /hdd
mount /dev/scsi/host0/bus0/target0/lun0/part1 /mnt/usb

if [ -e /var/etc/init ] ; then
	. /var/etc/init
fi

touch /tmp/.enigma
while [ -e /tmp/.enigma ]
do
    #dccamd <--- uitgeschakeld
    /bin/scam
    /bin/enigma
    case "$?" in
       0)
            echo "Shutdown"
            rm /tmp/.enigma
            /sbin/halt
            ;;
      2) 
            echo "restart"
            ;;
	3)	    
	    echo "Reboot Flash"
	    killall -9 dccamd <--- uitgeschakeld
          killall -9 scam
	    rm /tmp/.enigma
	    umount /var
	    umount /hdd
	    sleep 2
	    /tmp/reboot
	    ;;
        *)
            echo "Reboot"
	    /sbin/reboot
            ;;
    esac
    #killall -9 dccamd <--- uitgescakeld
    killall -9 scam
done

exit 0
