#!/bin/sh
BOOTLOG=/mnt/record/pvr/log/bootlog
mkdir -p /mnt/record/pvr/log 2>/dev/null

bootlog() {
	echo "`date +%Y%m%d\ %H%M%S` [pvrinit] $1" >>$BOOTLOG 2>&1;
}
bootlog "pvr-init log start at $BOOTLOG"

bootlog "generating ini: $PORT"
PORT=`cat /srv/pvr/proc/architecture`
if [ -e "/srv/pvr/etc/port/$PORT.ini" ]; then
	cat /srv/pvr/etc/config.skel /srv/pvr/etc/port/$PORT.ini >/srv/pvr/etc/config.ini
else
	cp /srv/pvr/etc/config.skel /srv/pvr/etc/config.ini
fi


# set db & restore backup if needed
bootlog "PVR unique id"
su - pvr -c '/srv/pvr/bin/run setusbid' >>$BOOTLOG 2>&1
# set db & restore backup if needed
bootlog "DB restore and versioning"
su - pvr -c '/srv/pvr/bin/run dbversion' >>$BOOTLOG 2>&1
# module cleanup
bootlog "module cleanup"
su - pvr -c '/srv/pvr/bin/run module.stop' >>$BOOTLOG 2>&1
umount /mnt/modules/* >/dev/null 2>&1
rmdir /mnt/modules/* >/dev/null 2>&1

# httpd
bootlog "start: httpd"
/etc/init.d/httpd stop >>$BOOTLOG 2>&1
pkill -9 apache2
rm /var/run/apache2.pid
/etc/init.d/httpd start >>$BOOTLOG 2>&1

# upnpd needs running httpd
bootlog "start: pvr upnpd"
/srv/pvr/etc/init.d/pvr-upnpd start >/dev/null 2>&1
# catch adb upnp responses
bootlog "ADB collect"
/srv/pvr/bin/adbcollect >>$BOOTLOG 2>&1

# radio ts->mp3 cleanup
bootlog "radio MP3 muxer"
su - pvr -c '/srv/pvr/bin/run mux.mp3' >>$BOOTLOG 2>&1

# bg new version check
su - pvr -c '/srv/pvr/bin/run version nowait' >>$BOOTLOG 2>&1

# try to catch IGMP join requests
#bootlog "IGMP Sniffer start"
#/etc/init.d/pvr-igmp start >>$BOOTLOG 2>&1

# module start
bootlog "module init"
su - pvr -c '/srv/pvr/bin/run module.init' >>$BOOTLOG 2>&1

bootlog "removing stale timer locks"
rm /srv/pvr/var/lock/timer_* >/dev/null 2>&1


bootlog "pvr up and running"

