#! /bin/bash

#
# A wrapper script used by upstart configuration files converted
# by add-upstart-service.
#
# When called with no command line arguments, it starts /bin/sh of the guest.
# When started with command line arguments, the first argument is the name of
# a program to run and the following ones are treated as arguments to that
# program. In both cases the script sets the working directory to the root of
# the guest file system.
#
# This script is intended for use only by upstart scripts generated
# by add-upstart-service. Do not use it directly.
#
# 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)

image_dir=$(realpath "$my_dir/..")
ubt_home=$(realpath "$my_dir/../../../bin/")
ubt="$ubt_home/$UBT_BINARY"
vpaths_list="$image_dir/.exagear/vpaths-list"
opaths_list="$image_dir/.exagear/opaths-list"

if [ $# -gt 0 ] ; then
    (
      cd "$image_dir"
      "$ubt" --path-prefix "$image_dir" --vpaths-list "$vpaths_list" --opaths-list "$opaths_list" -- "$@"
    )
    exit $?
fi

(
  cd "$image_dir"
  "$ubt" --path-prefix "$image_dir" --vpaths-list "$vpaths_list" --opaths-list "$opaths_list" -- /bin/sh
)
exit $?
