#!/bin/sh
#
# Copyright (c) 2000 by Sun Microsystems, Inc.
# All rights reserved.
#
# ident	"@(#)postinstall.tmpl	1.1	00/12/01 SMI"
#

#
# Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#
# ident	"@(#)proc.inetd_install	1.2	04/07/29 SMI"
#

# proc.inetd_install -- common code for inetd.conf entry addition
#
# inetd_init	: call before any other functions
# inetd_add     : if the regular expression specified as argument 1
#		  does not match any line in inetd.conf, add the lines
#		  provided on stdin to the file and restart inetd.conf
# inetd_undo	: call if rest of procedure script fails
# inetd_fini	: call if rest of procedure script succeeds
#
# inetd_init and inetd_add will perform necessary clean-up and
# return a non-zero exit code on failure.

inetconf=${PKG_INSTALL_ROOT:-/}/etc/inet/inetd.conf
inetold=/tmp/inetd.conf.$$

inetd_init() {
	cat $inetconf > $inetold
	if [ $? -ne 0 ]; then
		echo "can't create $inetold"
		return 1
	fi
	return 0
}

inetd_fini() {
	rm -f -- $inetold
	return 0
}

inetd_undo() {
	cat $inetold > $inetconf
	inetd_fini
}

inetd_add() {
	grep -s "$1" $inetconf > /dev/null 2>&1 || cat >> $inetconf
	if [ $? -ne 0 ]; then
		echo "can't edit $inetconf"
		inetd_undo
		return 1
	fi
	return 0
}

# Add entry for in.tftpd in /etc/inet/inetd.conf

inetd_init
if [ $? -ne 0 ]; then
	exit 1
fi
inetd_add "^[#	 ]*tftp[ 	]*dgram" << EOF
# TFTPD - tftp server (primarily used for booting)
#tftp	dgram	udp6	wait	root	/usr/sbin/in.tftpd	in.tftpd -s /tftpboot
EOF
if [ $? -ne 0 ]; then
	exit 1
fi
inetd_fini
