#!/bin/sh
#
# autosshd      autossh system service
#
# chkconfig: 2345 30 60
#
# description: autosshd run autossh as system service
#

processname="autossh"
startname="/usr/share/autosshd/autossh-conf" #need for export var, because start shell by root and run autossh by _autossh user
# processname: $processname
#

# Do not load RH compatibility interface.
WITHOUT_RC_COMPAT=1

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

# Source networking configuration.
SourceIfNotEmpty /etc/sysconfig/network

# AutoSSHDaemon configuration
SYSCONFIGFILE="/etc/sysconfig/autosshd"
SourceIfNotEmpty $SYSCONFIGFILE
# Load AutoSSHD variables and init_autossh function
SourceIfNotEmpty "$startname"


###========= Functions =========
#===== help message =====
help()
{
    echo "
Usage: ${0##*/} ACTION [CHANNEL]
ACTION  - requested action of all or given AutoSSH channel
CHANNEL - optional name of AutoSSH channel (with corresponding 
          CHANNEL.* files in $CONFIGDIR)

Usefull actions is:
start       - start all configured channels except listed 
              in AUTOSTART variable in $CONFIGDIR/CHANNEL.conf file
stop        - stop all running channels
restart     - restart channels that are configured for autostart 
reload      - restart only running channels
list        - print list of configured channels
status      - print status of running channels
help        - print this message.
testssh     - test connection configuration by ssh.

"
    RETVAL=1
    return $RETVAL
}

do_run_one()
{
    if [ -f $1 ]; then
	SourceIfNotEmpty $1

	# skip if in AUTOSTART mode
	is_yes "$AUTOSTART" || return 0
	
	[ -e "$AUTOSSH_LOCKFILE" ] && echo "autossh to $(basename $1 .conf) service is already running." && return 1
	
        start_daemon --lockfile "$AUTOSSH_LOCKFILE" \
            --user $AUTOSSHUSER --displayname "autossh to ${HOST}" -- $startname $1 "${AUTOSSH_OPTIONS}"
        RETVAL=$?
        sleep 1
        return $RETVAL
    else
	echo "Configuration file $1 not found"
	RETVAL=1
        return $RETVAL
    fi
}

do_stop_one()
{
    if [ -f $1 ]; then
	SourceIfNotEmpty $1
	if [ -s $AUTOSSH_PIDFILE ]; then
	    stop_daemon --pidfile "$AUTOSSH_PIDFILE" --lockfile "$AUTOSSH_LOCKFILE" \
	        --expect-user $AUTOSSHUSER --name $processname --displayname "autossh to ${HOST}" -- $processname
	    RETVAL=$?
	    return $RETVAL
	else
	    msg_not_running "autossh for `basename $1 .conf`"
	    echo
	fi
    else
	echo "Configuration file $1 not found"
	RETVAL=1
        return $RETVAL
    fi
}


show_status()
{
        RETVAL=0
        [ -n "$CHANNELS" ] || running_channels

        if [ -z "$CHANNELS" ]; then
                msg_not_running autossh
                echo
                RETVAL=1
        else
                for CHANNEL in $CHANNELS; do
	            init_autossh $config
                    status --pidfile "$PIDFILEDIR/$CHANNEL.pid" --lockfile "$LOCKFILEDIR/$CHANNEL.lck" \
                    --expect-user $AUTOSSHUSER --name $processname --displayname "autossh to $CHANNEL" -- $processname
                    RETVAL=$(( $RETVAL + $? ))
                done
        fi
        return $RETVAL
}

start()
{
    # run only listed channels
    if [ -n "$CHANNELS" ] ; then
        for i in $CHANNELS; do
            init_autossh $config
            do_run_one $CONFIGDIR/$i.conf
        done
        return 0
    fi

    # run all scheduled channels
    CHANNELS=$(get_channels_list)
    [ -z "$CHANNELS" ] && echo "Nothing to run. Put config in $CONFIGDIR/" && return 1
    for config in $CHANNELS; do
        init_autossh $config
        do_run_one $config
    done
    return 0
}

stop()
{
    [ -n "$CHANNELS" ] || running_channels

    for config in $CHANNELS; do
        SourceIfNotEmpty $config
        init_autossh $config
        do_stop_one $CONFIGDIR/$config.conf
    done
    return 0
}

get_channels_list()
{
    /bin/ls $CONFIGDIR/*.conf 2>/dev/null #returns only filenames
}


list_channels()
{
    LIST=$(get_channels_list)
    echo "Configured channels:"
        for config in $LIST; do
	    SourceIfNotEmpty $config
	    echo "   `basename $config .conf`. AUTOSTART: ${AUTOSTART} "
        done
    return 0
}

running_channels()
{
    LIST=`/bin/ls $PIDFILEDIR/*.pid 2>/dev/null`
    CHANNELS=""
    for proc in $LIST; do
        CHANNELS="$CHANNELS`basename $proc .pid` "
    done
    return 0
}

###====== Main =====
RETVAL=0

OP=$1
shift
CHANNELS="$@"

is_yes "$NETWORKING" || return 0

# See how we were called.
case "$OP" in
        start)
            start
            ;;
        stop|condstop)
            stop
            ;;
        reload|restart)
            stop
            sleep 2
            start
            ;;
	condrestart)
	    running_channels
	    if [ -n $CHANNELS ] ; then
                stop
                sleep 2
                start
	    fi
	    ;;
        status)
            show_status
            ;;
        list)
            list_channels
            ;;
        help)
            help
            ;;
        testssh)
	    su - -s /bin/sh -c "autosshd-ssh $1" _autossh
            ;;
        *)
                msg_usage "${0##*/} {start|stop|reload|condrestart|condstop|restart|status|list|help|testssh}"
                RETVAL=1
esac

exit $RETVAL
