#!/bin/sh

status-set maintenance "Started" || true
HTTP_SERVER_SCRIPT="${JUJU_CHARM_DIR}/scripts/simple-server.py"

RES_NAME="index"
RES_PATH=$(resource-get $RES_NAME)
DEFAULT_FILE="${JUJU_CHARM_DIR}/files/default-file.txt"

INDEX_FILE=${RES_PATH:-${DEFAULT_FILE}}

juju-log -l INFO "Stopping any existing simple servers."
# using 'grep hack' to not catch grep process in the list.
kill $(ps aux | grep [s]imple-server.py | awk '{print $2}') || true

juju-log -l INFO "Serving file: ${INDEX_FILE}"

nohup $HTTP_SERVER_SCRIPT --file-path "${INDEX_FILE}" --port 80 >> /tmp/server-output.log 2>&1 &
open-port 80

status-set active "Started" || true
