#!/bin/sh

# iptv PVR bootstrap
#
# @version	2011-12-05
# @arch		syna7
# @see		http://pvr.unzureichende.info/
# @env		run by DSM, needs $SYNOPKG_PKGDEST, example: "/volume1/@appstore/iptvPVR"


if [ "${SYNOPKG_PKGDEST}" = "" ]; then
	SYNOPKG_PKGDEST="/volume1/@appstore/iptvPVR"
fi
PVR_CHROOT=${SYNOPKG_PKGDEST}/usb/.pvr
PVR_USBRAW=${SYNOPKG_PKGDEST}/usb
#TODO: discovery
PVR_QUIETLOG=0
PVR_USBENABLED=0
PVR_VOLUME=`echo ${SYNOPKG_PKGDEST} | cut -d/ -f2`
PVR_BOOTLOG="/${PVR_VOLUME}/pvr/log/bootlog"

if [ ! -x /${PVR_VOLUME}/pvr/log ]; then
	mkdir -p /${PVR_VOLUME}/pvr/log                         
	chmod a+w /${PVR_VOLUME}/pvr/log                        
fi

pvr_bootlog() {
	if [ ${PVR_QUIETLOG} -eq 0 ]; then
	echo "`date +%Y%m%d\ %H%M%S` [synpkg] $1"
		echo "`date +%Y%m%d\ %H%M%S` [synpkg] $1" >>$PVR_BOOTLOG
	fi
}

pvr_usbinstall() {
	# search mounted USB volumes for "install here" file
	PVR_USBTARGET=`find /volumeUSB* -maxdepth 2 -name iptvpvr-usbinstall.txt | cut -s -d/ -f2,3`
	if [ -e "/${PVR_USBTARGET}/iptvpvr-usbinstall.txt" ]; then
		pvr_bootlog "USB install: ${PVR_USBTARGET}"	
		# find mountpoint
		PVR_USBPART=`grep " /${PVR_USBTARGET} " /proc/mounts | cut -d' ' -f1`
		pvr_bootlog "unmounting USB partition: ${PVR_USBPART}"
		umount /${PVR_USBTARGET}
		pvr_bootlog "creating filesystem ${PVR_USBPART}"
		mke2fs -T ext4 -L iptvpvr "${PVR_USBPART}" >>$PVR_BOOTLOG 2>&1
		pvr_bootlog "remounting /${PVR_REMOUNT}"
		PVR_USBVOLUME=`echo "${PVR_USBTARGET}" | cut -d/ -f1`
		PVR_REMOUNT="/${PVR_USBVOLUME}/iptvPVR"
		mkdir "${PVR_REMOUNT}" 2>/dev/null
		mount -t ext4 -o rw,noatime,sync "${PVR_USBPART}" "${PVR_REMOUNT}" >>$PVR_BOOTLOG 2>&1
		pvr_bootlog "moving settings, cache, modules"
		cp -R ${SYNOPKG_PKGDEST}/usb/* "${PVR_REMOUNT}/"
		chmod -R a+wr /${PVR_REMOUNT}/*
		pvr_bootlog "moving chroot - will take a long time"
		cp -aR ${SYNOPKG_PKGDEST}/usb/.pvr "/${PVR_REMOUNT}/"
		pvr_bootlog "removing from HDD"
#		rm -r ${SYNOPKG_PKGDEST}/usb
		rm -r ${SYNOPKG_PKGDEST}/usb.bak >/dev/null 2>&1
		mv ${SYNOPKG_PKGDEST}/usb ${SYNOPKG_PKGDEST}/usb.bak
		CHROOT="/${PVR_REMOUNT}/.pvr"
	else
		pvr_bootlog "no USB install"
	fi
}

pvr_usbfind() {
	pvr_bootlog "searching USB devices"
	if [ ! -x "${SYNOPKG_PKGDEST}/usb" ]; then
		grep /volumeUSB /proc/mounts >/dev/null 2>&1 && break
		if [ $? -ne 0 ]; then
			pvr_bootlog "no USB devices mounted"
		fi
#		pvr_bootlog "USB devices mounted"
	fi
	PVR_USBTARGET=`find /volumeUSB* -maxdepth 2 -name iptvpvr.volume | cut -s -d/ -f2,3`
	if [ -e "/${PVR_USBTARGET}/iptvpvr.volume" ]; then
		PVR_USBPART=`grep " /${PVR_USBTARGET} " /proc/mounts | cut -d' ' -f1`
		echo ${PVR_USBTARGET} | grep iptvPVR >/dev/null 2>&1
		if [ $? -eq 1 ]; then
			# remount
			pvr_bootlog "found /${PVR_USBTARGET}, unmounting"
			PVR_USBVOLUME=`echo "${PVR_USBTARGET}" | cut -d/ -f1`
			PVR_REMOUNT="/${PVR_USBVOLUME}/iptvPVR"
			umount -f /${PVR_USBTARGET}
			pvr_bootlog "remounting /${PVR_REMOUNT}"
			mount -t ext4 -o rw,noatime,errors=continue "${PVR_USBPART}" "${PVR_REMOUNT}" >>$PVR_BOOTLOG 2>&1
			PVR_USBTARGET="${PVR_USBVOLUME}/iptvPVR"
		fi
		pvr_bootlog "found /${PVR_USBTARGET} on ${PVR_USBPART}"
		PVR_CHROOT="/${PVR_USBTARGET}/.pvr"
		PVR_USBRAW="/${PVR_USBTARGET}"
		PVR_USBENABLED=1
	else
		pvr_bootlog "WARNING: no USB partition found, trying HDD"
		if [ -e "${SYNOPKG_PKGDEST}/usb/iptvpvr.volume" ]; then
			PVR_CHROOT=${SYNOPKG_PKGDEST}/usb/.pvr
		else
			pvr_bootlog "ERROR: iptv PVR not found. Insert USB device or reinstall."
#			exit 1
		fi
	fi
}

pvr_preparechroot() {
	pvr_bootlog "preparing: ${PVR_CHROOT}"
	mount -o bind /dev/pts ${PVR_CHROOT}/dev/pts
	mount -o bind /proc ${PVR_CHROOT}/proc
	mount -o bind /proc/bus/usb ${PVR_CHROOT}/proc/bus/usb

	pvr_bootlog "(pseudo-) usb: ${PVR_USBRAW}"	
	mount -o bind ${PVR_USBRAW} ${PVR_CHROOT}/mnt/usbraw
	mount -o rw,mode=0755,noatime `${PVR_CHROOT}/bin/mountpoint -n /${PVR_VOLUME} | cut -d' ' -f1` ${PVR_CHROOT}/mnt/record
	cp /etc/resolv.conf ${PVR_CHROOT}/etc/
	if [ ! -x /${PVR_VOLUME}/record ]; then
		pvr_bootlog "creating /${PVR_VOLUME}/record"
		mkdir /${PVR_VOLUME}/record >/dev/null 2>&1
		chmod a+rwx /${PVR_VOLUME}/record >/dev/null 2>&1
	else
		echo "/${PVR_VOLUME}/record exists"
	fi
	chmod a+rw /${PVR_VOLUME}/record/* >/dev/null 2>&1
}

pvr_checkrunning() {
	grep ${PVR_CHROOT} /proc/mounts >/dev/null 2>&1
	if [ $? = 0 ]; then
		pvr_bootlog "abort: iptv PVR already running"
		exit 0
	fi
	pvr_bootlog "iptv PVR bootstrap: synology"
}


case $1 in
	start)	cat /dev/null >$PVR_BOOTLOG
			pvr_checkrunning
			pvr_usbinstall
			pvr_usbfind
			pvr_preparechroot
			pvr_bootlog "chroot: ${PVR_CHROOT} /etc/rc.boot"
			chroot ${PVR_CHROOT} /etc/rc.boot
			;;
	stop)	pvr_usbfind
			chroot ${PVR_CHROOT} /etc/rc.shutdown >>${PVR_BOOTLOG}
#			kill -9 `${PVR_CHROOT}/bin/lsof -t ${PVR_CHROOT}`
			sleep 10
			umount ${PVR_CHROOT}/dev/pts  >>${PVR_BOOTLOG} 2>/dev/null 
			umount ${PVR_CHROOT}/proc/bus/usb 2>/dev/null 
			umount ${PVR_CHROOT}/proc 2>/dev/null 
			umount ${PVR_CHROOT}/mnt/record 2>/dev/null 
			umount -f ${PVR_CHROOT}/mnt/usbraw 2>/dev/null
#			cat /dev/null >${PVR_BOOTLOG}
			pvr_bootlog "stopped"
			exit 0
			;;
	status)		exit 0
			;;
	log)	PVR_QUIETLOG=1
			pvr_usbfind
			if [ ! -s "/${PVR_VOLUME}/pvr/log/bootlog" ]; then
				if [ -s "${PVR_CHROOT}/srv/pvr/var/log/vdr_log" ]; then
					echo "<h3>Aufnahmelog</h3>" >/${PVR_VOLUME}/pvr/log/synology.out
					sed ':a;N;$!ba;s/\n/<br\/>/g' "${PVR_CHROOT}/srv/pvr/var/log/vdr_log" >>/${PVR_VOLUME}/pvr/log/synology.out
				else
					echo "iptv PVR l&auml;uft - hoffentlich<br/>Hier erscheint das Aufnahmeprotokoll sobald es eines gibt.<br/>" >/${PVR_VOLUME}/pvr/log/synology.out
				fi	
			else
				echo "<h3>Bootlog</h3>" >/${PVR_VOLUME}/pvr/log/synology.out
				sed ':a;N;$!ba;s/\n/<br\/>/g' "/${PVR_VOLUME}/pvr/log/bootlog" >>/${PVR_VOLUME}/pvr/log/synology.out
			fi
			echo "/${PVR_VOLUME}/pvr/log/synology.out"
			;;
esac
exit 0

