#!/bin/sh
# CrawlCheck open observer — one-step install on any Linux server (Debian/Ubuntu/RHEL/Alpine, x64 or arm64). MIT.
# Paste as a VPS's cloud-init / user-data, or run as root:
#   curl -fsSL https://crawlcheck.io/observer/install.sh | sh -s -- --operator "Your name or company"
# Private observer (licence holders; readings go only to your licence):
#   curl -fsSL https://crawlcheck.io/observer/install.sh | CC_LICENCE_KEY=cc_... sh -s -- --private --operator "Your team"
# It installs Node 20 from nodejs.org (checksum-verified) into /opt/crawlcheck-observer, downloads the reference observer
# (checksum-verified against the value below), enrols this machine with a key made here (never sent anywhere), and adds
# an hourly cron job. Uninstall: rm -rf /opt/crawlcheck-observer /etc/cron.d/crawlcheck-observer; userdel ccobs
set -eu
OPERATOR=""; NETWORK=""; ID=""; PRIVATE=""; BASE="${CC_BASE:-https://crawlcheck.io}"
while [ $# -gt 0 ]; do case "$1" in --operator) OPERATOR="$2"; shift 2;; --network) NETWORK="$2"; shift 2;; --id) ID="$2"; shift 2;; --private) PRIVATE=1; shift;; *) echo "unknown option $1"; exit 2;; esac; done
[ -n "$OPERATOR" ] || { echo "--operator is required (who runs this observer)"; exit 2; }
[ "$(id -u)" = "0" ] || { echo "run as root (sudo)"; exit 2; }
[ -z "$PRIVATE" ] || [ -n "${CC_LICENCE_KEY:-}" ] || { echo "--private needs your licence key in CC_LICENCE_KEY"; exit 2; }
DIR=/opt/crawlcheck-observer; NODEV=20.18.1; OBS_SHA="26163b3b1ac09f5b5ab6a33d4d069c08280eff736fcc403b39563bc125ffab2d"
command -v curl >/dev/null || { (apt-get update -y && apt-get install -y curl xz-utils) 2>/dev/null || dnf install -y curl xz 2>/dev/null || apk add --no-cache curl xz; }
command -v xz >/dev/null || { apt-get install -y xz-utils 2>/dev/null || dnf install -y xz 2>/dev/null || apk add --no-cache xz; }
command -v crontab >/dev/null || [ -d /etc/cron.d ] || { apt-get install -y cron 2>/dev/null || dnf install -y cronie 2>/dev/null || apk add --no-cache dcron; }
case "$(uname -m)" in x86_64|amd64) ARCH=x64;; aarch64|arm64) ARCH=arm64;; *) echo "unsupported CPU $(uname -m)"; exit 2;; esac
mkdir -p "$DIR"; cd "$DIR"
if [ ! -x "$DIR/node/bin/node" ]; then
  T="node-v$NODEV-linux-$ARCH.tar.xz"
  curl -fsSLO "https://nodejs.org/dist/v$NODEV/$T"; curl -fsSL "https://nodejs.org/dist/v$NODEV/SHASUMS256.txt" | grep " $T\$" | sha256sum -c -
  tar -xJf "$T"; mv "node-v$NODEV-linux-$ARCH" node; rm -f "$T"
fi
curl -fsSL "$BASE/observer/observer.mjs" -o observer.mjs.new
echo "$OBS_SHA  observer.mjs.new" | sha256sum -c - && mv observer.mjs.new observer.mjs
id ccobs >/dev/null 2>&1 || useradd -r -s /usr/sbin/nologin -d "$DIR" ccobs 2>/dev/null || adduser -S -h "$DIR" ccobs
chown -R ccobs "$DIR"
P=ind; [ -z "$PRIVATE" ] || P=prv
if [ -z "$ID" ]; then ID="$P:$(hostname | tr 'A-Z' 'a-z' | tr -c 'a-z0-9-' '-' | cut -c1-20 | sed 's/-*$//')-$(od -An -N3 -tx1 /dev/urandom | tr -d ' \n')"; fi
if [ -z "$NETWORK" ]; then NETWORK="$(curl -fsS --max-time 8 https://ipinfo.io/org 2>/dev/null || echo unknown network), $(curl -fsS --max-time 8 https://ipinfo.io/city 2>/dev/null || true) $(curl -fsS --max-time 8 https://ipinfo.io/country 2>/dev/null || true)"; fi
if [ ! -f "$DIR/observer-key.json" ]; then
  if [ -n "$PRIVATE" ]; then KF="$DIR/.enrol-key"; umask 077; printf '%s' "$CC_LICENCE_KEY" > "$KF"; chown ccobs "$KF"; fi
  su -s /bin/sh ccobs -c "cd $DIR && CC_BASE=$BASE ${KF:+CC_LICENCE_KEY_FILE=$KF} ./node/bin/node observer.mjs enroll --id '$ID' --operator '$OPERATOR' --network '$NETWORK'" || { rm -f "${KF:-/nonexistent}"; exit 1; }
  rm -f "${KF:-/nonexistent}"
fi
mkdir -p /etc/cron.d
echo "$(od -An -N1 -tu1 /dev/urandom | tr -d ' ' | awk '{print $1%60}') * * * * ccobs cd $DIR && CC_BASE=$BASE ./node/bin/node observer.mjs run >> $DIR/run.log 2>&1" > /etc/cron.d/crawlcheck-observer
chmod 644 /etc/cron.d/crawlcheck-observer
(systemctl enable --now cron 2>/dev/null || systemctl enable --now crond 2>/dev/null || rc-service crond start 2>/dev/null || true)
su -s /bin/sh ccobs -c "cd $DIR && CC_BASE=$BASE ./node/bin/node observer.mjs run" | tail -3
if [ -n "$PRIVATE" ]; then echo "CrawlCheck private observer $ID installed. Its readings: $BASE/manage#private-observers"; else echo "CrawlCheck observer installed. Directory: $BASE/.well-known/crawlcheck-observers.json"; fi
