#! /bin/bash
# (c) by ennit interactive GmbH, 2015
# Author jbradler@ennit.com

# Include defaults if available
if [ -f /etc/default/hosting-tools ]; then
    . /etc/default/hosting-tools
fi

if [[ $# -eq 0 ]] ; then
    echo "Missing argument. Source file name for service start script is missing. Aborting."
    exit 1
fi

ORIGIN=$1
BASENAME=`basename $ORIGIN`
TARGET_DIR="/etc/init.d/$BASENAME"

if [ "" == "${BASENAME}" ]; then
    echo "  Could not find service start script. Aborting."
    exit 1
fi

if [ ! -f "${TARGET_DIR}/${BASENAME}" ]; then
    echo "  Service has been removed already. Aborting."
    exit 1
fi

update-rc.d ${BASENAME} disable
service ${BASENAME} stop

sleep 1

rm -f ${TARGET_DIR}/${BASENAME} 2> /dev/null

if [ $? -eq 0 ]; then
    exit 0
else
    echo "  Could not remove service start script ..."
    echo "  from ${TARGET_DIR}/${BASENAME}"
    exit 1
fi