#!/bin/bash
# Installs (or reinstalls) the two Feature Scout jobs on a Mac: a daily look for ideas (SCAN_HOUR:
# SCAN_MINUTE, default 08:15) and a build pass every 30 minutes. `feature-scout setup` runs this
# for you; it can also be run by hand from anywhere.
set -euo pipefail
here="$(cd "$(dirname "$0")/.." && pwd)"
bin="$(command -v scout || true)"; [ -x "$here/.venv/bin/scout" ] && bin="$here/.venv/bin/scout"
[ -n "$bin" ] || { echo "the scout command was not found; install Feature Scout first" >&2; exit 1; }
logdir="${SCOUT_DATA_DIR:-$HOME/.feature-scout}/logs"; mkdir -p "$logdir"
for job in com.featurescout.daily com.featurescout.poll; do
  src="$here/launchd/$job.plist"
  dst="$HOME/Library/LaunchAgents/$job.plist"
  launchctl bootout "gui/$(id -u)/$job" 2>/dev/null || true
  sed -e "s|__SCOUT_BIN__|$bin|g" -e "s|__SCOUT_DIR__|$here|g" -e "s|__HOME__|$HOME|g" \
      -e "s|__LOG_DIR__|$logdir|g" -e "s|__SCAN_HOUR__|${SCAN_HOUR:-8}|g" -e "s|__SCAN_MINUTE__|${SCAN_MINUTE:-15}|g" \
      "$src" > "$dst"
  launchctl bootstrap "gui/$(id -u)" "$dst"
  launchctl enable "gui/$(id -u)/$job"
  echo "installed $job"
done
