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

edge_url=""
agent_id=""
enrollment_token=""
release_base_url=""
agent_alias=""
install_dir="${HOME}/.oto/bin"
config_path="${HOME}/.oto/agent/config.yaml"
workspace_root="${HOME}/.oto/workspace"
log_dir="${HOME}/.oto/agent/log"
background="true"

usage() {
  echo "Usage: $0 --edge-url <url> --agent-id <id> --enrollment-token <token> --release-base-url <url> [options]"
  echo "Options:"
  echo "  --agent-alias <alias>       Alias for the agent"
  echo "  --install-dir <path>       Directory to install OTO agent (default: \$HOME/.oto/bin)"
  echo "  --config-path <path>       Path to config file (default: \$HOME/.oto/agent/config.yaml)"
  echo "  --workspace-root <path>    Workspace root directory (default: \$HOME/.oto/workspace)"
  echo "  --log-dir <path>           Log directory (default: \$HOME/.oto/agent/log)"
  echo "  --no-background            Run in the foreground"
}

while [[ $# -gt 0 ]]; do
  case "$1" in
    --edge-url)
      edge_url="$2"
      shift 2
      ;;
    --agent-id)
      agent_id="$2"
      shift 2
      ;;
    --enrollment-token)
      enrollment_token="$2"
      shift 2
      ;;
    --release-base-url)
      release_base_url="$2"
      shift 2
      ;;
    --agent-alias)
      agent_alias="$2"
      shift 2
      ;;
    --install-dir)
      install_dir="$2"
      shift 2
      ;;
    --config-path)
      config_path="$2"
      shift 2
      ;;
    --workspace-root)
      workspace_root="$2"
      shift 2
      ;;
    --log-dir)
      log_dir="$2"
      shift 2
      ;;
    --no-background)
      background="false"
      shift 1
      ;;
    *)
      echo "Unknown option: $1" >&2
      usage
      exit 1
      ;;
  esac
done

if [[ -z "$edge_url" || -z "$agent_id" || -z "$enrollment_token" || -z "$release_base_url" ]]; then
  echo "Error: Missing required arguments." >&2
  usage
  exit 1
fi

case "$release_base_url" in
  https://*) ;;
  *)
    echo "Error: --release-base-url must use https://" >&2
    exit 1
    ;;
esac

arch=$(uname -m)
case "$arch" in
  x86_64|amd64)
    asset_name="oto-linux-x64.tar.gz"
    ;;
  aarch64|arm64)
    asset_name="oto-linux-arm64.tar.gz"
    ;;
  *)
    echo "Error: Unsupported architecture: $arch" >&2
    exit 1
    ;;
esac

download_url="${release_base_url}/${asset_name}"
tmp_dir=$(mktemp -d)
trap 'rm -rf "$tmp_dir"' EXIT

echo "Downloading OTO agent..."
curl -fsSL "$download_url" -o "$tmp_dir/$asset_name"

echo "Extracting OTO agent..."
tar -xzf "$tmp_dir/$asset_name" -C "$tmp_dir"

mkdir -p "$install_dir"
cp "$tmp_dir/oto" "$install_dir/oto"
chmod +x "$install_dir/oto"

config_dir=$(dirname "$config_path")
agent_state_dir="${HOME}/.oto/agent"
pid_path="${agent_state_dir}/oto-agent.pid"

mkdir -p "$config_dir"
mkdir -p "$agent_state_dir"
mkdir -p "$workspace_root"
mkdir -p "$log_dir"

cat <<EOF > "$config_path"
agent:
  id: "$agent_id"
  alias: "$agent_alias"
  enrollment_token: "$enrollment_token"
edge:
  url: "$edge_url"
runtime:
  install_dir: "$install_dir"
  workspace_root: "$workspace_root"
  log_dir: "$log_dir"
EOF

chmod 600 "$config_path"

if [ "$background" = "true" ]; then
  echo "Starting OTO agent in the background..."
  nohup "$install_dir/oto" agent run --config "$config_path" >> "$log_dir/oto-agent.log" 2>&1 &
  pid=$!
  echo "$pid" > "$pid_path"
  echo "OTO agent started in the background. PID: $pid"
  echo "Log path: $log_dir/oto-agent.log"
else
  echo "OTO agent is configured to run in the foreground."
  echo "To run it manually, use the following command:"
  echo "  $install_dir/oto agent run --config $config_path"
fi
