#!/bin/bash
#
# Written by Hugo Haas <hugo@debian.org>.
# Modified by Marc Haber <mh+debian-packages@zugschlus.de>.

set -e

[ -n "$IPPLDEBUG" ] && set -x

PATH="/bin:/usr/bin:/sbin:/usr/sbin"

RUNDIR="/var/run/ippl"
PIDFILE="$RUNDIR/ippl.pid"
CONFDIR="/etc"
CONFDDIR="$CONFDIR/ippl.conf.d"
CONFFILE="$RUNDIR/ippl.conf"
IPPLCOMMENTS="no"
IPPL="/usr/sbin/ippl"

test -f $IPPL || exit 0

# run-parts emulation, stolen from Branden's /etc/X11/Xsession
# Addition: Use file.rul instead if file if it exists.
run_parts () {
        # reset LC_COLLATE
        unset LANG LC_COLLATE LC_ALL

        if [ -z "$1" ]; then
                errormessage "$0: internal run_parts called without an argument"
        fi
        if [ ! -d "$1" ]; then
                errormessage "$0: internal run_parts called, but $1 does not exist or is not a directory."
        fi
        for F in $(ls $1); do
                if expr "$F" : '[[:alnum:]_-]\+$' > /dev/null 2>&1; then
                        if [ -f "$1/$F" ] ; then
                                if [ -f "$1/${F}.rul" ] ; then
                                        echo "$1/${F}.rul"
                                else
                                        echo "$1/$F"
                                fi
                        fi
                fi
        done;
}

cat_parts() {
       if [ -z "$1" ]; then
               errormessage "$0: internal cat_parts called without an argument"
       fi
       if [ ! -d "$1" ]; then
               exit 0
       fi
       for file in $(run_parts $1); do
               echo "#####################################################"
               echo "### $file"
               echo "#####################################################"
               cat $file
               echo
               echo "#####################################################"
               echo "### end $file"
               echo "#####################################################"
       done
}

removecomments() {
        if [ "x${IPPLCOMMENTS}" = "xno" ] ; then
                grep -E -v '^[[:space:]]*#' | sed -e '/^$/N;/\n$/D' ;
        else
                cat
        fi
}

# also from Branden
errormessage () {
        # pretty-print messages of arbitrary length (no trailing newline)
        echo "$*" | fold -s -w ${COLUMNS:-80} >&2;
}


update_ippl_conf() {
cat << EOF > ${CONFFILE}.tmp
#########
# WARNING WARNING WARNING
# WARNING WARNING WARNING
# WARNING WARNING WARNING
# WARNING WARNING WARNING
# WARNING WARNING WARNING
# this file is generated dynamically from /etc/ippl/ippl.conf and the files
# in /etc/ippl/ippl.conf.d
# Any changes you make here will be lost.
# WARNING WARNING WARNING
# WARNING WARNING WARNING
# WARNING WARNING WARNING
# WARNING WARNING WARNING
# WARNING WARNING WARNING
#########
EOF

(cat ${CONFDIR}/ippl.conf 2>/dev/null; cat_parts ${CONFDDIR}) | \
  removecomments \
  >> ${CONFFILE}.tmp

# test validity if called without -o
# this is not currently possible with ippl,
# but can be easily enabled with this (of course untested) example code
#if [ "x${CONFFILE}" = "x${AUTOCONFIGFILE}" ] && \
#        [ -x ${IPPL} ] ; then
#        if ! ${IPPL} --config "${CONFFILE}.tmp" > /dev/null ; then
#                errormessage "Invalid new configfile ${CONFFILE}.tmp"
#                errormessage "not installing ${CONFFILE}.tmp to ${CONFFILE}"
#                exit 1
#        fi
#fi

mv -f ${CONFFILE}.tmp ${CONFFILE}
}

case "$1" in
  start)
    echo -n "Starting IP Protocols Logger: ippl"
    update_ippl_conf
    start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $IPPL -- -c /var/run/ippl/ippl.conf
    echo "."
    ;;

  stop)
    echo "Stopping IP Protocols Logger."
    start-stop-daemon --stop --quiet --oknodo --pidfile $PIDFILE
    ;;

  restart)
    $0 stop
    sleep 1
    $0 start
    ;;

  reload|force-reload)
    update_ippl_conf
    start-stop-daemon --stop --quiet --signal 1 --pidfile $PIDFILE
    ;;

  *)
    echo "Usage: /etc/init.d/$0 {start|stop|restart|reload|force-reload}"
    exit 1
    ;;
esac

exit 0
