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

. /etc/rc.config

# Determine the base and follow a runlevel link name.
base=\${0##*/}
link=\${base#*[SK][0-9][0-9]}

# Force execution if not called by a runlevel directory.
test \$link = \$base && START__INSTALL_NAME_=yes

if test "x\$START__INSTALL_NAME_" != "x"; then
  test "\$START__INSTALL_NAME_" = yes || exit 0
fi

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

# The echo return value for success (defined in /etc/rc.config).
return=\$rc_done
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.

        startproc \${SBINDIR}/_INSTALL_NAME_ || return=\$rc_failed

        echo -e "\$return"
        ;;
    stop)
        echo -n "Shutting down service _INSTALL_NAME_"
        ## Stop daemon with killproc(8) and if this fails
        ## set echo the echo return value.

        killproc -TERM \${SBINDIR}/_INSTALL_NAME_ || return=\$rc_failed

        echo -e "\$return"
        ;;
    restart)
        ## If first returns OK call the second, if first or
        ## second command fails, set echo return value.
        \$0 stop  &&  \$0 start  ||  return=\$rc_failed
        ;;
    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 -HUP \${SBINDIR}/_INSTALL_NAME_ || return=\$rc_failed
        echo -e "\$return"

        ## Exclusive possibility: Some services must be stopped
        ## and started to force a new load of the configuration.

        ## \$0 stop  &&  \$0 start  ||  return=\$rc_failed
        ;;
    status)
        echo -n "Checking for service _INSTALL_NAME_: "
        ## Check status with checkproc(8), if process is running
        ## checkproc will return with exit status 0.

        checkproc \${SBINDIR}/_INSTALL_NAME_ && echo OK || echo No process
        ;;
    probe)
        ## Optional: Probe for the necessity of a reload,
        ## give out the argument which is required for a reload.

        #test /etc/_INSTALL_NAME_.conf -nt /var/run/_INSTALL_NAME_.pid && echo reload
        ;;
    *)
        echo "Usage: \$0 {start|stop|status|restart|reload[|probe]}"
        exit 1
        ;;
esac

# Inform the caller not only verbosely and set an exit status.
test "\$return" = "\$rc_done" || exit 1
exit 0
EOF

  # exit 0   # FIXME


if test -d /etc/init.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
  if test -d /sbin/init.d ; then 
    cp _INSTALL_NAME_.boot /sbin/init.d/_INSTALL_NAME_;
    chmod 0744 /sbin/init.d/_INSTALL_NAME_;
    (cd /sbin/init.d/rc2.d/ && ln -f -s ../_INSTALL_NAME_ S99_INSTALL_NAME_); 
    (cd /sbin/init.d/rc2.d/ && ln -f -s ../_INSTALL_NAME_ K10_INSTALL_NAME_); 
    (cd /sbin/init.d/rc3.d/ && ln -f -s ../_INSTALL_NAME_ S99_INSTALL_NAME_); 
    (cd /sbin/init.d/rc3.d/ && ln -f -s ../_INSTALL_NAME_ K10_INSTALL_NAME_);
    (cd /sbin/init.d/rc4.d/ && ln -f -s ../_INSTALL_NAME_ S99_INSTALL_NAME_); 
    (cd /sbin/init.d/rc4.d/ && ln -f -s ../_INSTALL_NAME_ K10_INSTALL_NAME_); 
    (cd /sbin/init.d/rc5.d/ && ln -f -s ../_INSTALL_NAME_ S99_INSTALL_NAME_); 
    (cd /sbin/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 /sbin/init.d  (SuSE 6.x), /etc/init.d (SuSE 7.x)"; 
  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 '/sbin/init.d/_INSTALL_NAME_ start' | at now + 1hour 
