#!/bin/sh
# Pocket Homelab — one-command install of Pocket Core and its app installer.
#
#   curl -fsSL https://pockethomelab.com/install.sh | sh
#
# This file (deploy/install.sh in pocket-homelab-core) is the source: pockethomelab.com/install.sh is
# a copy of it (public/install.sh in pocket-homelab-web). Change it here, then copy it over.
#
# What it does (Linux with Docker, where it offers to install Docker if missing; or a Mac with
# Docker Desktop, OrbStack or Colima, which it starts when it isn't running):
#   - writes ~/pocket/docker-compose.yml + .env: Pocket Core, the installer agent (Install / Update /
#     Remove apps from Core's web page), and Caddy and Tailscale for when they are switched on
#   - starts them and prints the address of Core's setup page
# Then everything else happens in the browser: create your account, and the setup asks how phones
# reach Pocket with a padlock (Pocket's free address, your own domain on Cloudflare, or just this
# network's addresses), which apps to start with (bundles or one by one) and whether to connect
# Tailscale for away from home. Until a name is chosen every app has its own port on this machine
# (http://<this machine>:<port>), so nothing but a browser on the same network is needed.
#
# Run it again to update Pocket Core to the newest release; settings in .env are kept.
#
# Options:
#   --dir DIR        where everything lives (~/pocket)
#   --host HOST      address apps are opened on without a name (this machine's LAN address)
#   --version X.Y.Z  a specific Pocket Core release (default: the newest)
#   --port PORT      Core's port (8179)
#   --notify URL     where update notifications go: an ntfy topic (https://ntfy.sh/my-topic) or an
#                    Apprise …/notify/<key> URL (also settable later in Core: Settings → Box)
#   -y, --yes        don't ask (installs Docker if missing)
# The same choices as the setup in the browser, all at once (a new install; for scripts):
#   --address pocket       Pocket's free address, <random>.box.pockethomelab.net, with a padlock
#   --address plain        this network's addresses (the default until a name is chosen)
#   --cloudflare-token T --cloudflare-domain example.com [--cloudflare-name pocket]
#                          your own domain on Cloudflare: Pocket at core.pocket.example.com
#   --bundle LIST          app bundles to install: flagship (the 20 daily drivers), essentials,
#                          office, developer, media, family, money, health (comma separated;
#                          pockethomelab.com/box/bundles)
#   --apps LIST            single apps to install as well, as slugs (notes,tasks; pockethomelab.com/apps)
#   --admin-email E        the email you will create your Core account with; the apps make it
#                          their admin (asked for when bundles or apps are given)
#   --tailscale-key KEY    join Tailscale with this auth key (tskey-auth-…) instead of a sign-in link
#   --tailnet              Pocket as its own device on your tailnet, on a home box too (a VPS always is)
#   --private              keep Pocket's ports off the internet with a firewall rule (a VPS always does)
# On a VPS (no home-network address) the plain command is the whole setup: Pocket joins your tailnet
# as a device of its own (open the sign-in link it prints, or pass --tailscale-key), gets Pocket's
# free address with a padlock pointing there, and nothing of it is reachable from the internet.
# It needs none of the machine's ports, so it sits next to a web server that already runs there.
# Your own domain without Cloudflare, with ports 80/443 open to this machine:
#   --domain DOMAIN --email EMAIL   Caddy gets a certificate for core.DOMAIN and each <app>.DOMAIN
# Removing it again:
#   --uninstall            give the free name back, leave the tailnet, remove Pocket's containers,
#                          images and firewall rules, and the folder with all apps and their data
#   --uninstall --keep-data   the same, but the folder (settings, apps' data) stays
set -eu

REPO=abjelosevic88/pocket-homelab-core
BUNDLES_URL=https://raw.githubusercontent.com/abjelosevic88/pocket-homelab-box/main/bundles.json
DIR=${POCKET_DIR:-$HOME/pocket}
HOST="" DOMAIN="" EMAIL="" VERSION="" PORT="" NOTIFY="" YES=0 APPS="" BUNDLES="" ADMIN_EMAIL=""
ADDRESS="" CF_TOKEN="" CF_DOMAIN="" CF_NAME=pocket TS_KEY="" NAME_ON="" PRIVATE=0 TAILNET=""
UNINSTALL=0 KEEP_DATA=0

say() { printf '%s\n' "$*"; }
step() { printf '\n\033[1m%s\033[0m\n' "$*"; }
ok() { printf '  \033[32m✓\033[0m %s\n' "$*"; }
LOG=/dev/null # docker's own output goes here once the folder exists ($DIR/install.log)
die() { printf 'install.sh: %s\n' "$*" >&2; exit 1; }
tty_ok() { (: </dev/tty) 2>/dev/null; } # a terminal to ask on (not there under cron, CI, ssh without -t)
ask() { # ask "question" → 0 for yes
	[ "$YES" = 1 ] && return 0
	tty_ok || die "$1 Re-run with --yes to answer yes."
	printf '%s [Y/n] ' "$1" >/dev/tty
	read -r a </dev/tty || a=n
	case "$a" in "" | y | Y | yes) return 0 ;; *) return 1 ;; esac
}
MAC=0
[ "$(uname -s)" = Darwin ] && MAC=1
sedi() { # sedi SCRIPT FILE: sed -i the same on Linux and macOS, keeping the file's owner and mode
	(umask 077 && sed "$1" "$2" >"$2.new") && cat "$2.new" >"$2" && rm -f "$2.new"
}

while [ $# -gt 0 ]; do
	case "$1" in
	--dir) DIR=$2; shift 2 ;;
	--host) HOST=$2; shift 2 ;;
	--domain) DOMAIN=$2; shift 2 ;;
	--email) EMAIL=$2; shift 2 ;;
	--version) VERSION=${2#v}; shift 2 ;;
	--port) PORT=$2; shift 2 ;;
	--notify) NOTIFY=$2; shift 2 ;;
	--address) ADDRESS=$2; shift 2 ;;
	--cloudflare-token) CF_TOKEN=$2; ADDRESS=cloudflare; shift 2 ;;
	--cloudflare-domain) CF_DOMAIN=$2; shift 2 ;;
	--cloudflare-name) CF_NAME=$2; shift 2 ;;
	--bundle | --bundles) BUNDLES=$2; shift 2 ;;
	--apps) APPS=$2; shift 2 ;;
	--admin-email) ADMIN_EMAIL=$2; shift 2 ;;
	--tailscale-key) TS_KEY=$2; shift 2 ;;
	--name-on) NAME_ON=$2; shift 2 ;;
	--private) PRIVATE=1; shift ;;
	--tailnet) TAILNET=1; shift ;;
	--uninstall) UNINSTALL=1; shift ;;
	--keep-data) KEEP_DATA=1; shift ;;
	-y | --yes) YES=1; shift ;;
	-h | --help) sed -n '2,53p' "$0" 2>/dev/null || say "See https://pockethomelab.com/docs"; exit 0 ;;
	*) die "unknown option $1 (see --help)" ;;
	esac
done

case "$ADDRESS" in "" | pocket | plain) ;; cloudflare)
	[ -n "$CF_TOKEN" ] && [ -n "$CF_DOMAIN" ] || die "your own domain on Cloudflare needs --cloudflare-token and --cloudflare-domain"
	case "$CF_TOKEN$CF_DOMAIN$CF_NAME" in *[!A-Za-z0-9._-]*) die "--cloudflare-token, --cloudflare-domain and --cloudflare-name take letters, digits, dots, dashes and underscores" ;; esac ;;
*) die "--address is pocket or plain (or use --cloudflare-token for your own domain)" ;; esac
[ -z "$ADDRESS" ] || [ -z "$DOMAIN" ] || die "--domain already sets the address; leave out --address"
case "$TS_KEY" in "" | tskey-*) ;; *) die "--tailscale-key takes a Tailscale auth key (tskey-auth-…)" ;; esac
case "$TS_KEY" in *[!A-Za-z0-9_-]*) die "--tailscale-key: that doesn't look like an auth key" ;; esac
case "$NAME_ON" in "" | home) ;; tailscale) TAILNET=1 NAME_ON="" ;; *) die "--name-on is home or tailscale" ;; esac

[ "$(uname -s)" = Linux ] || [ "$MAC" = 1 ] || die "this installs on Linux (a Pocket Box, a mini PC, a VM or a Raspberry Pi 4/5) or a Mac."
if [ "$MAC" = 1 ]; then
	# A Mac: Docker runs in a Linux VM (Docker Desktop, OrbStack, Colima). No VPS there, no systemd.
	[ "$PRIVATE" = 0 ] && [ -z "$TAILNET" ] || die "--private and --tailnet are for Linux; on a Mac, Pocket is reached on this network (Tailscale can be connected in Core)."
fi
case "$(uname -m)" in x86_64 | amd64 | aarch64 | arm64) ;; *) die "Pocket Core runs on 64-bit x86 or ARM, not $(uname -m)." ;; esac
command -v curl >/dev/null || die "curl is needed."
SUDO=""
[ "$(id -u)" = 0 ] || [ "$MAC" = 1 ] || SUDO=sudo # a Mac's Docker runs as you
DOCKER=docker

# The installer agent's two sockets in $DIR/installer. On a Mac they are made inside Docker's Linux
# VM, where this Mac can't connect to them: Pocket Core's container (it has curl) asks for us.
SC=curl ST="" # sudo curl / sudo test while removing Pocket (the folder can be root's by then)
sock_curl() { # sock_curl SOCKET CURL-ARGS…
	s=$1
	shift
	if [ "$MAC" = 1 ]; then
		$DOCKER exec pocket-core curl --unix-socket "/run/pocket-installer/$s" "$@"
	else
		$SC --unix-socket "$DIR/installer/$s" "$@"
	fi
}
sock_ok() { # sock_ok SOCKET: the agent listens there
	if [ "$MAC" = 1 ]; then
		$DOCKER exec pocket-core test -S "/run/pocket-installer/$1" 2>/dev/null
	else
		$ST test -S "$DIR/installer/$1"
	fi
}

# A Mac: Docker Desktop, OrbStack or Colima; started when it isn't running (it takes a minute).
if [ "$MAC" = 1 ]; then
	for d in /Applications/Docker.app/Contents/Resources/bin "$HOME/.orbstack/bin" /opt/homebrew/bin /usr/local/bin; do
		[ -d "$d" ] && case ":$PATH:" in *":$d:"*) ;; *) PATH="$PATH:$d" ;; esac
	done
	export PATH
fi
mac_docker() { # → 0 once Docker answers
	docker info >/dev/null 2>&1 && return 0
	how="" ctx=$(docker context show 2>/dev/null || true)
	case "$ctx" in colima*) how=colima ;; orbstack) how=orbstack ;; desktop-linux) how=desktop ;; esac
	if [ -z "$how" ]; then
		if [ -d /Applications/OrbStack.app ]; then how=orbstack
		elif [ -d /Applications/Docker.app ]; then how=desktop
		elif command -v colima >/dev/null 2>&1; then how=colima
		else return 1
		fi
	fi
	case "$how" in
	colima)
		say "Starting Colima…"
		p=default
		case "$ctx" in colima-*) p=${ctx#colima-} ;; esac
		colima start "$p" >/tmp/pocket-colima.log 2>&1 || return 1 ;;
	orbstack) say "Starting OrbStack…"; open -ga OrbStack || return 1 ;;
	desktop) say "Starting Docker Desktop…"; open -ga Docker || return 1 ;;
	esac
	i=0
	until docker info >/dev/null 2>&1; do
		i=$((i + 1))
		[ $i -lt 90 ] || return 1
		sleep 2
	done
}

# ---- --uninstall: everything this script (and Pocket in it) set up, gone again ----
if [ "$UNINSTALL" = 1 ]; then
	SC="$SUDO curl" ST=$SUDO
	[ "$MAC" = 0 ] || mac_docker || die "Docker isn't running; start it first, so Pocket's containers can be removed."
	[ -d "$DIR" ] && DIR=$(cd "$DIR" && pwd)
	if [ ! -f "$DIR/docker-compose.yml" ] && [ ! -f /etc/systemd/system/pocket-private.service ] && [ ! -f /etc/systemd/system/pocket-firewall.service ]; then
		die "there is no Pocket install in $DIR (another folder? --dir DIR)"
	fi
	if [ "$KEEP_DATA" = 1 ]; then
		say "This removes Pocket from this machine: its containers, images and firewall rules, Pocket's"
		say "free address and its device on your tailnet. $DIR stays, with your settings and the apps' data."
	else
		say "This removes Pocket from this machine: every app, all their data and your Pocket account"
		say "($DIR), Pocket's free address and its device on your tailnet. It can't be undone."
	fi
	if [ "$YES" != 1 ]; then
		tty_ok || die "re-run with --yes to remove it."
		printf 'Type "remove" to go ahead: ' >/dev/tty
		read -r a </dev/tty || a=""
		[ "$a" = remove ] || die "stopped; nothing was removed."
	fi
	DOCKER=""
	if docker info >/dev/null 2>&1; then DOCKER=docker; elif $SUDO docker info >/dev/null 2>&1; then DOCKER="$SUDO docker"; fi
	get() { [ -f "$DIR/.env" ] && sed -n "s/^$1=//p" "$DIR/.env" | tail -n1 || true; }
	running() { [ -n "$DOCKER" ] && [ "$($DOCKER inspect -f '{{.State.Running}}' "$1" 2>/dev/null)" = true ]; }
	# The name first: going back to this network's addresses gives a Pocket name back (and takes your
	# Cloudflare records away). The installer agent does that, so it has to be running.
	if [ -n "$(get POCKET_ADDRESS)" ] && running pocket-core-installer && sock_ok address.sock; then
		printf '  Giving the address back'
		# Straight away, without moving the apps back first (they go next anyway); an installer from
		# before 0.42.14 doesn't know release, so it gets the slower switch back to this network.
		sock_curl address.sock -fsS -X POST http://agent/v1/address/release >/dev/null 2>&1 ||
			sock_curl address.sock -fsS -X PUT -H 'Content-Type: application/json' --data '{"mode":"plain"}' http://agent/v1/address >/dev/null 2>&1 || true
		n=0
		until $DOCKER logs --since 10m pocket-core-installer 2>&1 | grep -q 'gave the Pocket name back\|removed from Cloudflare'; do
			n=$((n + 1)); [ $n -lt 40 ] || break
			printf '.'; sleep 3
		done
		say
		if [ $n -lt 40 ]; then ok "Address given back"; else say "  The address wasn't given back in time; an unused Pocket name lapses by itself."; fi
	fi
	TS_WAS=""
	if running pocket-tailscale; then
		$DOCKER exec pocket-tailscale tailscale logout >/dev/null 2>&1 && ok "Logged out of Tailscale" && TS_WAS=1
	fi
	if [ -n "$DOCKER" ]; then
		rmi="--rmi all" vols="-v"
		[ "$KEEP_DATA" != 1 ] || vols=""
		# The apps, and the folders of installs that failed (kept for reading in apps/pocket-archive).
		# One line that counts up, so a long removal (60 apps on a Raspberry Pi take a quarter of an
		# hour) doesn't look stuck.
		total=0
		for d in "$DIR"/apps/*/ "$DIR"/apps/pocket-archive/*/; do [ -f "$d/docker-compose.yml" ] && total=$((total + 1)); done
		n=0
		for d in "$DIR"/apps/*/ "$DIR"/apps/pocket-archive/*/; do
			[ -f "$d/docker-compose.yml" ] || continue
			n=$((n + 1))
			printf '\r\033[K  Removing apps and their images: %d of %d (%s)' "$n" "$total" "$(basename "$d")"
			(cd "$d" && $DOCKER compose down $vols $rmi >/dev/null 2>&1) || true
		done
		[ "$total" = 0 ] || printf '\r\033[K'
		printf '  Removing Pocket Core and the leftover images…'

		[ ! -f "$DIR/docker-compose.yml" ] || (cd "$DIR" && $DOCKER compose --profile proxy --profile tailscale --profile domain down $vols $rmi >/dev/null 2>&1) || true
		for c in $($DOCKER ps -aq --filter name='^pocket-' 2>/dev/null); do $DOCKER rm -f "$c" >/dev/null 2>&1 || true; done
		# What compose down leaves: older versions of Pocket's own images (every update keeps the one
		# before) and layers no tag points at any more. Never `prune -a`: other images on this machine stay.
		for i in $($DOCKER images --format '{{.Repository}} {{.ID}}' 2>/dev/null | awk '$1 ~ /^ghcr\.io\/abjelosevic88\/pocket-homelab-/ {print $2}' | sort -u); do
			$DOCKER rmi -f "$i" >/dev/null 2>&1 || true
		done
		$DOCKER image prune -f >/dev/null 2>&1 || true
		printf '\r\033[K'
		ok "Containers and images removed"
	fi
	FW_WAS=""
	if [ -f /etc/systemd/system/pocket-private.service ]; then
		$SUDO systemctl disable --now pocket-private.service >/dev/null 2>&1 || true # its stop takes the rule out
		$SUDO rm -f /etc/systemd/system/pocket-private.service
		FW_WAS=1
	fi
	if [ -f /etc/systemd/system/pocket-firewall.service ] || [ -f /usr/local/sbin/pocket-firewall ]; then
		FW_WAS=1
		$SUDO systemctl disable --now pocket-firewall.service >/dev/null 2>&1 || true
		$SUDO rm -f /etc/systemd/system/pocket-firewall.service /usr/local/sbin/pocket-firewall
		# Its rules carry the comment "pocket-firewall"; take them out one by one (reloading the
		# firewall's file would also wipe Docker's and Tailscale's own rules).
		$SUDO nft -a list ruleset 2>/dev/null | awk '
			/^table / { fam = $2; tab = $3 }
			/^[ \t]*chain / { ch = $2 }
			/comment "pocket-firewall"/ && /# handle / { h = $NF; print fam, tab, ch, h }' |
			while read -r fam tab ch h; do $SUDO nft delete rule "$fam" "$tab" "$ch" handle "$h" || true; done
	fi
	if [ -n "$FW_WAS" ]; then
		$SUDO systemctl daemon-reload >/dev/null 2>&1 || true
		ok "Firewall rules removed"
	fi
	if [ "$KEEP_DATA" != 1 ] && [ -d "$DIR" ]; then
		# Only a folder install.sh made (its .env names it), never a home folder by mistake.
		if [ "$DIR" != "$HOME" ] && [ "$DIR" != / ] && [ "$(get POCKET_DIR)" = "$DIR" ]; then
			$SUDO rm -rf "$DIR" && ok "Removed $DIR"
		else
			say "  $DIR doesn't look like a folder install.sh made; left it, remove it yourself if you mean to."
		fi
	fi
	say
	say "  Pocket is gone from this machine. Docker stays (remove it with your package manager if you like)."
	[ -z "$TS_WAS" ] || say "  On your tailnet its device shows as offline: remove it in Tailscale's admin console (Machines)."
	exit 0
fi

# ---- Docker ----
if [ "$MAC" = 1 ]; then
	command -v docker >/dev/null 2>&1 || [ -d /Applications/Docker.app ] || [ -d /Applications/OrbStack.app ] || die "Pocket runs in Docker, and this Mac has none yet. Install one of these, then run this again:
  Docker Desktop  https://www.docker.com/products/docker-desktop/
  OrbStack        https://orbstack.dev
  Colima          brew install colima docker docker-compose && colima start"
	mac_docker || die "Docker isn't running and didn't start; start Docker Desktop, OrbStack or Colima, then run this again."
fi
engine() { # Docker's engine (not just a docker command) is on this machine
	command -v dockerd >/dev/null 2>&1 || [ -x /usr/bin/dockerd ] || [ -x /usr/sbin/dockerd ] ||
		systemctl cat docker.service >/dev/null 2>&1 || docker info >/dev/null 2>&1
}
if ! command -v docker >/dev/null 2>&1 || ! engine; then
	ask "Docker is not installed here. Install it now (get.docker.com)?" || die "install Docker first: https://docs.docker.com/engine/install/"
	# get.docker.com stops at the first apt error, with only apt's own words. Check apt first and say
	# what's wrong; a broken Ubuntu country mirror (xx.archive.ubuntu.com) is swapped for the main one.
	apt_ok() { # → 0 when "apt-get update" runs cleanly; its errors in APT_ERR
		APT_ERR=$($SUDO apt-get update 2>&1 | grep -E '^(E|Err|Error):' || true)
		[ -z "$APT_ERR" ]
	}
	if command -v apt-get >/dev/null 2>&1 && ! apt_ok; then
		bad=$(printf '%s\n' "$APT_ERR" | sed -n 's#.*Failed to fetch https\{0,1\}://\([^/ ]*\)/.*#\1#p' | sort -u | tr '\n' ' ')
		bad=${bad% } mirror=0 fix=""
		for h in $bad; do
			case "$h" in [a-z][a-z].archive.ubuntu.com) mirror=1 fix="$fix -e 's#//$h#//archive.ubuntu.com#'" ;; *) mirror=0; break ;; esac
		done
		if [ "$mirror" = 1 ] && ask "Ubuntu's download mirror $bad is refusing downloads. Switch to the main one, archive.ubuntu.com?"; then
			for h in $bad; do
				for f in $(grep -lF "//$h" /etc/apt/sources.list /etc/apt/sources.list.d/*.list /etc/apt/sources.list.d/*.sources 2>/dev/null || true); do
					$SUDO sed -i "s#//$(printf '%s' "$h" | sed 's/\./\\./g')#//archive.ubuntu.com#g" "$f" && say "  $f: $h → archive.ubuntu.com"
				done
			done
			apt_ok || die "apt still can't update its package lists:
$APT_ERR
Fix that first (sudo apt update must finish without errors), then run this again."
		elif [ "$mirror" = 1 ]; then
			die "Ubuntu's mirror $bad is refusing downloads, so Docker can't be installed. Switch to the main one:
  sudo sed -i$fix /etc/apt/sources.list.d/*.sources /etc/apt/sources.list
then run this again."
		else
			die "apt can't update its package lists, so Docker can't be installed yet:
$APT_ERR
Fix that first (sudo apt update must finish without errors), then run this again."
		fi
	fi
	say "Installing Docker…"
	curl -fsSL https://get.docker.com | $SUDO sh >/tmp/pocket-docker-install.log 2>&1 || die "installing Docker failed; see /tmp/pocket-docker-install.log"
	[ "$(id -u)" = 0 ] || $SUDO usermod -aG docker "$(id -un)" || true
	$SUDO systemctl enable --now docker >/dev/null 2>&1 || true
fi
DOCKER=docker
if ! docker info >/dev/null 2>&1; then
	if [ -n "$SUDO" ] && $SUDO docker info >/dev/null 2>&1; then
		DOCKER="$SUDO docker" # not in the docker group yet (log out and in once to drop sudo)
	elif $SUDO systemctl start docker >/dev/null 2>&1 && $SUDO docker info >/dev/null 2>&1; then
		DOCKER="$SUDO docker"
		docker info >/dev/null 2>&1 && DOCKER=docker
	else
		die "Docker is installed but not running (try: sudo systemctl start docker)."
	fi
fi
ok Docker

# ---- room for every app's network ----
# Each app gets its own small Docker network, and Docker's default address ranges run out after about
# 30 ("all predefined address pools have been fully subnetted"): every app after that fails to install.
# /24 networks from 172.16.0.0/12 (the range Docker uses anyway) leave room for thousands. docker0 keeps
# its usual 172.17.0.1/16. Needs a restart of Docker, so on a machine with running containers it asks.
# A Mac's Docker keeps its settings in its own app; the end of the run says where (POOLS_TIP).
DJ=/etc/docker/daemon.json POOLS_TIP=""
if [ "$MAC" = 1 ]; then
	[ "$($DOCKER info --format '{{json .DefaultAddressPools}}' 2>/dev/null)" != null ] || case "$(docker context show 2>/dev/null)" in
	colima*) POOLS_TIP="colima stop, then colima start --edit, and in docker: {} add" ;;
	orbstack) POOLS_TIP="OrbStack → Settings → Docker, add" ;;
	*) POOLS_TIP="Docker Desktop → Settings → Docker Engine, add" ;;
	esac
elif ! $SUDO grep -q '"default-address-pools"' "$DJ" 2>/dev/null; then
	cur=$($SUDO cat "$DJ" 2>/dev/null | tr -d ' \t\r\n')
	new=""
	case "$cur" in
	"" | "{}") new=$(printf '%s\n' '{' '  "bip": "172.17.0.1/16",' '  "default-address-pools": [{ "base": "172.16.0.0/12", "size": 24 }]' '}') ;;
	*) command -v python3 >/dev/null 2>&1 && new=$($SUDO cat "$DJ" | python3 -c '
import json, sys
d = json.load(sys.stdin)
d["default-address-pools"] = [{"base": "172.16.0.0/12", "size": 24}]
if "bip" not in d and "--bip" not in sys.argv[1]:
    d["bip"] = "172.17.0.1/16"
print(json.dumps(d, indent=2))' "$(ps -o args= -C dockerd 2>/dev/null)" 2>/dev/null) || new="" ;;
	esac
	running=$($DOCKER ps -q 2>/dev/null | wc -l | tr -d ' ')
	if [ -z "$new" ]; then
		say "  Docker can only make networks for about 30 apps. Add this to $DJ and restart Docker for more:"
		say '    "default-address-pools": [{ "base": "172.16.0.0/12", "size": 24 }]'
	elif ! command -v systemctl >/dev/null 2>&1; then
		say "  Docker can only make networks for about 30 apps; add default-address-pools to $DJ for more (see pockethomelab.com/install)."
	elif [ "$running" = 0 ] || ask "  Docker has room for about 30 apps' networks; give it room for thousands? Docker restarts, so running containers stop for about a minute."; then
		$SUDO mkdir -p /etc/docker
		[ -n "$cur" ] && $SUDO cp "$DJ" "$DJ.pocket-backup" 2>/dev/null || true
		printf '%s\n' "$new" | $SUDO tee "$DJ" >/dev/null
		if $SUDO systemctl restart docker >/dev/null 2>&1; then
			i=0
			while ! $DOCKER info >/dev/null 2>&1 && [ $i -lt 30 ]; do sleep 1; i=$((i + 1)); done
			ok "Docker: room for every app's network"
		else
			[ -f "$DJ.pocket-backup" ] && $SUDO cp "$DJ.pocket-backup" "$DJ" || $SUDO rm -f "$DJ"
			$SUDO systemctl restart docker >/dev/null 2>&1 || true
			say "  Docker didn't restart with more network room, so its settings are back as they were."
		fi
	fi
fi
$DOCKER compose version >/dev/null 2>&1 || die "the Docker Compose plugin is missing (package docker-compose-plugin)."
SOCK=/var/run/docker.sock
[ "$MAC" = 1 ] || [ -S "$SOCK" ] || die "no Docker socket at $SOCK (rootless Docker is not supported yet)."

# ---- settings: keep what .env has, flags win ----
mkdir -p "$DIR"
DIR=$(cd "$DIR" && pwd)
ENV="$DIR/.env"
get() { [ -f "$ENV" ] && sed -n "s/^$1=//p" "$ENV" | tail -n1 || true; }
[ -n "$DOMAIN" ] || DOMAIN=$(get POCKET_DOMAIN)
[ -n "$EMAIL" ] || EMAIL=$(get POCKET_EMAIL)
HOME_IP=""
# A Mac moves between networks: its address is looked up afresh on every run (unless --host), and
# POCKET_HOME_IP tells the installer agent, which inside Docker's VM only sees the VM's address.
[ -n "$HOST" ] || [ "$MAC" = 1 ] || HOST=$(get POCKET_HOST)
[ -n "$PORT" ] || PORT=$(get CORE_PORT)
[ -n "$NOTIFY" ] || NOTIFY=$(get NOTIFY_URL)
[ -n "$PORT" ] || PORT=8179
TOKEN=$(get INSTALLER_TOKEN)
[ -n "$TOKEN" ] || TOKEN=$(od -An -tx1 -N32 /dev/urandom | tr -d ' \n')
if [ -z "$HOST" ] && [ "$MAC" = 1 ]; then
	HOST=$(ipconfig getifaddr "$(route -n get 1.1.1.1 2>/dev/null | awk '/interface:/ {print $2}')" 2>/dev/null || true)
	[ -n "$HOST" ] || HOST=$(ipconfig getifaddr en0 2>/dev/null || ipconfig getifaddr en1 2>/dev/null || true)
	[ -n "$HOST" ] || HOST=$(get POCKET_HOST)
	[ -n "$HOST" ] || die "could not tell this Mac's address on your network; pass --host."
elif [ -z "$HOST" ]; then
	HOST=$(ip -4 route get 1.1.1.1 2>/dev/null | awk '{for (i = 1; i < NF; i++) if ($i == "src") { print $(i + 1); exit }}')
	[ -n "$HOST" ] || HOST=$(hostname -I 2>/dev/null | awk '{print $1}')
	[ -n "$HOST" ] || die "could not tell this machine's address; pass --host."
fi
[ "$MAC" = 0 ] || HOME_IP=$HOST
TZ_NAME=$(timedatectl show -p Timezone --value 2>/dev/null || true)
[ -n "$TZ_NAME" ] || TZ_NAME=$(cat /etc/timezone 2>/dev/null || true)
[ -n "$TZ_NAME" ] || TZ_NAME=$(readlink /etc/localtime 2>/dev/null | sed -n 's#.*zoneinfo/##p')
[ -n "$TZ_NAME" ] || TZ_NAME=UTC

# A VPS: the address this machine goes out from is a public one, not a home network's. Pocket's
# apps must not be open to the internet there: with Tailscale, only the tailnet reaches them.
SRC=""
[ "$MAC" = 1 ] || SRC=$(ip -4 route get 1.1.1.1 2>/dev/null | awk '{for (i = 1; i < NF; i++) if ($i == "src") { print $(i + 1); exit }}')
PUBLIC=1
case "$SRC" in 10.* | 192.168.* | 172.1[6-9].* | 172.2[0-9].* | 172.3[01].* | "") PUBLIC=0 ;; esac
[ -n "$NAME_ON" ] || NAME_ON=$(get POCKET_NAME_ON)
[ "$NAME_ON" = tailscale ] && TAILNET=1 NAME_ON="" # 0.39's way of saying it
[ "$(get POCKET_PRIVATE)" = 1 ] && PRIVATE=1
[ -n "$TAILNET" ] || TAILNET=$(get POCKET_TAILNET)
# A VPS (or --tailnet): Pocket is a device of its own on your tailnet. Its Caddy runs inside Pocket's
# Tailscale container, so it takes none of this machine's ports, and a firewall rule keeps the
# ports Pocket's containers publish off the internet.
if [ "$PUBLIC" = 1 ] && [ -z "$DOMAIN" ]; then
	TAILNET=1 PRIVATE=1
fi
[ -z "$DOMAIN" ] || TAILNET=""
if [ "$TAILNET" = 1 ]; then
	PRIVATE=1
	[ -n "$ADDRESS" ] || [ -n "$(get POCKET_ADDRESS)" ] || ADDRESS=pocket
fi

if [ -z "$VERSION" ]; then
	# The newest release tag (GitHub answers /releases/latest with a redirect to it).
	VERSION=$(curl -fsSI "https://github.com/$REPO/releases/latest" | tr -d '\r' | sed -n 's#^[Ll]ocation: .*/tag/v##p' | tail -n1)
	[ -n "$VERSION" ] || die "could not find the newest Pocket Core release; pass --version."
fi

# The address. With --domain: Caddy and that domain, as set here. Otherwise the installer agent's
# address service (Settings → System in Core) switches the install over to a name once there is one
# and keeps it in .env (POCKET_ADDRESS), so running this again keeps it.
NAME=$(get POCKET_ADDRESS)
ADDR_SOCK="" PROFILES="" CADDYFILE=""
if [ -n "$DOMAIN" ]; then
	CORE_URL="https://core.$DOMAIN" APP_URL="https://{slug}.$DOMAIN" SECURE=true PROFILES=domain NAME=""
	CADDYFILE="$DIR/caddy/Caddyfile"
else
	ADDR_SOCK=/run/pocket-installer/address.sock
	# Keep what was switched on (proxy for a name, tailscale), drop --domain's.
	for p in $(get COMPOSE_PROFILES | tr ',' ' '); do
		case "$p" in proxy | tailscale) PROFILES="${PROFILES:+$PROFILES,}$p" ;; esac
	done
	[ "$TAILNET" != 1 ] || case ",$PROFILES," in *,proxy,*) ;; *) PROFILES="${PROFILES:+$PROFILES,}proxy" ;; esac
	if [ -n "$NAME" ]; then
		CORE_URL="https://core.$NAME" APP_URL="https://{slug}.$NAME" SECURE=true
	else
		CORE_URL="http://$HOST:$PORT" APP_URL="http://$HOST:{port}" SECURE=false
	fi
fi
PUID=$(id -u) PGID=$(id -g)
if [ "$MAC" = 1 ]; then # the socket the containers see is the one in Docker's VM
	DOCKER_GID=$($DOCKER run --rm --entrypoint stat -v "$SOCK:$SOCK" "ghcr.io/abjelosevic88/pocket-homelab-core-installer:$VERSION" -c %g "$SOCK" 2>/dev/null) || DOCKER_GID=0
else
	DOCKER_GID=$(stat -c %g "$SOCK")
fi

# ---- apps to install right away (--bundle, --apps; a new install only) ----
# Only a Core without data is new: its registration key is chosen here and pinned for the first
# start, so the installer agent can install the apps before anyone has an account. Afterwards the
# pin is dropped again (Core keeps the key, and Settings → Box can rotate it).
NEW=1
[ -n "$(ls -A "$DIR/core/data" 2>/dev/null)" ] && NEW=0
if [ -n "$BUNDLES" ]; then
	json=$(curl -fsSL "$BUNDLES_URL") || die "could not fetch the bundles ($BUNDLES_URL)"
	for b in $(printf '%s' "$BUNDLES" | tr ',' ' '); do
		printf '%s' "$json" | grep -q "\"id\": *\"$b\"\|^ *\"$b\": *\[" || die "no bundle '$b' (there are: flagship $(printf '%s' "$json" | sed -n 's/.*"id": *"\([^"]*\)".*/\1/p' | tr '\n' ' '))"
	done
	# bundles.json has each bundle's "id" a line before its "apps": [...], and the flagship apps
	# (Tier 1, the 20 daily drivers) on one line "flagship": [...] (pocket-homelab-box).
	from=$(printf '%s\n' "$json" | awk -v want=",$BUNDLES," '
		/^ *"flagship": *\[/ && index(want, ",flagship,") { s = $0; sub(/.*\[/, "", s); sub(/\].*/, "", s); gsub(/[" ]/, "", s); print s }
		/"id":/ { cur = $0; sub(/.*"id": *"/, "", cur); sub(/".*/, "", cur) }
		/"apps":/ && cur != "" && index(want, "," cur ",") { s = $0; sub(/.*\[/, "", s); sub(/\].*/, "", s); gsub(/[" ]/, "", s); print s }')
	APPS="${APPS:+$APPS,}$(printf '%s' "$from" | tr '\n' ',')"
fi
APPS=$(printf '%s' "$APPS" | tr ',' '\n' | awk 'NF && !seen[$0]++' | paste -sd, -)
for a in $(printf '%s' "$APPS" | tr ',' ' '); do
	case "$a" in *[!a-z0-9-]*) die "--apps takes app slugs like notes,tasks, not '$a' (pockethomelab.com/apps)" ;; esac
done
if [ -n "$APPS" ] && [ "$NEW" = 0 ]; then
	say "Pocket Core is already set up here: add apps in Core (Apps → Install app); --bundle and --apps are for new installs."
	APPS=""
fi
REGKEY=""
if [ -n "$APPS" ]; then
	if [ -z "$ADMIN_EMAIL" ] && [ "$YES" != 1 ] && tty_ok; then
		printf 'Installing Pocket Core with %s.\nThe email you will create your Core account with (the apps make it their admin)%s: ' \
			"$(printf '%s' "$APPS" | sed 's/,/, /g')" "${EMAIL:+ [$EMAIL]}" >/dev/tty
		read -r ADMIN_EMAIL </dev/tty || ADMIN_EMAIL=""
	fi
	[ -n "$ADMIN_EMAIL" ] || ADMIN_EMAIL=$EMAIL
	ADMIN_EMAIL=$(printf '%s' "$ADMIN_EMAIL" | tr 'A-Z' 'a-z')
	case "$ADMIN_EMAIL" in
	"") say "No email given: installing Pocket Core only (pick the apps in the browser instead)."; APPS="" ;;
	*[!a-z0-9._%+@-]* | *@*@* | @* | *@) die "'$ADMIN_EMAIL' does not look like an email address." ;;
	*@*) ;;
	*) die "'$ADMIN_EMAIL' does not look like an email address." ;;
	esac
	[ -z "$APPS" ] || REGKEY="pcreg_$(od -An -tx1 -N24 /dev/urandom | tr -d ' \n')"
fi
if [ "$TAILNET" != 1 ] && { [ "$ADDRESS" = pocket ] || [ "$ADDRESS" = cloudflare ]; }; then
	if [ "$MAC" = 1 ]; then
		busy=$(lsof -nP -iTCP -sTCP:LISTEN 2>/dev/null | awk '{print $9}' | grep -E '[:.](80|443)$' | sort -u || true)
	else
		busy=$(ss -ltnH 2>/dev/null | awk '{print $4}' | grep -E '[:.](80|443)$' || true)
	fi
	[ -z "$busy" ] || [ "$NAME" != "" ] || die "ports 80/443 are taken on this machine ($(printf '%s' "$busy" | tr '\n' ' ')); a name with a padlock needs them for Caddy"
fi

mkdir -p "$DIR/core/data" "$DIR/installer" "$DIR/apps" "$DIR/caddy"
chmod 700 "$DIR/installer"
LOG="$DIR/install.log"
: >"$LOG"

umask 077
cat >"$ENV" <<EOF
# Pocket Core — written by install.sh, kept when you run it again. The installer agent changes
# BASE_URL, SECURE_COOKIES, INSTALLER_PUBLIC_URL, COMPOSE_PROFILES and POCKET_ADDRESS itself when
# the address is changed in Core (Settings → System).
POCKET_VERSION=$VERSION
POCKET_DIR=$DIR
POCKET_HOST=$HOST
POCKET_HOME_IP=$HOME_IP
POCKET_DOMAIN=$DOMAIN
POCKET_EMAIL=$EMAIL
POCKET_ADDRESS=$NAME
POCKET_NAME_ON=$NAME_ON
POCKET_PRIVATE=$PRIVATE
POCKET_TAILNET=$TAILNET
COMPOSE_PROFILES=$PROFILES
CORE_PORT=$PORT
BASE_URL=$CORE_URL
SECURE_COOKIES=$SECURE
NOTIFY_URL=$NOTIFY
TZ=$TZ_NAME
PUID=$PUID
PGID=$PGID
DOCKER_GID=$DOCKER_GID
INSTALLER_TOKEN=$TOKEN
INSTALLER_PUBLIC_URL=$APP_URL
INSTALLER_CADDYFILE=$CADDYFILE
BOX_ADDRESS=$ADDR_SOCK
# Set only while install.sh installs the first apps (--bundle, --apps).
REGISTRATION_KEY=$REGKEY
EOF
umask 022

[ -f "$DIR/docker-compose.yml" ] && cp "$DIR/docker-compose.yml" "$DIR/docker-compose.yml.bak"
cat >"$DIR/docker-compose.yml" <<'EOF'
# Pocket Core + its app installer, written by install.sh (https://pockethomelab.com/install.sh).
# Settings are in .env; run install.sh again to update. Apps you install live in ./apps.
services:
  core:
    image: ghcr.io/abjelosevic88/pocket-homelab-core:${POCKET_VERSION}
    container_name: pocket-core
    restart: unless-stopped
    user: "${PUID}:${PGID}"
    ports:
      - "0.0.0.0:${CORE_PORT}:8080" # IPv4 only, so --private's firewall rule covers it
    environment:
      TZ: ${TZ}
      BASE_URL: ${BASE_URL}
      SECURE_COOKIES: ${SECURE_COOKIES}
      NOTIFY_URL: ${NOTIFY_URL:-}
      REGISTRATION_KEY: ${REGISTRATION_KEY:-}
      INSTALLER_SOCKET: /run/pocket-installer/installer.sock
      INSTALLER_TOKEN: ${INSTALLER_TOKEN}
      # The address choice and Tailscale, through the installer agent (empty with --domain).
      BOX_ADDRESS: ${BOX_ADDRESS:-}
    volumes:
      - ./core/data:/data
      - ./installer:/run/pocket-installer
    extra_hosts:
      - "host.docker.internal:host-gateway"

  # Does the Docker work behind Install / Update / Remove, and switches the install to the address
  # chosen in Core. Core never sees the Docker socket. Root inside (the Docker socket is
  # root-equivalent anyway), so it can give each app's data folders to the uid Pocket images run as
  # (1000) and everything else to you (FILES_OWNER).
  installer:
    image: ghcr.io/abjelosevic88/pocket-homelab-core-installer:${POCKET_VERSION}
    container_name: pocket-core-installer
    restart: unless-stopped
    network_mode: host # sees which ports are free and checks new apps on 127.0.0.1
    environment:
      TZ: ${TZ}
      INSTALLER_SOCKET: /run/pocket-installer/installer.sock
      INSTALLER_TOKEN: ${INSTALLER_TOKEN}
      INSTALLER_PUBLIC_URL: ${INSTALLER_PUBLIC_URL}
      INSTALLER_APPS_DIR: ${POCKET_DIR}/apps
      INSTALLER_CORE_URL: http://host.docker.internal:${CORE_PORT}
      INSTALLER_DOCKER_GID: ${DOCKER_GID}
      INSTALLER_FILES_OWNER: ${PUID}:${PGID}
      INSTALLER_CADDYFILE: ${INSTALLER_CADDYFILE}
      INSTALLER_CADDY_CONTAINER: pocket-caddy
      INSTALLER_ADDRESS_SOCKET: ${BOX_ADDRESS:-}
      INSTALLER_POCKET_DIR: ${POCKET_DIR}
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
      - ${POCKET_DIR}:${POCKET_DIR} # same path inside and out: compose files name host folders
      - ./installer:/run/pocket-installer

EOF
if [ "$TAILNET" = 1 ]; then
	TUN="    devices:
      - /dev/net/tun:/dev/net/tun"
	TUNARG=""
	[ -c /dev/net/tun ] || { TUN="" TUNARG=', "--tun=userspace-networking"'; } # no tun device (some container VPSes)
	cat >>"$DIR/docker-compose.yml" <<EOF

  # Pocket's own device on your tailnet. Caddy shares its network, so https://core.<name> and the
  # apps listen on Pocket's tailnet address only, and on none of this machine's ports.
  tailscale:
    image: tailscale/tailscale:stable
    container_name: pocket-tailscale
    restart: unless-stopped
    # tailscaled itself, not the image's containerboot: that one gives up and restarts the container
    # while the sign-in link waits, and Caddy would lose its network with it. The installer agent
    # runs \`tailscale up\` in here.
    command: ["tailscaled", "--statedir=/var/lib/tailscale"$TUNARG]
    cap_add: [NET_ADMIN, NET_RAW]
$TUN
    volumes:
      - ./tailscale:/var/lib/tailscale
    extra_hosts:
      - "host.docker.internal:host-gateway"

  # HTTPS for core.<name> and every app (the installer agent writes ./caddy/Caddyfile and ./caddy/tls),
  # and each of them on its own port over plain http on the tailnet address until there is a name.
  caddy:
    image: caddy:2
    container_name: pocket-caddy
    restart: unless-stopped
    profiles: [proxy]
    network_mode: service:tailscale
    depends_on: [tailscale]
    volumes:
      - ./caddy:/etc/caddy
      - ./caddy/data:/data
      - ./caddy/config:/config
EOF
	[ -f "$DIR/caddy/Caddyfile" ] || printf '# Pocket Core on its port of Pocket'"'"'s tailnet address; the installer agent writes this file again.\nhttp://:%s {\n\treverse_proxy host.docker.internal:%s\n}\n' "$PORT" "$PORT" >"$DIR/caddy/Caddyfile"
else
	cat >>"$DIR/docker-compose.yml" <<'EOF'

  # HTTPS for core.<name> and every app: with --domain (profile domain), or once Core has a name
  # (profile proxy; the installer agent writes ./caddy/Caddyfile and ./caddy/tls then).
  caddy:
    image: caddy:2
    container_name: pocket-caddy
    restart: unless-stopped
    profiles: [domain, proxy]
    ports:
      - "0.0.0.0:80:80"
      - "0.0.0.0:443:443"
    volumes:
      - ./caddy:/etc/caddy
      - ./caddy/data:/data
      - ./caddy/config:/config
    extra_hosts:
      - "host.docker.internal:host-gateway"

  # Away from home (profile tailscale): started when you connect Tailscale in Core.
  tailscale:
    image: tailscale/tailscale:stable
    container_name: pocket-tailscale
    restart: unless-stopped
    profiles: [tailscale]
    network_mode: host
    cap_add: [NET_ADMIN, NET_RAW]
    devices:
      - /dev/net/tun:/dev/net/tun
    environment:
      TS_STATE_DIR: /var/lib/tailscale
      TS_USERSPACE: "false"
    volumes:
      - ./tailscale:/var/lib/tailscale
EOF
fi

if [ "$MAC" = 1 ]; then
	# Unix sockets don't work through a Mac's folder sharing into Docker's VM: the agent's sockets
	# live in a volume inside the VM instead (this Mac reaches them through Core's container).
	sedi 's#- \./installer:/run/pocket-installer#- installer:/run/pocket-installer#' "$DIR/docker-compose.yml"
	printf '\nvolumes:\n  installer: # the installer agent'"'"'s sockets (a Mac: inside Docker'"'"'s VM)\n' >>"$DIR/docker-compose.yml"
fi

if [ -n "$DOMAIN" ] && [ ! -f "$CADDYFILE" ]; then
	{
		[ -n "$EMAIL" ] && printf '{\n\temail %s\n}\n\n' "$EMAIL"
		printf '# Pocket Core. The installer adds a site here for every app it installs.\ncore.%s {\n\treverse_proxy host.docker.internal:%s\n}\n' "$DOMAIN" "$PORT"
	} >"$CADDYFILE"
fi

# ---- private: only the tailnet (and this machine) reaches Pocket's containers ----
# Docker's published ports go around ufw; DOCKER-USER is the chain Docker leaves to its owner. New
# connections that come in on the public interface are dropped there: SSH, Tailscale itself and
# everything the containers start are not affected.
if [ "$PRIVATE" = 1 ]; then
	IFACE=$(ip -4 route get 1.1.1.1 2>/dev/null | awk '{for (i = 1; i < NF; i++) if ($i == "dev") { print $(i + 1); exit }}')
	[ -n "$IFACE" ] || die "could not tell this machine's internet interface for --private."
	command -v iptables >/dev/null 2>&1 || $SUDO sh -c 'command -v iptables' >/dev/null 2>&1 || die "--private needs iptables."
	rule="DOCKER-USER -i $IFACE -m conntrack --ctstate NEW -j DROP"
	printf '%s\n' "[Unit]" \
		"Description=Pocket: only the tailnet reaches Pocket's containers, not $IFACE (install.sh --private)" \
		"After=docker.service" "PartOf=docker.service" "" "[Service]" "Type=oneshot" "RemainAfterExit=yes" \
		"ExecStart=/bin/sh -c 'iptables -C $rule 2>/dev/null || iptables -I $rule'" \
		"ExecStop=/bin/sh -c 'iptables -D $rule 2>/dev/null || true'" "" "[Install]" "WantedBy=docker.service" |
		$SUDO tee /etc/systemd/system/pocket-private.service >/dev/null
	$SUDO systemctl daemon-reload
	$SUDO systemctl enable pocket-private.service >/dev/null 2>&1
	$SUDO systemctl restart pocket-private.service
	$SUDO systemctl is-active --quiet pocket-private.service || die "the firewall rule didn't go in; see: systemctl status pocket-private"
	ok "Closed to the internet (only your tailnet reaches Pocket)"
fi

cd "$DIR"
[ -n "${POCKET_NO_PULL:-}" ] || $DOCKER compose pull --quiet >>"$LOG" 2>&1 || die "downloading Pocket failed; see $LOG" # POCKET_NO_PULL=1: images built here (tests)
$DOCKER compose up -d --remove-orphans >>"$LOG" 2>&1 || die "starting Pocket failed; see $LOG"

wait_core() {
	i=0
	until curl -fsS "http://127.0.0.1:$PORT/healthz" >/dev/null 2>&1; do
		i=$((i + 1))
		[ $i -lt 60 ] || die "Pocket Core did not start; see: cd $DIR && $DOCKER compose logs core"
		sleep 2
	done
}
wait_core
ok "Pocket Core $VERSION"
if [ "$MAC" = 1 ] && [ -z "$NAME" ] && ! curl -fsS -m 5 "http://$HOST:$PORT/healthz" >/dev/null 2>&1; then
	say "  Pocket answers on this Mac (http://localhost:$PORT) but not on http://$HOST:$PORT, so phones and other"
	say "  computers can't reach it yet: let Docker's ports onto your network (and through the Mac's firewall)."
fi

# ---- a firewall that keeps Docker's containers off the internet ----
# A hand-made nftables firewall with a forward (or input) policy of drop stops every container here:
# no Tailscale sign-in, no app downloads. pocket-firewall lets traffic that starts in Docker's
# networks through (out, and to the ports Docker publishes on this machine); nothing new comes in
# from outside. A small unit runs it again after every boot, after nftables has loaded its file.
FW=/usr/local/sbin/pocket-firewall
fw_script() {
	cat <<'FWEOF'
#!/bin/sh
# Written by Pocket's install.sh: let Docker's containers through this machine's nftables firewall,
# out to the internet and to the ports Docker publishes here. Nothing new comes in from outside.
# "pocket-firewall check" only lists the chains that would change.
set -f
command -v nft >/dev/null 2>&1 || exit 0
chains() { # family table chain hook, for each nftables chain that drops forwarded/incoming traffic
	nft list chains 2>/dev/null | awk '
		/^table / { fam = $2; tab = $3 }
		/^[ \t]*chain / { ch = $2 }
		/hook (forward|input)/ && /policy drop/ {
			if ((fam == "ip" || fam == "ip6") && ch ~ /^[A-Z]+$/) next # iptables: Docker manages those
			print fam, tab, ch, ($0 ~ /hook forward/ ? "forward" : "input")
		}'
}
todo=""
chains | while read -r fam tab ch hook; do
	rules=$(nft list chain "$fam" "$tab" "$ch")
	need() { ! printf '%s\n' "$rules" | grep -qF "$1"; }
	for r in 'ct state established,related accept' 'iifname "docker0" accept' 'iifname "br-*" accept'; do
		[ "$hook" = forward ] || [ "$r" != 'ct state established,related accept' ] || continue
		need "$r" || continue
		if [ "${1:-}" = check ]; then echo "$fam $tab $ch"; else nft add rule $fam $tab $ch $r comment '"pocket-firewall"'; fi
	done
done | sort -u
FWEOF
}
online() { $DOCKER exec pocket-core wget -q -T 8 -O /dev/null https://controlplane.tailscale.com/key?v=1 >/dev/null 2>&1; }
check_firewall() {
	online && return 0
	tmp=$(mktemp)
	fw_script >"$tmp"
	in_way=$($SUDO sh "$tmp" check 2>/dev/null || true)
	rm -f "$tmp"
	if [ -z "$in_way" ]; then
		say "  Pocket's containers can't reach the internet (no route out of Docker's network?); apps and Tailscale need it."
		return 0
	fi
	say "  A firewall on this machine (nftables: $(printf '%s' "$in_way" | tr '\n' ',' | sed 's/,$//; s/,/, /g')) keeps"
	say "  Pocket's containers off the internet, so Tailscale and the apps can't download anything."
	if ask "  Let Docker's traffic out through it? Nothing new comes in from outside."; then
		fw_script | $SUDO tee "$FW" >/dev/null
		$SUDO chmod 755 "$FW"
		printf '%s\n' "[Unit]" "Description=Pocket: let Docker's containers through this machine's nftables firewall (install.sh)" \
			"After=nftables.service docker.service" "" "[Service]" "Type=oneshot" "RemainAfterExit=yes" "ExecStart=$FW" "" \
			"[Install]" "WantedBy=multi-user.target" | $SUDO tee /etc/systemd/system/pocket-firewall.service >/dev/null
		$SUDO systemctl daemon-reload
		$SUDO systemctl enable pocket-firewall.service >/dev/null 2>&1
		$SUDO systemctl restart pocket-firewall.service
		if online; then ok "Firewall lets Pocket's containers out"; else say "  Still offline; see: sudo nft list ruleset"; fi
	else
		say "  Then allow it yourself: in nftables, iifname \"docker0\" and iifname \"br-*\" accept (forward and input chains)."
	fi
}

[ "$MAC" = 1 ] || check_firewall # a Mac: Docker's VM has its own way out

# The installer agent's two sockets: installs (bearer token) and the address/Tailscale service.
agent() { # agent PATH [JSON]
	if [ $# -gt 1 ]; then
		sock_curl installer.sock -fsS -H "Authorization: Bearer $TOKEN" -H 'Content-Type: application/json' --data "$2" "http://agent$1"
	else
		sock_curl installer.sock -fsS -H "Authorization: Bearer $TOKEN" "http://agent$1"
	fi
}
address() { # address METHOD PATH [JSON]: answers JSON, {"error": …} when refused
	sock_curl address.sock -sS -X "$1" -H 'Content-Type: application/json' ${3:+--data} ${3:+"$3"} "http://agent$2"
}
field() { sed -n "s/.*\"$1\":\"\([^\"]*\)\".*/\1/p" | head -n1; } # field NAME < json
wait_agent() {
	i=0
	until agent /v1/status >/dev/null 2>&1 && { [ -z "$ADDR_SOCK" ] || sock_ok address.sock; }; do
		i=$((i + 1))
		[ $i -lt 30 ] || die "the installer agent did not start; see: cd $DIR && $DOCKER compose logs installer"
		sleep 2
	done
}

# ---- the first apps, through the installer agent (the same Install as Core's Apps page) ----
INSTALLED="" FAILED=""
if [ -n "$APPS" ]; then
	say "  Installing apps"
	wait_agent
	for a in $(printf '%s' "$APPS" | tr ',' ' '); do
		printf '  %s ' "$a"
		job=$(agent /v1/install "{\"slug\":\"$a\",\"registration_key\":\"$REGKEY\",\"admin_email\":\"$ADMIN_EMAIL\"}" 2>&1) || {
			say "could not start: $job"
			FAILED="$FAILED $a"
			continue
		}
		id=$(printf '%s' "$job" | field id) state=running out="" n=0
		# The agent gives up on a job after 20 minutes itself; this only guards against it vanishing.
		while [ "$state" = running ] && [ $n -lt 450 ]; do
			sleep 3
			n=$((n + 1))
			printf '.'
			out=$(agent "/v1/jobs/$id" 2>/dev/null) || continue
			state=$(printf '%s' "$out" | field state)
		done
		if [ "$state" = done ]; then
			say " ok"
			INSTALLED="$INSTALLED $a"
		else
			err=$(printf '%s' "$out" | field error)
			say " failed${err:+: $err}"
			FAILED="$FAILED $a"
		fi
	done
	# Drop the pin: Core keeps the key it was given, and rotating it in Settings now sticks.
	sedi 's/^REGISTRATION_KEY=.*/REGISTRATION_KEY=/' "$ENV"
	$DOCKER compose up -d core >>"$LOG" 2>&1
	wait_core
fi

# ---- Tailscale, then the address, when given here (otherwise: in the browser) ----
# Tailscale first: in tailnet mode the name points at Pocket's own tailnet address.
TS_IP=""
ts_ip() { sed -n 's/.*"ips":\["\(100\.[0-9.]*\)".*/\1/p'; }
if [ "$TAILNET" = 1 ] || [ -n "$TS_KEY" ]; then
	wait_agent
	out=$(address GET /v1/tailscale)
	if [ "$(printf '%s' "$out" | field state)" != running ]; then
		out=$(address POST /v1/tailscale/up "{\"authkey\":\"$TS_KEY\"}")
	fi
	err=$(printf '%s' "$out" | field error)
	url=$(printf '%s' "$out" | field auth_url)
	if [ -z "$err" ] && [ -z "$url" ] && [ "$(printf '%s' "$out" | field state)" != running ] &&
		! $DOCKER exec pocket-tailscale wget -q -T 8 -O /dev/null https://controlplane.tailscale.com/key?v=1 >/dev/null 2>&1; then
		err="Pocket's containers can't reach the internet, so Tailscale can't sign in. Usually a firewall on this machine (nftables) drops Docker's traffic: run this again and let it fix that, or allow traffic from the docker0 and br-* interfaces yourself"
	fi
	if [ -z "$err" ] && [ -n "$url" ] && [ "$(printf '%s' "$out" | field state)" != running ]; then
		say
		say "  Add Pocket to your Tailscale: open this link and approve it"
		say
		printf '      \033[1m%s\033[0m\n' "$url"
		say
		say "  (no Tailscale account yet? the link makes one, free)"
		printf '  Waiting'
		n=0
		while [ $n -lt 300 ]; do # 15 minutes
			sleep 3
			n=$((n + 1))
			[ $((n % 5)) = 0 ] && printf '.'
			out=$(address GET /v1/tailscale 2>/dev/null || true)
			[ "$(printf '%s' "$out" | field state)" != running ] || break
		done
		say
	fi
	TS_IP=$(printf '%s' "$out" | ts_ip)
	if [ -n "$err" ] || [ -z "$TS_IP" ]; then
		say "  Tailscale isn't connected yet${err:+: $err}"
		[ "$TAILNET" != 1 ] || say "  Run this command again to get a new sign-in link."
	else
		ok "On your tailnet as $(printf '%s' "$out" | field name | cut -d. -f1) ($TS_IP)"
		if [ "$PRIVATE" = 1 ]; then
			# Without a name, Pocket's own addresses are the tailnet's now.
			HOST=$TS_IP
			sedi "s/^POCKET_HOST=.*/POCKET_HOST=$TS_IP/" "$ENV"
			if [ -z "$(get POCKET_ADDRESS)" ]; then
				CORE_URL="http://$TS_IP:$PORT"
				sedi "s#^BASE_URL=.*#BASE_URL=$CORE_URL#; s#^INSTALLER_PUBLIC_URL=.*#INSTALLER_PUBLIC_URL=http://$TS_IP:{port}#" "$ENV"
				$DOCKER compose up -d core installer >>"$LOG" 2>&1
				wait_core
				wait_agent
			fi
		fi
	fi
fi
if [ -n "$ADDRESS" ] && { [ "$TAILNET" != 1 ] || [ -n "$TS_IP" ]; }; then
	wait_agent
	case "$ADDRESS" in
	cloudflare)
		zones=$(address POST /v1/address/cloudflare/zones "{\"token\":\"$CF_TOKEN\"}")
		zid=$(printf '%s' "$zones" | tr '{' '\n' | sed -n "s/.*\"id\":\"\([^\"]*\)\",\"name\":\"$CF_DOMAIN\".*/\1/p" | head -n1)
		[ -n "$zid" ] || die "Cloudflare: $(printf '%s' "$zones" | field error) (this token can't change $CF_DOMAIN)"
		out=$(address PUT /v1/address "{\"mode\":\"cloudflare\",\"token\":\"$CF_TOKEN\",\"zone_id\":\"$zid\",\"sub\":\"$CF_NAME\"}") ;;
	*) out=$(address PUT /v1/address "{\"mode\":\"$ADDRESS\"}") ;;
	esac
	err=$(printf '%s' "$out" | field error)
	[ -z "$err" ] || die "the address: $err"
	if [ "$ADDRESS" = plain ]; then
		ok "This network's addresses"
	else
		printf '  Getting your address and its certificate (a minute or two)'
		n=0
		while [ $n -lt 100 ]; do
			sleep 5
			n=$((n + 1))
			printf '.'
			out=$(address GET /v1/address 2>/dev/null || true)
			NAME=$(printf '%s' "$out" | field domain)
			[ -z "$NAME" ] || break
			case "$(printf '%s' "$out" | sed -n 's/.*"status":{"state":"\([a-z-]*\)".*/\1/p')" in
			failed | limited | signin | offline) say; say "  Not yet: $(printf '%s' "$out" | sed -n 's/.*"status":{[^}]*"message":"\([^"]*\)".*/\1/p') It keeps trying by itself."; break ;;
			esac
		done
		[ -z "$NAME" ] || { printf '\r\033[K'; ok "Address with a padlock: $NAME"; CORE_URL="https://core.$NAME"; }
	fi
fi

say
case "$CORE_URL" in https://*) say "  Pocket is ready: $(printf '\033[1m%s\033[0m' "$CORE_URL")" ;; *) say "  Pocket is running: $(printf '\033[1m%s\033[0m' "$CORE_URL")" ;; esac
say
if [ "$TAILNET" = 1 ]; then
	say "  Open it on a phone or laptop with Tailscale (same account)."
	[ -n "$TS_IP" ] || say "  First approve Pocket on Tailscale: run this command again for the link."
fi
if [ -n "$INSTALLED" ]; then
	say "  Create your account with $ADMIN_EMAIL; the apps are ready ($(printf '%s' "$INSTALLED" | sed 's/^ //; s/ /, /g'))."
elif [ "$NEW" = 1 ]; then
	say "  Create your account there, then pick your apps."
fi
[ -z "$FAILED" ] || say "  Not installed:$FAILED (try again from Core: Apps → Install app)."
[ -n "$DOMAIN" ] && say "  (core.$DOMAIN must point here with ports 80/443 open before you can sign in.)"
say
[ "$DOCKER" = docker ] || say "  Tip: log out and back in once, so Docker works without sudo."
[ -z "$POOLS_TIP" ] || [ "$NEW" = 0 ] || say "  Tip: Docker on this Mac makes networks for about 30 apps. For more: $POOLS_TIP
       \"default-address-pools\": [{ \"base\": \"172.16.0.0/12\", \"size\": 24 }]"
say "  Update: run this command again · Files: $DIR · Log: $LOG"
