#!/bin/sh
# Installs the MopokeHQ daemon from MopokeHQ's signed package repository,
# enrols this server and starts the service:
#
#   curl -fsSL https://get.mopokehq.com | sudo sh -s -- --enroll CODE [--logs]
#
# The daemon only reports (OS packages, runtimes, sites, containers, and
# with --logs its auth, web server and PHP-FPM logs): it opens no port and
# accepts no commands. Rather do it by hand? It is three
# steps, and this script does nothing else:
#
#   1. Add the repository and its signing key, checking the fingerprint
#      below: /etc/apt/sources.list.d/mopokehq.list (Debian, Ubuntu),
#      /etc/yum.repos.d/mopokehq.repo (AlmaLinux, Rocky), or a line in
#      /etc/apk/repositories and the key in /etc/apk/keys (Alpine).
#   2. Install the package: apt-get install mopoke-daemon,
#      dnf install mopoke-daemon, or apk add mopoke-daemon.
#   3. Enrol and start: mopoke-daemon enrol --code CODE (and for --logs,
#      mopoke-daemon logs on --no-restart), then
#      systemctl enable --now mopoke-daemon (Alpine: rc-update add
#      mopoke-daemon default && rc-service mopoke-daemon start).
set -eu

REPO="https://packages.mopokehq.com"
# The repository signing key (apt and dnf). Check it against
# https://docs.mopokehq.com/daemon before you trust this script.
KEY_FINGERPRINT="31FDF504CDE5CC92B367C350414C610F9D890C18"
# Alpine signs with an RSA key instead: the sha256 of its public key file.
APK_KEY_SHA256="6bebaabec473f4465f9b653b373863f90c898e34f3a735040e6d380a238a831d"

die() { echo "mopoke: $*" >&2; exit 1; }
CODE=""
LOGS=""
while [ $# -gt 0 ]; do
	case "$1" in
	--enroll | --enrol) [ $# -ge 2 ] || die "--enroll needs a code"; CODE="$2"; shift 2 ;;
	--enroll=* | --enrol=*) CODE="${1#*=}"; shift ;;
	--logs) LOGS=1; shift ;;
	*) die "unknown option $1 (usage: sh install.sh [--enroll CODE] [--logs])" ;;
	esac
done
case "$KEY_FINGERPRINT$APK_KEY_SHA256" in *PLACEHOLDER*) die "this copy of the script has no signing key in it yet" ;; esac
[ "$(id -u)" = 0 ] || die "run as root: curl -fsSL https://get.mopokehq.com | sudo sh -s -- --enroll CODE"
[ -r /etc/os-release ] || die "no /etc/os-release, so no package for this system; see https://docs.mopokehq.com/daemon"
. /etc/os-release
TMP=$(mktemp -d)
trap 'rm -rf "$TMP"' EXIT

fetch() { if command -v curl >/dev/null 2>&1; then curl -fsSL "$1" -o "$2"; else wget -qO "$2" "$1"; fi; }
check_key() {
	fpr=$(gpg --show-keys --with-colons "$1" 2>/dev/null | awk -F: '$1 == "fpr" { print $10; exit }')
	[ "$fpr" = "$KEY_FINGERPRINT" ] || die "the repository key's fingerprint is '$fpr', not $KEY_FINGERPRINT: stopping"
	echo "mopoke: repository key $fpr"
}

case " ${ID:-} ${ID_LIKE:-} " in
*" debian "* | *" ubuntu "*)
	command -v gpg >/dev/null 2>&1 || { apt-get update -qq && apt-get install -y -qq gpg >/dev/null; }
	fetch "$REPO/mopokehq.gpg" "$TMP/mopokehq.gpg"
	check_key "$TMP/mopokehq.gpg"
	install -m 0644 "$TMP/mopokehq.gpg" /usr/share/keyrings/mopokehq.gpg
	echo "deb [signed-by=/usr/share/keyrings/mopokehq.gpg] $REPO/apt stable main" >/etc/apt/sources.list.d/mopokehq.list
	apt-get update -qq -o Dir::Etc::sourcelist=sources.list.d/mopokehq.list -o Dir::Etc::sourceparts=- -o APT::Get::List-Cleanup=0
	DEBIAN_FRONTEND=noninteractive apt-get install -y mopoke-daemon
	;;
*" rhel "* | *" centos "* | *" fedora "* | *" almalinux "* | *" rocky "*)
	fetch "$REPO/mopokehq.asc" "$TMP/mopokehq.asc"
	check_key "$TMP/mopokehq.asc"
	install -m 0644 "$TMP/mopokehq.asc" /etc/pki/rpm-gpg/RPM-GPG-KEY-mopokehq
	cat >/etc/yum.repos.d/mopokehq.repo <<EOF
[mopokehq]
name=MopokeHQ
baseurl=$REPO/rpm/\$basearch
enabled=1
gpgcheck=1
repo_gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mopokehq
EOF
	dnf install -y mopoke-daemon
	;;
*" alpine "*)
	fetch "$REPO/apk/mopokehq.rsa.pub" "$TMP/mopokehq.rsa.pub"
	sum=$(sha256sum "$TMP/mopokehq.rsa.pub" | cut -d' ' -f1)
	[ "$sum" = "$APK_KEY_SHA256" ] || die "the repository key's sha256 is $sum, not $APK_KEY_SHA256: stopping"
	install -m 0644 "$TMP/mopokehq.rsa.pub" /etc/apk/keys/mopokehq.rsa.pub
	grep -qx "$REPO/apk" /etc/apk/repositories || echo "$REPO/apk" >>/etc/apk/repositories
	apk update -q && apk add mopoke-daemon
	;;
*) die "no package for ${PRETTY_NAME:-this system}; see https://docs.mopokehq.com/daemon for the tarball" ;;
esac

if [ -n "$CODE" ] && [ -f /var/lib/mopoke/state.json ]; then
	echo "mopoke: this server is already enrolled. To enrol it again: mopoke-daemon enrol --force --code CODE"
elif [ -n "$CODE" ]; then
	mopoke-daemon enrol --code "$CODE"
fi
if [ -n "$LOGS" ]; then
	# Writes logs = true into /etc/mopoke/daemon.toml before the first start.
	mopoke-daemon logs on --no-restart >/dev/null
	echo "mopoke: log shipping is on (sudo mopoke-daemon logs off to stop it)."
fi
if [ ! -f /var/lib/mopoke/state.json ]; then
	echo "mopoke: installed. Enrol with a code from MopokeHQ: mopoke-daemon enrol --code CODE"
elif [ -d /run/systemd/system ]; then
	# restart, not enable --now: on a server already running the daemon,
	# re-running this line must still pick up --logs.
	systemctl enable mopoke-daemon && systemctl restart mopoke-daemon
	echo "mopoke: running; the first report goes out now (journalctl -u mopoke-daemon)."
else
	rc-update add mopoke-daemon default && rc-service mopoke-daemon restart
	echo "mopoke: running; the first report goes out now (/var/log/mopoke-daemon.log)."
fi
