#!/usr/bin/env bash
#
# Create a fly.toml file to deploy PicoShare to fly.io.

# Exit on first error.
set -e

# Treat undefined environment variables as errors.
set -u

print_help() {
  cat << EOF
Usage: ${0##*/} [-h] app_name
  Creates a fly.toml config file.
  app_name: Name of fly.io instance to use.
  -h Display this help and exit.
EOF
}

# Parse command-line arguments.
while getopts 'h' opt; do
  case "${opt}" in
    h)
      print_help
      exit
      ;;
    *)
      print_help >&2
      exit 1
  esac
done

# Ensure 'app_name' is given.
shift $((OPTIND - 1))
if (( $# == 0 )); then
  echo 'Input parameter missing: app_name' >&2
  exit 1
fi
readonly APP_NAME="$1"

cat > fly.toml <<EOF
app = "${APP_NAME}"

kill_signal = "SIGINT"
kill_timeout = 5
processes = []

[env]
  PORT = "8080"
  PS_BEHIND_PROXY="yes"

[experimental]
  allowed_public_ports = []
  auto_rollback = true

[[services]]
  http_checks = []
  internal_port = 8080
  processes = ["app"]
  protocol = "tcp"
  script_checks = []

  [services.concurrency]
    hard_limit = 25
    soft_limit = 20
    type = "connections"

  [[services.ports]]
    handlers = ["http"]
    port = 80

  [[services.ports]]
    handlers = ["tls", "http"]
    port = 443

  [[services.tcp_checks]]
    grace_period = "1s"
    interval = "15s"
    restart_limit = 0
    timeout = "2s"
EOF
