#!/bin/sh
# Crawdad Sonar fleet script for macOS and Linux.
#
# Run it as root from your remote-management tool, on each machine:
#
#   sh sonar-fleet.sh --client "Client name" --out-dir /path/to/share
#
# It downloads the Crawdad Sonar binary for this machine from getcrawdad.dev, checks
# its SHA-256, scans every person's files on the machine (as root it reads them all),
# and writes one result file, <client>__<machine>.json, to --out-dir (default
# /var/tmp/crawdad-sonar). It prints the same line too, for tools that keep a script's
# output. Open the files in the fleet report at https://getcrawdad.dev/sonar/fleet.
#
# The script and Sonar send nothing about the machine anywhere. The result file stays
# where you put it, and it names the machine, its people and their files, so keep it
# with your other client records. It also lists the AI sites each person used in a
# browser in the last 30 days (site, browser, visit count; never a page address); add
# --no-browser-history to leave them out.
set -eu

BASE_URL="${CRAWDAD_SONAR_BASE_URL:-https://getcrawdad.dev}"
client=""
out_dir="/var/tmp/crawdad-sonar"
browser_flag=""
while [ $# -gt 0 ]; do
  case "$1" in
    --client) client="${2:-}"; shift 2 ;;
    --client=*) client="${1#--client=}"; shift ;;
    --out-dir) out_dir="${2:-}"; shift 2 ;;
    --out-dir=*) out_dir="${1#--out-dir=}"; shift ;;
    --no-browser-history) browser_flag="--no-browser-history"; shift ;;
    -h|--help) sed -n '2,19p' "$0"; exit 0 ;;
    *) echo "sonar-fleet: unknown option $1" >&2; exit 64 ;;
  esac
done

os="$(uname -s)"; arch="$(uname -m)"
case "$os" in
  Darwin) os="macos" ;;
  Linux) os="linux" ;;
  *) echo "sonar-fleet: this script is for macOS and Linux; use sonar-fleet.ps1 on Windows" >&2; exit 1 ;;
esac
case "$arch" in
  arm64|aarch64) arch="arm64" ;;
  x86_64|amd64) arch="x86_64" ;;
  *) echo "sonar-fleet: no Sonar build for $arch" >&2; exit 1 ;;
esac

if command -v curl >/dev/null 2>&1; then
  dl() { curl -fsSL --retry 2 "$1" -o "$2"; }
elif command -v wget >/dev/null 2>&1; then
  dl() { wget -q -O "$2" "$1"; }
else
  echo "sonar-fleet: needs curl or wget" >&2; exit 1
fi
if command -v sha256sum >/dev/null 2>&1; then
  sha() { sha256sum "$1" | awk '{print $1}'; }
else
  sha() { shasum -a 256 "$1" | awk '{print $1}'; }
fi

tmp="$(mktemp -d 2>/dev/null || mktemp -d -t crawdad-sonar)"
trap 'rm -rf "$tmp"' EXIT INT TERM
url="$BASE_URL/download/sonar/latest/crawdad-sonar-$os-$arch"
dl "$url" "$tmp/crawdad-sonar" || { echo "sonar-fleet: download failed: $url" >&2; exit 1; }
dl "$url.sha256" "$tmp/sum" || { echo "sonar-fleet: could not fetch the checksum" >&2; exit 1; }
expected="$(awk '{print $1}' "$tmp/sum")"
actual="$(sha "$tmp/crawdad-sonar")"
if [ -z "$expected" ] || [ "$expected" != "$actual" ]; then
  echo "sonar-fleet: checksum mismatch, refusing to run it (expected $expected, got $actual)" >&2
  exit 1
fi
chmod +x "$tmp/crawdad-sonar"

if [ -n "$client" ]; then
  line="$("$tmp/crawdad-sonar" --fleet --client "$client" ${browser_flag:+"$browser_flag"})"
else
  line="$("$tmp/crawdad-sonar" --fleet ${browser_flag:+"$browser_flag"})"
fi

host="$(hostname 2>/dev/null || uname -n)"
safe() { printf '%s' "$1" | tr -c 'A-Za-z0-9._ -' '_' | cut -c1-80; }
name="$(safe "${client:-machine}")__$(safe "$host").json"
mkdir -p "$out_dir"
printf '%s\n' "$line" > "$out_dir/$name"
printf '%s\n' "$line"
echo "sonar-fleet: wrote $out_dir/$name" >&2
