#!/bin/sh                                                                                             
#
# socat		Start/stop the socat channell.
#
# chkconfig:    2345 11 89
#
# description:  Multipurpose relay
#

# Get service config
[ -f /etc/sysconfig/socat ] && . /etc/sysconfig/socat

[ -n "$2" ] && TUNNELS="$2"

# no tunnels. exit silently
if [ -z "$TUNNELS" ]; then
	case "$1" in
		start|stop|restart|reload|force-reload)
		exit 0
		;;
	esac
fi

# Source function library
. /etc/rc.d/init.d/functions

# check if the tunnel $1 is up
tunlup() {
	local tun="$1"
	local pidfile=/var/run/socat/$tun.pid
	local pid=$(cat $pidfile 2>/dev/null)
	kill -0 $pid 2>/dev/null
	return $?
}

# check if all the configured tunnels are up
tunlsup() {
	ret=0
	for tun in $TUNNELS; do
		tunlup $tun && continue
		ret=1
	done
	return $ret
}

rclog_warning() {
	if is_yes "${RC_LOGGING}"; then
		echo "WARNING: RC_LOGGING is enabled!"
		echo "socat instances will be started but you can have some problems, i.e.:"
		echo " - service status is not available"
		echo " - instances can be stopped manually only"
		echo " - duplicate processes can happen"
		echo
	fi
}


start() {
	# Check if the service is already running?
	if ! tunlsup; then
		rclog_warning
		msg_starting "socat"; started
		for tun in $TUNNELS; do
			# Clear vars:
			unset OPTIONS
			unset BIADDRESS1
			unset BIADDRESS2
			config="/etc/socat/$tun.conf"
			if [ ! -f "$config" ]; then
				nls "Invalid tunnel \`%s': missing config: %s" $tun "$config"
				fail
				RET=1
			else
				show "Starting socat tunnel %s" "$tun"
				if tunlup $tun; then
					started
					continue
				fi
				
				# include configuration:
				. $config
				# FIXME: check if variables are set
				daemon --fork --pidfile /var/run/socat/$tun.pid --makepid \
					/usr/bin/socat ${SOCAT_OPT} ${OPTIONS} \
					${BIADDRESS1} ${BIADDRESS2}
                                RET=$?
                        fi
                        [ $RETVAL -eq 0 ] && RETVAL=$RET
                done
                [ $RETVAL -eq 0 ] && touch /var/lock/subsys/socat
        else
                msg_already_running "socat"
        fi
}

stop() {
	rclog_warning
	if tunlsup; then
		# Stop daemons.
		msg_stopping "socat"; started
                for tun in $TUNNELS; do
			pidfile=/var/run/socat/$tun.pid
			[ -f "$pidfile" ] || continue
			pid=`cat "$pidfile"`
			show "Stopping socat tunnel %s" "$tun"; busy
			pkill -g $pid && ok
		done
		rm -f /var/lock/subsys/socat >/dev/null 2>&1
	else
		msg_not_running "socat"
	fi
}

RETVAL=0
# See how we were called.
case "$1" in
  start)
	start
	;;
  stop)
	stop
	;;
  restart)
	stop
	sleep 1
	start
	exit $?
	;;
  status)
  	rclog_warning
	nls "Configured tunnels:"
	echo " $TUNNELS"
	nls "Currently active tunnels:"
	for pidfile in /var/run/socat/*.pid; do
		[ -f "$pidfile" ] || continue
		tun=${pidfile#/var/run/socat/}
		tun=${tun%.pid}
		tunlup $tun && echo -n " $tun($(cat $pidfile))"
	done
	echo ""
	tunlsup
	exit $?
	;;
  *)
	msg_usage "$0 {start|stop|restart|status}"
	exit 3
	;;
esac

exit $RETVAL  


