#!/bin/bash
#
# Copyright (C) 2016 Canonical, Ltd.
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License version 3, as published
# by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranties of
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
# PURPOSE.  See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# Authors:
#     Xavi Garcia <xavi.garcia.mena@canonical.com>
#

# Set the APP_EXEC environment variable
initctl set-env "APP_EXEC=$APP_URIS"
# remove the ' from the APP_URIS variable
CMD=`echo "${APP_URIS//\'}"`

# covert CMD to an array
IFS=' ' read -r -a URIS_ARRAY <<< "${CMD}"

if [ ${#URIS_ARRAY[@]} -eq 2 ]; then
    # cd to the directory
    cd "${URIS_ARRAY[1]}"
fi

# Launch the command
eval ${URIS_ARRAY[0]}
