# MODFS_MODSCRIPT
# SUPPORTS precheck postcheck install language(en,de)
# NAME install YourFritz hooks
# DESCRIPTION en
# modify various startup and shutdown scripts to get hooks to execute own commands
# inside /var/media/ftp
# DESCRIPTION de
# Modifizieren verschiedener Start- und Shutdown-Skripte, um bei diversen Events     
# mit eigenen Kommandos reagieren zu können, ist als Basis für die korrekte Funktion
# des YourFritz-Frameworks erforderlich
# EOH
#
# process parameters
#
language=$1
rootdir=$2
mode=$3
step=$4
[ ${#4} -eq 0 ] && exit 59 # invalid call
#
# execute the requested step
#
rc=0
inithook=/etc/init.d/E19-yourfritz
yourfritz_dir=/var/media/ftp/yourfritz
hookdir=/var/yourfritz/hooks
tmpfile=/var/tmp/yourfritz_mod_input.txt
cmdfile=/var/tmp/yourfritz_mod_cmd.txt
wrapper=/wrapper
install_hooks()
{
	set -x
	
	# we need a better equipped busybox
	cp files/busybox $rootdir/bin/busybox
	
	cd $rootdir

	# set configuration file path in S09-config
	echo "export YOURFRITZ_CONF=$yourfritz_dir/etc/yourfritz.conf" >> etc/init.d/S09-config

	# S20-inithook
	cat > etc/init.d/S20-inithook <<EOT-S20-inithook
#! /bin/sh
{ hook=$hookdir/init; [ -e \$hook ] && /bin/sh \$hook; }
EOT-S20-inithook
	chmod 555 etc/init.d/S20-inithook

	# S89-startuphook
	cat > etc/init.d/S89-startuphook <<EOT-S89-startuphook
#! /bin/sh
{ hook=$hookdir/startup; [ -e \$hook ] && /bin/sh \$hook; }
EOT-S89-startuphook
	chmod 555 etc/init.d/S89-startuphook

	# S99-tail
	echo "{ hook=$hookdir/sysready; [ -e \$hook ] && /bin/sh \$hook; }" >> etc/init.d/S99-tail

	# onlinechanged
	cat > etc/onlinechanged/yourfritz <<EOT-onlinechanged
#! /bin/sh
hook=$hookdir/onlinechanged
[ -e \$hook ] && /bin/sh \$hook \$*
EOT-onlinechanged
	chmod 555 etc/onlinechanged/yourfritz

	# hotplug/storage
	sed \
	-e "/^test -x \$FRITZNASDBCONTROL && \$FRITZNASDBCONTROL remove_all_usb_partitions/i\
{ hook=$hookdir/umount; [ -e \$hook ] && /bin/sh \$hook umount_all; }
" \
	-e "/^test -x \$WEBDAVCONTROL && \$WEBDAVCONTROL lost_partition \"\$MP\"/i\
{ hook=$hookdir/umount; [ -e \$hook ] && /bin/sh \$hook lost \"\$MP\"; }
" \
	-e "/^if test -d \/var\/InternerSpeicher ; then/i\
{ hook=$hookdir/umount; [ -e \$hook ] && /bin/sh \$hook reconfig; }
" \
	-e "/^test -x \$WEBDAVCONTROL && \$WEBDAVCONTROL lost_all_partitions/i\
{ hook=$hookdir/umount; [ -e \$hook ] && /bin/sh \$hook lost_all; }
" \
	-i etc/hotplug/storage
	
	# hotplug/udev-mount-sd
	sed \
	-e "/^test -x \$FRITZNASDBCONTROL && \$FRITZNASDBCONTROL new_partition \"\$MNTPATH\"/a\
{ hook=$hookdir/mount; [ -e \$hook ] && /bin/sh \$hook mount \"$MNTPATH\"; }
" \
	-e "/^echo udev-mount-sd:unmounting \"\$MPOINT\" > \/dev\/console/a\
{ hook=$hookdir/umount; [ -e \$hook ] && /bin/sh \$hook umount \$MPOINT; }
" \
	-e "/MNTNAME=\$(nicename \$BLKDEVMAIN \$MEDNUM \$PARTNUM)/a\
{ hook=$hookdir/mount; [ -e \$hook ] && MNTNAME=\$(/bin/sh \$hook mountpoint \$BLKDEVMAIN \$MEDNUM \$PARTNUM); }
" \
	-i etc/hotplug/udev-mount-sd

	# post_install
	tar xf var.tar
	cat >var/post_install.new <<EOT-post_install
#! /bin/sh
yourfritz_reboot_hook()
{
	local YF_EVENT=reboot YF_HOOK=$hookdir/reboot rc=0
	if [ -e \$YF_HOOK ]; then
		/bin/sh \$YF_HOOK reboot_\$1
		rc=\$?
		[ \$rc -ne 0 ] && echo "FAILED \$rc" || echo "CONTINUE"
	else
		echo "CONTINUE"
	fi
}
[ x\$(yourfritz_reboot_hook init) != xCONTINUE ] && exit
EOT-post_install
	sed \
	-e "/\/etc\/hotplug\/usb.pandu stop/i\
yourfritz_reboot_hook stopusb
" \
	-e "/## wer hat hier IMMERNOCH die finger drauf/a\
yourfritz_reboot_hook killer
" \
	-e "/## saving events to flash/i\
yourfritz_reboot_hook saveevents
" \
	-e "\$a\
yourfritz_reboot_hook finished
" \
	-e "\$a\
/bin/sh $wrapper/e99-yourfritz
" \
	var/post_install >>var/post_install.new
	mv var/post_install.new var/post_install
	tar cf var.tar var
	rm -r var/* 2>/dev/null	

	# profile	
	echo "{ hook=$hookdir/profile; [ -e \$hook ] && /bin/sh \$hook; }" >> etc/profile

	# eventctrl
	ln -s $yourfritz_dir/scripts/framework/eventctrl sbin/eventctrl

	# E19-yourfritz
	ln -s $wrapper/${inithook##*/} ./$inithook	

	# various links for runit applets
	ln -s /bin/busybox usr/sbin/runsv
	ln -s /bin/busybox usr/sbin/runsvdir
	ln -s /bin/busybox usr/sbin/svlogd

	# create GUI subdirectory
	for dir in $(ls usr/www); do
		case $dir in
			cgi-bin|css|html|js|kids)
				:
				;;
			*)
				mkdir usr/www/$dir/yourfritz
				;;
		esac
	done

	# modify page header templates to include yourfritz menu if necessary
	cat >$tmpfile <<EOT
local yf, err = loadfile("../yourfritz/lib/yf_show_link.lua")
if nil ~= yf then yf(true) end
?>
EOT
	cat >$cmdfile <<EOT
/^\?>/ {
r $tmpfile
d
}
EOT
	sed -f $cmdfile -i usr/www/avm/templates/menu_page_head.html

}
case $step in
	precheck)
		# we check only the existence of the E19-yourfritz link, if it's
		# present already, we assume this modscript has been processed earlier
		if [ -L $rootdir$inithook ]; then
			case "$language" in
				de)
					echo "Die Modifikation wurde bereit angewendet." 1>&2
					;;
				*)
					echo "The hooks are already installed." 1>&2
					;;
			esac
			rc=1
		else
			rc=0
		fi
		;;
	postcheck)
		if ! [ -L $rootdir$inithook ]; then
			case "$language" in
				de)
					echo "Die Modifikation war nicht oder nur teilweise erfolgreich." 1>&2
					;;
				*)
					echo "Some hooks have failed to be installed." 1>&2
					;;
			esac
			rc=1
		fi
		;;
	install)
		opwd=$(pwd)
		install_hooks
		rc=$?
		cd $opwd
		;;
	*)
		rc=59
		;;
esac
exit $rc
