#!/bin/sh
# Starts the modem power-cycle web trigger, then the NMEA server (run.sh).
# The router starts it as: start.sh serve <password> <lte-interface>

# ---- Setting ----
CYCLETOKEN="ChangeMeToALongRandomString" # secret part of the trigger URL, letters and digits only
# -----------------

# Remove Windows line endings, then run the cleaned copy. #
mkdir -p /tmp #
[ -z "$START_CLEAN" ] && tr -d '\r' < "$0" > /tmp/start.sh && export START_CLEAN=1 && exec /bin/sh /tmp/start.sh "$@" #

# The trigger reads the router API address from here. /tmp is inside the container only.
printf 'API=%s\n' "http://nmea:$2@172.17.0.1/rest" > /tmp/cycle.env
mkdir -p /tmp/www/cgi-bin
tr -d '\r' < /nmea/cycle.cgi > "/tmp/www/cgi-bin/$CYCLETOKEN"
chmod 755 "/tmp/www/cgi-bin/$CYCLETOKEN"
httpd -p 8080 -h /tmp/www
exec /bin/sh /nmea/run.sh "$@"
