#!/bin/bash -i

#
# This script removes a service of the ExaGear guest from the list of host services.
#
# The script undoes changes made by add-guest-service.
#
# Copyright (c) 2013 "Elbrus Technologies" LLC. All rights reserved.
#
# This script comes NO warranty, not even for MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.
#

if [ -e "$1" ]; then
    echo "Use"
    echo "    $0 name-of-guest-service"
    echo "to remove a guest service from the host's init scripts."
    exit 1
fi

. $EXAGEAR_GUEST_CONFFILE

if [ $DELEGATE_RC_SCRIPTS_AUTOMATICALLY == "Y" ]; then
    echo "The guest image $EXAGEAR_IMAGE_NAME uses automatic integration of the startup"
    echo "and shutdown mechanisms of the host and guest systems. If you want to use"
    echo "this script to enable the autostart of individual guest services, please"
    echo "first disable the automatic integration. You can do this by editing the file"
    echo "    $EXAGEAR_GUEST_CONFFILE"
    exit 0
fi

my_dir=$(dirname $0)
etc_src=$(realpath "$my_dir/../etc/")
etc_dst="/etc/"

mgmt_script_dst="$etc_dst/init.d/$1"

image_dir=$(realpath "$my_dir/..")

echo "Removing the startup script $mgmt_script_dst..."
rm -f $mgmt_script_dst

for rcdir in $(find $etc_src -type d -name 'rc*.d' -printf "%P\n"); do
    for script in $(find $etc_src/$rcdir/ -type l -name "*$1" -printf "%P\n"); do
        dst="$etc_dst/$rcdir/${script}"
        echo "Removing the startup script $dst..."
        rm -f $dst
    done
done

exit 0
