#!/bin/bash
#
# Disable TinyPilot WiFi access point.

# Exit on first failure.
set -e

# Echo commands before executing them, by default to stderr.
set -x

# Exit on unset variable.
set -u

if (( "${EUID}" != 0 )); then
  >&2 echo 'This script requires root privileges.'
  >&2 echo 'Please re-run with sudo:'
  >&2 echo "  sudo $0 $*"
  >&2 exit 1
fi

print_help() {
  cat <<EOF
Usage: ${0##*/} [--help]
Disable TinyPilot WiFi access point.
  --help  Display this help and exit.

Note: Running this script will clear any potentially existing custom static IP
      configuration and also any potentially existing WiFi settings that might
      have been set up previously.

EOF
}

# Parse command-line arguments.
while (( "$#" > 0 )); do
  case "$1" in
    --help)
      print_help
      exit
      ;;
    *)
      >&2 echo "Unknown agrument: $1"
      >&2 print_help
      exit 1
      ;;
  esac
done

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" &> /dev/null && pwd)"
readonly SCRIPT_DIR

# Delegate to disable-wifi — the disable action is identical regardless of
# whether the connection is in client or AP mode.
"${SCRIPT_DIR}/disable-wifi"

echo 'TinyPilot has disabled its WiFi access point.'
