################################################################################
# Filename: ./src/plugins/tftpd/include/tftpd-functions
# 
# The contents of this file are subject to the Qlusters Public License
# Version 1.1 (the License); you may not use this file except in compliance
# with the License.
# You may obtain a copy of the License at
# http://www.openqrm.org/content/view/33/69/.
# 
# Software distributed under the License is distributed on an AS IS basis,
# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
# the specific language governing rights and limitations under the License.
# 
# The Original Code is openQRM.
# 
# The Initial Developer of the Original Code is Qlusters Corporation. Portions
# created by Qlusters Corporation are Copyright (C) 2003-2005. All Rights
# Reserved.
# 
# Contributor(s):
# 
# Description:
# 
# Comments:
################################################################################
PLUGIN_INIT_SCRIPT=${QRM_PLUGIN_BASE_DIR}/etc/init.d/tftpd
function set_system_tftpd_service() {
# needs to be declared on the very left
	NEWL='\
'

	# is there a system tftp service ?
	if [ -f /etc/xinetd.d/tftp ]; then
		echo "Found system's tftp service and re-configuring it now"
		TMP_FILE=`mktemp -q /tmp/XXXXXX`
		case "$1" in
			off)
				if grep disable /etc/xinetd.d/tftp 1>/dev/null; then
					cat /etc/xinetd.d/tftp | sed -e "s/.*disable.*/        disable         = yes/g" > $TMP_FILE
				else
					cat /etc/xinetd.d/tftp | sed -e "s/.*}.*/        disable         = yes$NEWL}/g" > $TMP_FILE
				fi
				;;
			on)
				if grep disable /etc/xinetd.d/tftp 1>/dev/null; then
					cat /etc/xinetd.d/tftp | sed -e "s/.*disable.*/#        disable         = yes/g" > $TMP_FILE
				else
					cat /etc/xinetd.d/tftp | sed -e "s/.*}.*/#        disable         = yes$NEWL}/g" > $TMP_FILE
				fi
				;;		
			*)
				echo "No such option. Use with on/off as parameter"
				/bin/rm -f $TMP_FILE
				return 1
				;;
		esac
		/bin/mv $TMP_FILE /etc/xinetd.d/tftp
		# restart needed ?
		if [ -x /etc/init.d/xinetd ]; then
			if ps -C xinetd 1>/dev/null; then
				echo "Restarting xinetd to udpate the system's tftp configuration"
				/etc/init.d/xinetd restart
			fi
		else
			echo "ERROR: Could not restart xinetd to update the system's tftp configuration."
			echo "       Please restart xinetd manually and try again."
			return 1
		fi			
	fi
	return 0
}



 

function tftpd_install() {
	set_system_tftpd_service off
}

function tftpd_uninstall() {
	set_system_tftpd_service on
}

function tftpd_start() {
	${PLUGIN_INIT_SCRIPT} start
}

function tftpd_stop() {
	${PLUGIN_INIT_SCRIPT} stop
}

function tftpd_run_once() {
	: 
}
