#!/bin/sh
# Migronix installer - https://migronix.io
#
#   curl -fsSL https://migronix.io/install.sh | sh
#
# Run it as root on the StarkOne server you migrate to. It downloads the
# static migronix binary for this machine, checks it against the published
# SHA-256 sums and installs it as /usr/local/bin/migronix. Nothing else on
# the system is changed. Running it again upgrades in place.
set -eu

BASE="${MIGRONIX_BASE:-https://migronix.io/download}"
BIN_DIR="${MIGRONIX_BIN_DIR:-/usr/local/bin}"

say() { printf '%s\n' "$*"; }
die() {
	printf 'migronix install: %s\n' "$*" >&2
	exit 1
}

fetch() {
	if command -v curl >/dev/null 2>&1; then
		curl -fsSL --retry 3 -o "$2" "$1" || die "download failed: $1"
	elif command -v wget >/dev/null 2>&1; then
		wget -q -O "$2" "$1" || die "download failed: $1"
	else
		die "curl or wget is needed"
	fi
}

sha256() {
	if command -v sha256sum >/dev/null 2>&1; then
		sha256sum "$1" | awk '{print $1}'
	elif command -v shasum >/dev/null 2>&1; then
		shasum -a 256 "$1" | awk '{print $1}'
	else
		die "sha256sum is needed to check the download"
	fi
}

# Everything runs from main, so a download cut short executes nothing.
main() {
	[ "$(uname -s)" = Linux ] || die "Migronix runs on Linux (the StarkOne server), not $(uname -s)"
	case "$(uname -m)" in
	x86_64 | amd64) arch=amd64 ;;
	aarch64 | arm64) arch=arm64 ;;
	*) die "no build for $(uname -m) yet (x86_64 and aarch64 are available)" ;;
	esac
	if [ "$(id -u)" -ne 0 ] && [ -z "${MIGRONIX_BIN_DIR:-}" ]; then
		die "run it as root: curl -fsSL https://migronix.io/install.sh | sudo sh"
	fi

	tmp=$(mktemp -d)
	trap 'rm -rf "$tmp"' EXIT INT TERM
	file="migronix-linux-$arch"

	say "Downloading $file from $BASE"
	fetch "$BASE/$file" "$tmp/$file"
	fetch "$BASE/SHA256SUMS" "$tmp/SHA256SUMS"
	want=$(awk -v f="$file" '$2 == f || $2 == "*" f {print $1}' "$tmp/SHA256SUMS")
	[ -n "$want" ] || die "SHA256SUMS lists no $file"
	got=$(sha256 "$tmp/$file")
	[ "$got" = "$want" ] || die "checksum mismatch for $file: got $got, expected $want"
	say "Checksum OK ($want)"

	chmod 755 "$tmp/$file"
	"$tmp/$file" version >/dev/null 2>&1 || die "the downloaded binary does not run on this machine"
	mkdir -p "$BIN_DIR"
	# Copy next to the old binary, then rename over it: overwriting a
	# binary that is running (a migration, the web UI) in place fails.
	cp "$tmp/$file" "$BIN_DIR/migronix.new"
	mv -f "$BIN_DIR/migronix.new" "$BIN_DIR/migronix"
	say "Installed $("$BIN_DIR/migronix" version) as $BIN_DIR/migronix"

	for t in ssh rsync; do
		command -v "$t" >/dev/null 2>&1 || say "Note: migronix uses $t, which is not installed here."
	done
	command -v starkone-cli >/dev/null 2>&1 ||
		say "Note: starkone-cli was not found. Migronix runs on the StarkOne server you migrate to."

	cat <<'EOF'

Next steps:
  migronix ui                         web UI: prints its URL and the ssh -L line to reach it
  migronix init --source <ip>         or link this server with the DirectAdmin server here
  migronix help                       every command
EOF
}

main "$@"
