#! /bin/sh

#
# --- MUST escape all '$' AND all '`' (backticks) AND all '\' ---
#
# in other words: `\' must be used to quote the
#                 characters `\', `$', and ``'
#

cat > _INSTALL_NAME_.boot <<EOF
#!/bin/sh


if test "x_PREFIX_" = "xOPT"; then
   SBINDIR=/opt/_INSTALL_NAME_/bin
else
   SBINDIR=_PREFIX_/sbin
fi

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=\${SBINDIR}/_INSTALL_NAME_
NAME=_INSTALL_NAME_

test -f \$DAEMON || exit 0

case "\$1" in
    start)
        echo -n "Starting service \$NAME"
        ## Start daemon with startproc(8). If this fails
        ## the echo return value is set appropriate.

        start-stop-daemon --stop --quiet --exec \${DAEMON}
	echo "."
        ;;
    stop)
        echo -n "Shutting down service \$NAME"
        ## Stop daemon with killproc(8) 
        start-stop-daemon --stop --quiet --exec \${DAEMON}
        echo "."
        ;;
    restart)
        ## If first returns OK call the second, if first or
        ## second command fails, set echo return value.
        echo -n "Restarting service \$NAME"
        start-stop-daemon --stop --quiet --exec $DAEMON
        sleep 3
        start-stop-daemon --start --quiet --exec $DAEMON
        echo "."
        ;;
    reload|force-reload)
        ## Choose ONE of the following two cases:

        ## First possibility: A few services accepts a signal
        ## to reread the (changed) configuration.

        echo -n "Reload service \$NAME configuration files."
        start-stop-daemon --stop --signal 1 --quiet --exec \$DAEMON
        ;;
    *)
        echo "Usage: \$0 {start|stop|status|restart|reload|force-reload}" >&2
        exit 1
        ;;
esac

exit 0
EOF

  # exit 0   # FIXME

if test -d /etc/init.d && test -d /etc/rc2.d ; then 
    cp _INSTALL_NAME_.boot /etc/init.d/_INSTALL_NAME_;
    chmod 0744 /etc/init.d/_INSTALL_NAME_;
    update-rc.d  _INSTALL_NAME_  defaults 99 10; 
else
 if test -d /etc/init.d  && test -d /etc/init.d/rc2.d; then 
    cp _INSTALL_NAME_.boot /etc/init.d/_INSTALL_NAME_;
    chmod 0744 /etc/init.d/_INSTALL_NAME_;
    (cd /etc/init.d/rc2.d/ && ln -f -s ../_INSTALL_NAME_ S99_INSTALL_NAME_); 
    (cd /etc/init.d/rc2.d/ && ln -f -s ../_INSTALL_NAME_ K10_INSTALL_NAME_); 
    (cd /etc/init.d/rc3.d/ && ln -f -s ../_INSTALL_NAME_ S99_INSTALL_NAME_); 
    (cd /etc/init.d/rc3.d/ && ln -f -s ../_INSTALL_NAME_ K10_INSTALL_NAME_);
    (cd /etc/init.d/rc4.d/ && ln -f -s ../_INSTALL_NAME_ S99_INSTALL_NAME_); 
    (cd /etc/init.d/rc4.d/ && ln -f -s ../_INSTALL_NAME_ K10_INSTALL_NAME_); 
    (cd /etc/init.d/rc5.d/ && ln -f -s ../_INSTALL_NAME_ S99_INSTALL_NAME_); 
    (cd /etc/init.d/rc5.d/ && ln -f -s ../_INSTALL_NAME_ K10_INSTALL_NAME_);
  else
    echo "Could not find an appropriate place for boot script ..."; 
    echo ".. tried /etc/init.d  (Debian), /etc/init.d (LSB)"; 
  fi
fi

### Uncomment this to start the client.
### Note that you need to have everything ready on the server side
### when the client is starting, else it will abort.

# echo '/etc/rc.d/init.d/_INSTALL_NAME_ start' | at now + 1hour 
