#! /bin/bash
# (c) by hajtech
# Author ppatzelt@haj.tech

# 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/systemd/system"

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

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

systemctl stop ${BASENAME}
systemctl disable ${BASENAME}

sleep 3

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

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