#!/usr/bin/env bash
# Install Windows autostart (XAMPP + Laravel scheduler) on production via SSH.

set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# shellcheck source=common.sh
source "${SCRIPT_DIR}/common.sh"

run_setup_autostart() {
  load_config

  local remote_scripts="${DEPLOY_PATH_WIN}/deploy/windows"
  local install_ps1="${remote_scripts}/install-autostart.ps1"

  log "Installing Windows autostart on ${DEPLOY_HOST}"
  local xampp_root="${XAMPP_ROOT_WIN:-C:/xampp}"
  log "Paths: XAMPP=${xampp_root} App=${DEPLOY_PATH_WIN}"

  ssh_cmd "powershell -NoProfile -ExecutionPolicy Bypass -File \"${install_ps1}\" -XamppRoot \"${xampp_root}\" -AppRoot \"${DEPLOY_PATH_WIN}\" -PhpBinary \"${PHP_BINARY_WIN}\""

  log "Autostart installed. Tasks: AddFirst-Startup, AddFirst-Laravel-Schedule"
  log "Log file on server: ${DEPLOY_PATH_WIN}/storage/logs/windows-startup.log"
}

if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
  run_setup_autostart
fi
