#! /bin/bash -i

#
# The script creates a wrapper around an upstart init script in the guest
# system. The wrapper is installed as a host init script. The wrapper calls
# guest's /bin/sh and feeds it with the script contained in the original
# upstart configuration file. 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
#     initctl start|stop service_name
#
# 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.
#

my_dir=$(dirname $0)

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

confdir_src="$image_dir/etc/init"
confdir_dst="/etc/init"

descriptor_src="${confdir_src}/$1.conf"
descriptor_dst="${confdir_dst}/$1.conf"

if ! [ -e "$descriptor_src" ] ; then
    echo "The service configuration file $descriptor_src does not exist."
    echo "Make sure that '$1' is spelled correctly."
    exit 1
fi

if [ -e "$descriptor_dst" ] ; then
    echo "The host system has the configuration file $descriptor_src."
    echo "It is not possible to have identically named upstart services"
    echo "in the host and guest systems".
    echo
    echo "If you do not use the service $1 on the host, you can uninstall it"
    echo "and then rerun this script to export $1 from the guest system."
    echo
    echo "Please see the user's manual to learn about the limitations of"
    echo "the guest and host integration mechanisms."
    exit 1
fi

transformed=$(mktemp)

exec_wrapper="$image_dir/.exagear/upstart_wrapper"
script_wrapper_start="$image_dir/.exagear/upstart_wrapper << '__WRAPPED_FOR_EXAGEAR'"
script_wrapper_end="__WRAPPED_FOR_EXAGEAR"

cat - > "$transformed" <<END_OF_PROLOGUE
# This file represents the service $1 in the guest system $image_dir.
# Automatically generated from the file $descriptor_src.
# Do not edit this file.
#
# $EXAGEAR_GUEST_WRAP_MARKER: MARKER OF THE CONVERTED CONFIGURATION FILE (DO NOT EDIT OR REMOVE)
#

END_OF_PROLOGUE

cat "$descriptor_src" \
  | awk -- \
    " \
      BEGIN { in_script=0 }                                                                                        \
      /^[ \t]*script[ \t]*\$/                 { print \$0 ; print \"$script_wrapper_start\" ; in_script=1 ; next } \
      /^[ \t]*pre-start[ \t]*script[ \t]*\$/  { print \$0 ; print \"$script_wrapper_start\" ; in_script=1 ; next } \
      /^[ \t]*post-start[ \t]*script[ \t]*\$/ { print \$0 ; print \"$script_wrapper_start\" ; in_script=1 ; next } \
      /^[ \t]*pre-stop[ \t]*script[ \t]*\$/   { print \$0 ; print \"$script_wrapper_start\" ; in_script=1 ; next } \
      /^[ \t]*post-stop[ \t]*script[ \t]*\$/  { print \$0 ; print \"$script_wrapper_start\" ; in_script=1 ; next } \
      /^[ \t]*end[ \t]+script[ \t]*\$/        { print \"$script_wrapper_end\" ; print \$0 ; in_script=0 ; next }   \
      (in_script == 1)                        { print \$0 ; next }                                                 \
      (in_script == 0)                        {                                                                    \
          sub(\"^[ \t]*exec[ \t]*\", \"exec $exec_wrapper \") ;                                                    \
          sub(\"^[ \t]*pre-start[ \t]+exec[ \t]*\", \"pre-start exec $exec_wrapper \") ;                           \
          sub(\"^[ \t]*post-start[ \t]+exec[ \t]*\", \"post-start exec $exec_wrapper \") ;                         \
          sub(\"^[ \t]*pre-stop[ \t]+exec[ \t]*\", \"pre-stop exec $exec_wrapper \") ;                             \
          sub(\"^[ \t]*post-stop[ \t]+exec[ \t]*\", \"post-stop exec $exec_wrapper \") ;                           \
          print \$0 ; \
          next \
      } \
    " \
  >> "$transformed"

install -m 0666 "$transformed" "$descriptor_dst"
