#!/bin/bash -i

#
# The script creates a wrapper around a SysV-style init script in the guest
# system. The wrapper is installed as a host init script; it delegates all
# start|stop|reload|etc. requests to the guest script which is run under ExaGear.
# This way, services of the ExaGear guest system appear as services of the host
# system and they can be managed from the host with usual
#     service name_of_service start|stop|reload
#
# Copyright (c) 2013 "Elbrus Technologies" LLC. All rights reserved.
#
# This script comes with NO warranty, not even for MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.
#

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

mgmt_script="/etc/init.d/$1"
mgmt_script_src="$etc_src/init.d/$1"
mgmt_script_dst="$etc_dst/init.d/$1"

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

ubt="$ubt_home/$UBT_BINARY"
vpaths_list="$ubt_home/vpaths-list"
opaths_list="$ubt_home/opaths-list"

if ! [ -e "$mgmt_script_src" ]; then
    echo "The service management script '$mgmt_script_src' not found."
    echo "Please check that the name '$1' is spelled correctly."
    exit 1
fi

delegate="$(mktemp)"
chmod 755 $delegate

echo "#! /bin/sh" > $delegate

cat - >> $delegate <<END_OF_PROLOGUE
# This file represents the service $1 in the guest system $image_dir.
# Automatically generated from the file $mgmt_script.
# Do not edit this file.
#
# $EXAGEAR_GUEST_WRAP_MARKER: MARKER OF THE CONVERTED INIT.D SCRIPT (DO NOT EDIT OR REMOVE)
#
END_OF_PROLOGUE

awk -- \
    'BEGIN {copy = 0} /.*BEGIN INIT INFO/ {copy = 1} /.*/ && (copy == 1) { print $0 } /.*END INIT INFO/ {copy = 0}' \
    < $mgmt_script_src >> $delegate

echo >> $delegate
cat - >> $delegate <<END_OF_THE_DELEGATE

image_dir="$image_dir"
ubt_home="$ubt_home"
ubt="\$ubt_home/$UBT_BINARY"
vpaths_list="\$image_dir/.exagear/vpaths-list"
opaths_list="\$image_dir/.exagear/opaths-list"

# Note: the path "$mgmt_script" is not for the host system but for the guest one.
# The script to be called is "$image_dir$mgmt_script".
exec \$ubt --path-prefix \$image_dir --vpaths-list \$vpaths_list --opaths-list \$opaths-list -- $mgmt_script "\$@"
END_OF_THE_DELEGATE

err=

echo "Creating the startup script $mgmt_script_dst..."
mv $delegate $mgmt_script_dst || err="failed to create $mgmt_script_dst"

find ${etc_dst}rc*.d -type l -name "*$1" | xargs rm -f

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 "Creating the startup script $dst..."
        ln -s $mgmt_script_dst $dst || err="failed to create the symlink $dst"
    done
done

if [ -n "$err" ]; then
    echo "Failed to create host startup scripts: $err"
    echo "Please make sure you have write access to /etc/init.d/ and /etc/rcN.d/ directories."
    exit 1
fi

systemctl daemon-reload > /dev/null 2>1

exit 0
