#! /bin/sh
# Copyright (c) 1998-2001 SuSE GmbH Nuernberg, Germany.
#
# Author:	Carsten Hoeger <feedback@suse.de>
#		Lars Mueller <feedback@suse.de>
#
# /etc/init.d/cipe
#	and its symbolic link
# /usr/sbin/rccipe
#
### BEGIN INIT INFO
# Provides:       cipe
# Required-Start: $network $remote_fs
# Required-Stop:
# Default-Start:  3 5
# Default-Stop:  0 1 2 6
# Description:    start the CIPE Tunnel
### END INIT INFO

CIPED_BIN="/usr/sbin/ciped-cb"
CIPED_OPTS_BASE="/etc/cipe/options"
CIPED_PID_FILE_BASE="/var/run/cipe/cipcb"

# Status shell functions
. /etc/rc.status
# Reset status of this service
rc_reset

# Check if there is at least configuration file
ls ${CIPED_OPTS_BASE}* >/dev/null 2>&1
if [ $? -ne 0 ]; then
	echo -n "No configuration file with basename ${CIPED_OPTS_BASE} found."
	# Tell the user this has skipped
	rc_status -s
	exit 6
fi

# Check for missing binary
if [ ! -x ${CIPED_BIN} ]; then
	echo -n "CIPE daemon, ${CIPED_BIN} is not installed."
	# Tell the user this has skipped
	rc_status -s
	exit 5
fi

# Check permissions of the config files and set to 0600 if needed
TMPFILE=$( mktemp /var/tmp/cipe_options.XXXXXX)
for OPT_FILE in ${CIPED_OPTS_BASE}*; do
	echo "${OPT_FILE} root.root 0600" > ${TMPFILE}
	chkstat -set ${TMPFILE}
done
rm ${TMPFILE}

# Create daemon wording
test $( ls ${CIPED_OPTS_BASE}* | wc -l) -gt 1 && CIPED_WORD="daemons" || CIPED_WORD="daemon"

function get_peer()
{
	while read peer host; do \
		case "${peer}" in \
			""|\#*) continue ;; \
			peer) break ;; \
		esac \
	done < "${OPT_FILE}"
	PEER=${host%:*}
}

case "$1" in
    start)
	echo "Starting CIPE ${CIPED_WORD}"

	# Check if device for the peer is up
	for OPT_FILE in ${CIPED_OPTS_BASE}*; do
		get_peer
		echo -n "for peer ${PEER}."
		rc=1
		timer=15
		while [ ${rc} -ne 0 ] && [ ${timer} -gt 0 ]; do
			LNR=0
			while IFS=: read IFACE OTHER ; do
				test "$((++LNR))" -le 2 && continue
				ifuser "${IFACE// }" "${PEER}"
				if [ $? -eq 0 ]; then
					rc=0
					break
				fi
			done < /proc/net/dev
			test ${rc} -ne 0 && sleep 1 || break
			echo -n "."
			timer=$[${timer}-1]
		done
		if [ ${rc} -eq 0 ]; then
			# Check if ciped for this option file is running
			for pid in $( pidof ${CIPED_BIN}); do
				grep "${OPT_FILE}" /proc/${pid}/cmdline >/dev/null
				if [ $? -eq 0 ]; then
					rc=1
					break
				fi
			done
			rc_reset
			if [ ${rc} -eq 0 ]; then
				startproc -f ${CIPED_BIN} -o "${OPT_FILE}" || rc_failed
				rc_status -v
			else
				echo -n "  Warning: Already running."
				rc_status -s
			fi
		else
			echo -n "  Error: No route to peer."
			rc_status -s
		fi
	done
	;;
    stop)
	echo -n "Shutting down CIPE ${CIPED_WORD}"
	/sbin/killproc ${CIPED_BIN} || rc_failed

	rc_status -v
	;;
    restart|force-reload)
        ## If first returns OK call the second, if first or
        ## second command fails, set echo return value.
        $0 stop  &&  $0 start  ||  rc_failed
	rc_status
        ;;
    reload)
        # Reload not supported.
	rc_failed 3
	rc_status -v
        ;;
    status)
        echo "Checking CIPE ${CIPED_WORD}"
	CIPED_NOT_RUNNING="no"
	for OPT_FILE in ${CIPED_OPTS_BASE}*; do
		get_peer
		echo -n "for peer ${PEER}."
		# Check if ciped for this option file is running
		rc=1
		for pid in $( pidof ${CIPED_BIN}); do
			grep "${OPT_FILE}" /proc/${pid}/cmdline >/dev/null
			if [ $? -eq 0 ]; then
				rc=0
				break
			fi
		done
		rc_reset
		if [ ${rc} -eq 1 ]; then
			rc_failed
			CIPED_NOT_RUNNING="yes"
		fi
		rc_status -v
	done
	test ${CIPED_NOT_RUNNING} = "yes" && exit 7
        ;;
    *)
	echo "Usage: $0 {start|stop|status|restart|reload|force-reload}"
	exit 1
	;;
esac

rc_exit

