#!/bin/sh
### BEGIN INIT INFO
# Provides:          fts
# Required-Start:    $local_fs $remote_fs $network
# Required-Stop:     $local_fs $remote_fs $network
# Should-Start:	     $syslog
# Should-Stop:       $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Modular TFTP/Fuse supplicant
# Description:       Start FTS service
### END INIT INFO

set -e

PATH=/sbin:/bin:/usr/sbin:/usr/bin
DESC="TFTP supplicant"
SERVICE="fts"
SCRIPTNAME=/etc/init.d/fts
CONFIG=/etc/fts/config
DAEMON_USER=


mount_path() {
	sed -e 's/[[:space:]]*\=[[:space:]]*/=/g' \
            -e 's/;.*$//' \
            -e 's/[[:space:]]*$//' \
            -e 's/^[[:space:]]*//' \
            -e "s/^\(.*\)=\([^\"']*\)$/\1=\"\2\"/" $CONFIG | \
	    sed -n -e "/^\[[Tt][Ff][Tt][Pp]\]/,/^\s*\[/{/^[^;].*\=.*/p;}" | \
	    sed -n -e 's/^path="\(.*\)"$/\1/p'
}


start_service() {
	if LC_ALL=C mount | grep -q "^$SERVICE on"; then
            log_progress_msg "already running"
            return
        fi

	if ! test -z "$DAEMON_USER"; then
            start-stop-daemon --start --background --quiet --chuid $DAEMON_USER \
		--exec /usr/sbin/$SERVICE -- --config $CONFIG
        else
            start-stop-daemon --start --quiet --background \
		--exec /usr/sbin/$SERVICE -- --config $CONFIG
        fi
}


stop_service() {
	if LC_ALL=C mount | grep -q "^$SERVICE on"; then
                if [ "$(uname)" = Linux ]; then
			fusermount -u "$(mount_path)"
                elif [ "$(uname)" = GNU/kFreeBSD ]; then
                        umount -u $(mount_path)
                else
                        echo "FTS does not know how to unmount for this architecture." >&2
                        exit 1
                fi
	fi
}


# Gracefully exit if the package has been removed.
test -x /usr/sbin/$SERVICE || exit 0

. /lib/lsb/init-functions

# Include defaults if available.
test -f /etc/default/$SERVICE && . /etc/default/$SERVICE

case "$1" in
    start)
        # Warn if there's an old configuration around
	[ -e /etc/fts/fts.conf ] && echo "WARNING: there's an old fts.conf file around - see README.Debian"

        # If we've no configuration now, stop more or less silently
        if [ ! -e $CONFIG ]; then
            echo ""
            echo "FTS is not configured: please create $CONFIG to enable"
            exit 0
        fi

	if ! test -z "$DAEMON_USER"; then
            if ! getent group fuse | grep -q "[,:]$DAEMON_USER\(,\|$\)"; then
                echo ""
                echo "FTS is not configured: please add the user $DAEMON_USER to the fuse group"
                exit 0
            fi
	    if ! grep -q '^[^#]*\s*user_allow_other\s*$' /etc/fuse.conf; then
                echo ""
                echo "FTS is not configured: please add the 'user_allow_other' option to /etc/fuse.conf"
                exit 0
	    fi
	fi

        log_daemon_msg "Starting $DESC"
        log_progress_msg "$SERVICE"
        start_service
        log_end_msg $?
        ;;

    stop)
        if [ ! -e $CONFIG ]; then
            echo ""
            echo "FTS is not configured, please create $CONFIG to enable"
            exit 0
        fi

        log_daemon_msg "Stopping $DESC"
        stop_service
        log_progress_msg "$SERVICE"
        log_end_msg $?
        ;;

    restart|force-reload)
        log_daemon_msg "Restarting $DESC"
        log_progress_msg "$SERVICE"
        stop_service
        start_service
        log_end_msg $?
        ;;

    *)
        echo "Usage: $SCRIPTNAME {start|stop|restart|force-reload}" >&2
        exit 1
        ;;
esac

exit 0
