#!/bin/bash

set -e

# -------------------------------
# 1. Install Docker
# -------------------------------
apt-get update -y
apt-get install -y docker.io

systemctl enable docker
systemctl start docker

# -------------------------------
# 2. Prepare directories and volume
# -------------------------------
mkdir -p /home/runner/apptainer
chmod 777 /home/runner/apptainer

docker volume create workdir

# -------------------------------
# 3. Pull Docker images
# -------------------------------
docker pull inductiva/task-runner:latest
docker pull inductiva/file-tracker:latest

# -------------------------------
# 4. Read metadata and export env variables
# -------------------------------
for var in INDUCTIVA_API_KEY INDUCTIVA_API_URL MACHINE_GROUP_NAME MAX_IDLE_TIMEOUT; do
    export "$var"=$(curl -s "http://metadata.google.internal/computeMetadata/v1/instance/attributes/$var" -H "Metadata-Flavor: Google")
    echo "$var=${!var}"
done

# -------------------------------
# 5. Launch file-tracker
# -------------------------------
docker run -d --name file-tracker \
  --network host \
  -v workdir:/workdir \
  -e API_URL="$INDUCTIVA_API_URL" \
  -e USER_API_KEY="$INDUCTIVA_API_KEY" \
  inductiva/file-tracker:latest

# -------------------------------
# 6. Launch task-runner
# -------------------------------
docker run -d --name task-runner \
  --network host \
  --privileged \
  --platform linux/amd64 \
  -v /home/runner/apptainer:/executer-images \
  -v workdir:/workdir \
  --add-host host.docker.internal:host-gateway \
  -e EXECUTER_IMAGES_DIR=/executer-images \
  -e API_URL="$INDUCTIVA_API_URL" \
  -e USER_API_KEY="$INDUCTIVA_API_KEY" \
  -e MACHINE_GROUP_NAME="$MACHINE_GROUP_NAME" \
  -e HOST_NAME="${TASK_RUNNER_HOSTNAME:-$(hostname)}" \
  -e MAX_IDLE_TIMEOUT="$MAX_IDLE_TIMEOUT" \
  inductiva/task-runner:latest

# -------------------------------
# 7. Monitor task-runner and shutdown VM if it stops
# -------------------------------
METADATA_URL="http://metadata.google.internal/computeMetadata/v1/instance"
VM_NAME=$(curl -s -H "Metadata-Flavor: Google" "$METADATA_URL/name")
ZONE=$(curl -s -H "Metadata-Flavor: Google" "$METADATA_URL/zone" | awk -F/ '{print $4}')

while true; do
    sleep 10
    if [ -z "$(docker ps -q -f name=task-runner)" ]; then
        echo "Task runner stopped, deleting VM..."
        gcloud compute instances delete "$VM_NAME" --zone="$ZONE" --quiet
        break
    fi
done
