#!/bin/bash
set -euo pipefail

SOURCE=$(cd "$(dirname "$0")" && pwd)
# shellcheck source=openbot-hosted-update
source "$SOURCE/openbot-hosted-update"

[ "$(id -u)" -eq 0 ] || fail "Run this as root."
[ "$#" -ge 2 ] && [ "$#" -le 4 ] || fail "usage: $0 <service-user> <unpacked release> [<account server URL> [<display size>]]"
SERVICE_USER=$1
RELEASE=$2
AUTH_API_URL=${3:-}
DISPLAY_SIZE=${4:-}
installed_user=""
if [ -r "$INSTALL/hosted/mode" ] && [ "$(<"$INSTALL/hosted/mode")" = self ] && [ -r "$INSTALL/hosted/service-user" ]; then
  installed_user=$(<"$INSTALL/hosted/service-user")
fi
if [ -z "$SERVICE_USER" ]; then
  SERVICE_USER=${installed_user:-openbot}
elif [ -n "$installed_user" ] && [ "$SERVICE_USER" != "$installed_user" ]; then
  fail "OpenBot is installed for the user $installed_user. Install again without --user, or with --user $installed_user."
fi

[[ $SERVICE_USER =~ ^[a-zA-Z_][a-zA-Z0-9_-]*$ ]] && [ "$SERVICE_USER" != root ] || fail "The service user is not valid."
[ -x "$RELEASE/openbot" ] || fail "The release has no openbot executable."
if [ -n "$AUTH_API_URL" ]; then
  [[ $AUTH_API_URL =~ ^https://[^/?#[:space:]]+/?$ ]] || fail "The account server URL must be an HTTPS origin."
fi
if [ -n "$DISPLAY_SIZE" ]; then
  [[ $DISPLAY_SIZE =~ ^([1-9][0-9]{2,3})x([1-9][0-9]{2,3})$ ]] &&
    [ "${BASH_REMATCH[1]}" -ge 640 ] && [ "${BASH_REMATCH[1]}" -le 2576 ] &&
    [ "${BASH_REMATCH[2]}" -ge 480 ] && [ "${BASH_REMATCH[2]}" -le 2576 ] ||
    fail "The display size must be WIDTHxHEIGHT from 640x480 to 2576x2576, for example 1920x1080."
fi
if [ -f "$INSTALL/hosted/service-user" ] && ! { [ -r "$INSTALL/hosted/mode" ] && [ "$(<"$INSTALL/hosted/mode")" = self ]; }; then
  fail "This computer is an OpenBot hosted server. A self-hosted install would replace it."
fi
if ! id "$SERVICE_USER" >/dev/null 2>&1; then
  useradd --system --create-home --home-dir "/var/lib/$SERVICE_USER" --shell /bin/bash "$SERVICE_USER"
  echo "Created the user $SERVICE_USER."
fi

exec 9>"$LOCK"
flock 9
install_packages "$SOURCE/packages.txt"
install_optional_packages "$SOURCE/optional-packages.txt"
install -d -m 0755 "$INSTALL" "$INSTALL/hosted"
recover
prepare_release "$RELEASE"
version=$(version_of "$RELEASE")
[ -n "$version" ] || fail "The release has no version."
installed=$(version_of "$INSTALL/app")
if [ -n "$installed" ] && newer "$installed" "$version"; then
  fail "OpenBot $installed is installed, and this release has an older version."
fi
systemctl stop openbot.service 2>/dev/null || true
stage_release "$RELEASE"
swap_in
printf '%s\n' "$SERVICE_USER" >"$INSTALL/hosted/service-user"
printf 'self\n' >"$INSTALL/hosted/mode"
chmod 0644 "$INSTALL/hosted/service-user" "$INSTALL/hosted/mode"
install_hosting "$SOURCE"
finish_swap

install -d -o "$SERVICE_USER" -g "$(id -gn "$SERVICE_USER")" -m 0700 /srv/openbot-hosted
set_server_env() {
  local file="$INSTALL/hosted/openbot.env" temporary
  temporary=$(mktemp "$INSTALL/hosted/.openbot.env.XXXXXX")
  if [ -f "$file" ]; then grep -v "^$1=" "$file" >"$temporary" || true; fi
  printf '%s=%s\n' "$1" "$2" >>"$temporary"
  chmod 0644 "$temporary"
  mv -f "$temporary" "$file"
}
if [ -n "$AUTH_API_URL" ]; then set_server_env OPENBOT_AUTH_API_URL "${AUTH_API_URL%/}"; fi
if [ -n "$DISPLAY_SIZE" ]; then set_server_env OPENBOT_DISPLAY_SIZE "$DISPLAY_SIZE"; fi
flock -u 9
systemctl start openbot.service
systemctl start openbot-update.timer
echo "Installed OpenBot $version. It runs as the user $SERVICE_USER."
