#!/bin/sh
#
# This script can be used to develop and test the frontend without having to
# link the build in a running core instance through the frontend/development_repo setting.
#
# WARNING:
# If you have an active login session in the frontend. The core that was used
# as a backend during the time of the login remains used until you logout again.
# So if you reuse the url hosting the frontend, you will need to logout before
# it will actually start using the core backend configured by this script.
#
# If you run this script without parameters, the frontend will be accessible under http://localhost:8124.
# And it will use the core instance running under http://localhost:8123 as a backend.
# Note that from a devcontainer, the frontend will be accessible under port 8124 on the host container.
# Inside the devcontainer it will be accessible under port 8123 instead.
# The core instance endpoint remains the same in both cases, as this is resolved from the browser.
#
# You can change the core instance the frontend connects to by passing the -c option.
# For example: script/develop_and_serve -c https://myhost.duckdns.org:8123
# This will also work for existing production core instances.
# It does not need to be a development version hosted locally.
#
# You can change the port the frontend is served on by passing the -p option.
# For example: script/develop_and_serve -p 8654
# Note that if you are running from a devcontainer, you will need to setup
# port forwarding as well if you want to access it from the container host.

# Stop on errors
set -e

cd "$(dirname "$0")/.."

# parse input parameters
if [ -n "$DEVCONTAINER" ]; then
    frontendPort=8123
else
    frontendPort=8124
fi

coreUrl=http://localhost:8123

while getopts p:c:h flag
do
    case "${flag}" in
        p) frontendPort=${OPTARG};;
        c) coreUrl="${OPTARG}";;
        h) echo Documentation can be found inside "$0" && exit 0;;
        *) echo Documentation can be found inside "$0" && exit 1;;
    esac
done

# display used settings
if [ -n "$DEVCONTAINER" ]; then
    echo Frontend is available inside container as http://localhost:${frontendPort}
    if [ 8123 -eq $frontendPort ]; then
      echo Frontend is available on container host as http://localhost:8124
    fi
else
    echo Frontend is hosted on http://localhost:${frontendPort}
fi
echo Core is used from ${coreUrl}

# build the frontend so it connects to the passed core
HASS_URL="$coreUrl" ./node_modules/.bin/gulp develop-app &
develop_pid=$!

# serve the frontend
./node_modules/.bin/serve -p $frontendPort --single --no-port-switching --config ../script/serve-config.json ./hass_frontend &
serve_pid=$!

stop_children() {
    trap - EXIT INT TERM HUP
    kill "$develop_pid" "$serve_pid" 2>/dev/null || true
    wait "$develop_pid" 2>/dev/null || true
    wait "$serve_pid" 2>/dev/null || true
}

trap stop_children EXIT
trap 'stop_children; exit 130' INT
trap 'stop_children; exit 143' TERM
trap 'stop_children; exit 129' HUP

while kill -0 "$develop_pid" 2>/dev/null && kill -0 "$serve_pid" 2>/dev/null; do
    sleep 1
done

develop_status=
serve_status=
if ! kill -0 "$develop_pid" 2>/dev/null; then
    if wait "$develop_pid"; then develop_status=0; else develop_status=$?; fi
fi
if ! kill -0 "$serve_pid" 2>/dev/null; then
    if wait "$serve_pid"; then serve_status=0; else serve_status=$?; fi
fi

if [ -n "$develop_status" ] && [ "$develop_status" -ne 0 ]; then
    status=$develop_status
elif [ -n "$serve_status" ]; then
    status=$serve_status
else
    status=$develop_status
fi

exit "$status"
