#! /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

#
# chkconfig: 2345 99 10
# description: _INSTALL_NAME_ keeps a eye on your stuff


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

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

RETVAL=0

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

        daemon \${SBINDIR}/_INSTALL_NAME_
	RETVAL=\$?
	[ \$RETVAL -eq 0 ] && touch /var/lock/subsys/_INSTALL_NAME_
	echo
        ;;
    stop)
        echo -n \$"Stopping service _INSTALL_NAME_"
        ## Stop daemon with killproc(8) 
        killproc _INSTALL_NAME_ 
	rm -f /var/lock/subsys/_INSTALL_NAME_
        echo
        ;;
    restart)
        ## If first returns OK call the second, if first or
        ## second command fails, set echo return value.
        \$0 stop
	sleep 3
        \$0 start
        ;;
    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 _INSTALL_NAME_"
        killproc _INSTALL_NAME_ -HUP
        echo 
        ;;
    status)
        # echo -n "Checking for service _INSTALL_NAME_: "
        ## Check status with checkproc(8), if process is running
        ## checkproc will return with exit status 0.

        status _INSTALL_NAME_
	exit \$?
        ;;
    *)
        echo \$"Usage: \$0 {start|stop|status|restart|reload}"
        exit 1
        ;;
esac

exit \$RETVAL
EOF

  # exit 0   # FIXME

if test -d /etc/rc.d/init.d && test -d /etc/rc.d/rc2.d; then
    cp _INSTALL_NAME_.boot /etc/rc.d/init.d/_INSTALL_NAME_;
    chmod 0744 /etc/rc.d/init.d/_INSTALL_NAME_;
    (cd /etc/rc.d/rc2.d/ && ln -f -s ../init.d/_INSTALL_NAME_ S99_INSTALL_NAME_); 
    (cd /etc/rc.d/rc2.d/ && ln -f -s ../init.d/_INSTALL_NAME_ K10_INSTALL_NAME_); 
    (cd /etc/rc.d/rc3.d/ && ln -f -s ../init.d/_INSTALL_NAME_ S99_INSTALL_NAME_); 
    (cd /etc/rc.d/rc3.d/ && ln -f -s ../init.d/_INSTALL_NAME_ K10_INSTALL_NAME_);
    (cd /etc/rc.d/rc4.d/ && ln -f -s ../init.d/_INSTALL_NAME_ S99_INSTALL_NAME_); 
    (cd /etc/rc.d/rc4.d/ && ln -f -s ../init.d/_INSTALL_NAME_ K10_INSTALL_NAME_); 
    (cd /etc/rc.d/rc5.d/ && ln -f -s ../init.d/_INSTALL_NAME_ S99_INSTALL_NAME_); 
    (cd /etc/rc.d/rc5.d/ && ln -f -s ../init.d/_INSTALL_NAME_ K10_INSTALL_NAME_);

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/rc.d/init.d  (RedHat), /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 
