#!/usr/bin/env bash
set -euo pipefail

root=@@PROVIDER_ROOT@@
state=@@STATE_ROOT@@
key_dir="$state/host-ssh"
log_dir="$state/logs/provider-@@PROVIDER_ID@@"
env_file="$state/provider-@@PROVIDER_ID@@.env"
unit=/etc/systemd/system/unidesk-provider-gateway-@@PROVIDER_ID@@.service
image=@@PROVIDER_IMAGE@@
container=@@PROVIDER_CONTAINER@@

install -d -m 0755 -o @@UBUNTU_USER@@ -g @@UBUNTU_USER@@ "$state" "$log_dir"
install -d -m 0700 -o @@UBUNTU_USER@@ -g @@UBUNTU_USER@@ "$key_dir" /home/@@UBUNTU_USER@@/.ssh

if [[ ! -s "$key_dir/id_ed25519" ]]; then
  sudo -u @@UBUNTU_USER@@ ssh-keygen -q -t ed25519 -N '' -C unidesk-@@PROVIDER_ID@@-host -f "$key_dir/id_ed25519"
fi
touch /home/@@UBUNTU_USER@@/.ssh/authorized_keys
chown @@UBUNTU_USER@@:@@UBUNTU_USER@@ /home/@@UBUNTU_USER@@/.ssh/authorized_keys
chmod 0600 /home/@@UBUNTU_USER@@/.ssh/authorized_keys
public_key=$(<"$key_dir/id_ed25519.pub")
grep -Fqx "$public_key" /home/@@UBUNTU_USER@@/.ssh/authorized_keys || printf '%s\n' "$public_key" >>/home/@@UBUNTU_USER@@/.ssh/authorized_keys

cat >"$env_file" <<'EOF'
UNIDESK_MASTER_SERVER=@@MASTER_SERVER@@
PROVIDER_ID=@@PROVIDER_ID@@
PROVIDER_NAME=@@PROVIDER_NAME@@
PROVIDER_UPGRADE_HOST_PROJECT_ROOT=@@PROVIDER_ROOT@@
PROVIDER_UPGRADE_WORKSPACE_PATH=/workspace
PROVIDER_UPGRADE_COMPOSE_FILE=provider-@@PROVIDER_ID@@.yml
PROVIDER_UPGRADE_ENV_FILE=@@STATE_ROOT@@/provider-@@PROVIDER_ID@@.env
PROVIDER_UPGRADE_COMPOSE_PROJECT=@@COMPOSE_PROJECT@@
PROVIDER_UPGRADE_SERVICE=provider-gateway
PROVIDER_UPGRADE_RUNNER_IMAGE=@@PROVIDER_IMAGE@@
HOST_SSH_HOST=host.docker.internal
HOST_SSH_PORT=22
HOST_SSH_USER=@@UBUNTU_USER@@
HOST_SSH_KEY=/run/host-ssh/id_ed25519
HOST_REMOTE_CWD=/home/@@UBUNTU_USER@@
HOST_LOGIN_SHELL=/bin/bash
EOF
chown @@UBUNTU_USER@@:@@UBUNTU_USER@@ "$env_file"
chmod 0600 "$env_file"

cd "$root"
docker build --pull -t "$image" -f src/components/provider-gateway/Dockerfile .

cat >"$unit" <<EOF
[Unit]
Description=UniDesk Provider Gateway @@PROVIDER_ID@@
After=docker.service network-online.target ssh.service
Wants=network-online.target
Requires=docker.service

[Service]
Type=simple
ExecStartPre=-/usr/bin/docker rm -f $container
ExecStart=/usr/bin/docker run --name $container --restart always --pid host --env-file $env_file -p 127.0.0.1:18789:18789 -v /var/run/docker.sock:/var/run/docker.sock -v $root:/workspace:ro -v $log_dir:/var/log/unidesk -v $key_dir:/run/host-ssh:ro --add-host host.docker.internal:host-gateway $image
ExecStop=-/usr/bin/docker stop -t 20 $container
Restart=always
RestartSec=5
TimeoutStartSec=0
TimeoutStopSec=30

[Install]
WantedBy=multi-user.target
EOF

systemctl daemon-reload
systemctl enable --now unidesk-provider-gateway-@@PROVIDER_ID@@.service

deadline=$((SECONDS + 120))
while ((SECONDS < deadline)); do
  state_value=$(docker inspect --format '{{.State.Status}}' "$container" 2>/dev/null || true)
  if [[ "$state_value" == running ]]; then
    docker exec "$container" test -s /run/host-ssh/id_ed25519
    docker exec "$container" ssh -T -i /run/host-ssh/id_ed25519 -o BatchMode=yes -o StrictHostKeyChecking=accept-new -o ConnectTimeout=10 @@UBUNTU_USER@@@host.docker.internal true
    systemctl is-active --quiet docker
    systemctl is-active --quiet k3s
    kubectl get node @@K3S_NODE_NAME@@ -o name
    exit 0
  fi
  sleep 3
done

journalctl -u unidesk-provider-gateway-@@PROVIDER_ID@@.service -n 80 --no-pager >&2
exit 1
