#!/bin/sh

# Source debconf library.
. /usr/share/debconf/confmodule

# Returns 0, if run on UCS
UCS_detect()
{
	[ -f '/usr/sbin/udm' ]
	echo $?
}

if [ $(UCS_detect) -eq 0 ]
then
	rm /etc/init.d/tftpd-hpa
	ln -s /bin/true /etc/init.d/tftpd-hpa
	killall -9 in.tftpd
	/etc/init.d/openbsd-inetd restart
	ucr set security/packetfilter/udp/69/all=ACCEPT
	/etc/init.d/univention-firewall restart
fi

db_get m23-tftp/configureTFTP
if [ "$RET" = "true" ] && [ $(UCS_detect) -ne 0 ]
then
	if [ -f /etc/inetd.conf ]
	then
		sed 's/^tftp/# tftp/g' /etc/inetd.conf > /tmp/inetd.conf
		echo "tftp dgram udp4 wait root /usr/sbin/in.tftpd /usr/sbin/in.tftpd -s /m23/tftp -r blksize" >> /tmp/inetd.conf
		mv /tmp/inetd.conf /etc
		chown root:root /etc/inetd.conf
		chmod 644 /etc/inetd.conf
	fi

	#Add IPv4 mode on Raspberry Pi
	if [ `uname -m` = armv6l ]
	then
		add4=' -4'
	fi
	
	echo '#Defaults for tftpd-hpa
RUN_DAEMON="yes"
OPTIONS="-l -s /m23/tftp"
' > /etc/default/tftpd-hpa
	/etc/init.d/tftpd-hpa start

	if [ -f /etc/default/tftpd-hpa ]
	then
		echo 'TFTP_OPTIONS="--verbose --secure'$add4'"
TFTP_USERNAME="tftp"
TFTP_DIRECTORY="/m23/tftp"
TFTP_ADDRESS="'`/m23/bin/serverInfoIP`':69"' > /etc/default/tftpd-hpa
		/etc/init.d/tftpd-hpa restart
	fi
fi


#Check, if the directory with backuped bootimages exists
if [ -d /m23/tftp/bak2.6 ]
then
	#Restore the backup files
	cp -a /m23/tftp/bak2.6/* /m23/tftp/
fi

db_stop