#!/usr/bin/env bash
# Fix builder nginx on sshfs mounts + optional reload on hosts.
set -euo pipefail
REPO_ROOT="$(cd "$(dirname "$0")/../../../.." && pwd)"
BE_NGINX="${BE_NGINX:-$REPO_ROOT/tmp/BE_alpine/etc/nginx/conf.d}"
FE_NGINX="${FE_NGINX:-$REPO_ROOT/tmp/FE_gentoo/etc/nginx}"
EXAMPLES="$REPO_ROOT/examples"

install -D -m 0644 "$EXAMPLES/crash_reporter/backend/nginx.apps-builder.frag" \
  "$BE_NGINX/apps_builder.frag"
install -D -m 0644 "$EXAMPLES/crash_reporter/backend/nginx.fe-apps.conf.fragment" \
  "$FE_NGINX/apps.conf"

if [[ "${1:-}" == "--port80" ]]; then
  "$EXAMPLES/crash_reporter/backend/scripts/apply-be-port80-nginx.sh" \
    "$BE_NGINX/apps.conf"
fi

echo "Installed:"
echo "  $BE_NGINX/apps_builder.frag"
echo "  $FE_NGINX/apps.conf"
echo
echo "Reload on hosts:"
echo "  ssh alpine-be 'sudo nginx -t && sudo rc-service nginx reload'"
echo "  # Gentoo FE: sudo nginx -t && sudo rc-service nginx reload"
echo
echo "Verify:"
echo "  curl -sSI 'https://apps.f0xx.org/app/androidcast_project/build/' | grep -E 'HTTP|location|x-powered-by'"
