#!/bin/sh
set -x
. /var/etc/autostart/start-config

echo "start /var/etc/autostart/start-e2.sh"

#Bauernbub anfang
if  grep -q "config.misc.firstrun=true" /usr/local/share/enigma2/settings && [ -e /mnt/usb/XBA*/ba/backup-e2/bouquets.tv ]; then
  /usr/lib/enigma2/python/AAF/Menu/Plugins/script/restore2.sh
fi
#Bauernbub ende

if [ $pl2303 == 'y' ]; then
  mknod /dev/ttyUSB0 c 188 0
  chmod 666 /dev/ttyUSB0
  insmod /lib/modules/usbserial.ko
  insmod /lib/modules/pl2303.ko
fi

if [ $pl2303 == 'n' ] && [ $ftdi == 'y' ] ; then
  mknod /dev/ttyUSB0 c 188 0
  chmod 666 /dev/ttyUSB0
  insmod /lib/modules/usbserial.ko
  insmod /lib/modules/ftdi_sio.ko
fi  

if [ $profile == 'y' ]; then                           
	echo "create profile"
	touch /var/tmp/profile
	echo "create blacklist"
	touch /var/tmp/blacklist
	echo "create playlist.e2pls"
	touch /var/tmp/playlist.e2pls
	echo "create whitelist"
	touch /var/tmp/whitelist
	ln -s /var/tmp/profile /var/usr/local/share/enigma2/profile
fi



if [ "$emu" != "off" ]; then
	echo start $emu
	/var/emu/$emu &	
fi

#if  [ -e /media/hdd ] && [ $rmemthy == 'y' ]; then                      
#	echo start rmemthy
#	LIST=`ls -1 /media/hdd`
#	for ROUND in $LIST; do
#		LENG=`ls -al /media/hdd/"$ROUND" | wc -l`
#		if [ $LENG = "2" ]; then		
#			echo "remove empty folder $LIST / $ROUND"  
#			rm -rf /media/hdd/"$ROUND"
#		fi
#	done
#fi

if [ `which amount | wc -l` -eq 1 ] && [ $automount == 'y' ]; then                      
	echo "start amount auto"
	amount
fi

if [ -e /hdd/epg.dat ]; then
  mkdir -p /backup/epg > /dev/null 2>&1
  cp /hdd/epg.dat /backup/epg/epg.dat
else
	if [ -e /backup/epg/epg.dat ]; then
	  cp /backup/epg/epg.dat /hdd/epg.dat
	fi
fi
sync	  
  

if [ `which autodns | wc -l` -eq 1 ] && [ $autodns == 'y' ]; then                      
	echo "start autodns"
	autodns &
fi

if [ $autoFS == 'y' ]; then                      
	echo "start autoFS"
	insmod /lib/modules/autofs4.ko
	/etc/init.d/autofs start
fi

if [ -e /var/usr/local/share/enigma2/playlist.e2pls ] && [ $playlist == 'y' ]; then
	echo "start remove playlist"
	rm /var/usr/local/share/enigma2/playlist.e2pls
fi

if [ -e /lib/modules/fuse.ko ] && [ $netmod == 'y' ]; then
	IP="`ifconfig | grep inet | grep Bcast | cut -d : -f 2 | sed 's/ Bcast//' | sed 's/ //'`"
	echo "set IP $IP in smb.conf"
	HELPIP=`echo -n "interfaces = $IP"`
	if [ `cat /var/root/.smb/smb.conf | grep "$HELPIP" | wc -l` -lt 1 ]; then
	  sed -e "s/interfaces = *.*.*.*\//$HELPIP\//g" -i /var/root/.smb/smb.conf
	  sync
	fi
	echo "start netmod"
	#insmod /lib/modules/fuse.ko
	#insmod /lib/modules/cifs.ko
	if [ `cat /var/etc/smbfs | grep "True" | wc -l` -eq 1 ]; then
	  /bin/fusesmb -o nonempty /media/smb
	fi
	if [ `cat /var/etc/upnpfs | grep "True" | wc -l` -eq 1 ]; then
	  /bin/djmount -o nonempty /media/upnp
	fi  
fi

if [ `which portmap | wc -l` -eq 1 ] && [ -e /etc/init.d/portmap ] && [ $portmap == 'y' ]; then                           
	echo "start portmap"
	/etc/init.d/portmap start
fi

if [ `which swap | wc -l` -eq 1 ] && [ $swap == 'y' ]; then                      
	echo "start swap"
	swap &
fi

if [ `ls -1 /var/media/hdd | wc -l` != 0 ] && [ $rmcrashlog == 'y' ]; then                      
	echo "start rmcrashlog"
	rm -rf /var/media/hdd/enigma2_crash_*.log > /dev/null 2>&1
fi

if [ -e /bin/smbd ] && [ -e /bin/nmbd ] && [ $sambaserver == 'y' ]; then
	echo "start samba"
	/bin/smbd -D
	/bin/nmbd -D
fi

if [ `which amount | wc -l` -eq 1 ] && [ $automount == 'y' ]; then                      
	echo "start amount auto"
	(sleep 20; amount 10 &) &
fi

exit