#!/usr/bin/env bash

set -euo pipefail

TARGET="${1:-status}"
FLAG_PATH="${MAINTENANCE_FLAG_PATH:-$(pwd)/uploads/system/maintenance.flag}"

mkdir -p "$(dirname "$FLAG_PATH")"

case "$TARGET" in
  on|enable)
    touch "$FLAG_PATH"
    echo "maintenance=on ($FLAG_PATH)"
    ;;
  off|disable)
    rm -f "$FLAG_PATH"
    echo "maintenance=off ($FLAG_PATH)"
    ;;
  status)
    if [[ -f "$FLAG_PATH" ]]; then
      echo "maintenance=on ($FLAG_PATH)"
    else
      echo "maintenance=off ($FLAG_PATH)"
    fi
    ;;
  *)
    echo "Usage: ./scripts/maintenance-flag.sh [on|off|status]" >&2
    exit 1
    ;;
esac
