#!/bin/sh
# Container Manager's docker-project worker starts the project before
# "start" and stops it after "stop" (timing FROM_STARTUP_TO_HALT), so start
# and stop have nothing left to do. The package user has no Docker socket,
# so "status" asks the Relay itself: any HTTP answer on its listener means
# it is running.
# relay-start.sh writes the Relay's HTTP port here on every container start,
# in the package's private folder, so this script needs no YAML parsing;
# 28081 (the seeded default) until it has.
PORT_FILE="/var/packages/${SYNOPKG_PKGNAME:-ViaalinkRelay}/var/http-port"

http_port() {
    port="$(head -n 1 "$PORT_FILE" 2>/dev/null | tr -cd '0-9')"
    echo "${port:-28081}"
}

case "$1" in
    start|stop|prestart|prestop)
        exit 0
        ;;
    status)
        if curl -s -o /dev/null -m 3 "http://127.0.0.1:$(http_port)/"; then
            exit 0
        fi
        exit 3
        ;;
    log)
        echo ""
        exit 0
        ;;
    *)
        echo "Usage: $0 {start|stop|status}" >&2
        exit 1
        ;;
esac
