#!/usr/bin/env bash
# opensim service: the main process of opensim.service, its launch master in the foreground (ExecStart of the unit). What it starts,
# asked by opensim start from a shell, the engine API or the start at boot, is the service's, and stops with it.
# With --install, --remove or --print: the start at boot of an install that is not the package's (a clone), as the engine
# defines it: the unit of systemd (as root), on macOS the agent of launchd of the person.

USAGE='opensim service [--install | --remove | --print]'
HELP="Runs the launch master of opensim.service in the foreground: it listens on /run/opensim/launch.sock and starts the instances it is asked to, as children of the service. Run by systemd (systemctl start opensim), not by hand. From a clone, --install makes the grids start at boot (the unit of systemd, as root; on macOS an agent of launchd, at login), --remove takes that away, --print shows what --install writes."

source "$(dirname "$(realpath "${BASH_SOURCE[0]}" 2>/dev/null || echo "${BASH_SOURCE[0]}")")/../src/lib/common"

action=
for argument in "$@"; do
	case $argument in
	--install | --remove | --print) action=${argument#--} ;;
	esac
done
[[ -n "$action" ]] || exec php "$CLI_ROOT/vendor/way4os/w4os-engine/bin/w4os-launch-master.php"

command=$CLI_ROOT/bin/opensim
# What runs the grids here: launchd on macOS, systemd elsewhere
kind=${OPENSIM_SERVICE_KIND:-$([[ $(uname -s) == Darwin ]] && echo launchd || echo systemd)}

if [[ $kind == launchd ]]; then
	agents=${OPENSIM_LAUNCH_AGENTS:-$HOME/Library/LaunchAgents}
	agent=$agents/org.w4os.opensim.plist
	# The PATH of whoever installs it goes with it: launchd gives an agent the one of the system alone
	text=$(run boot agent "$command" "$HOME/Library/Logs/opensim-start.log" "$PATH") || exit $?
	case $action in
	print) printf '%s\n' "$text" ;;
	install)
		mkdir -p "$agents" && printf '%s\n' "$text" >"$agent" || die "could not write $agent"
		# Loaded where launchd reads it: the folder of the person's own agents, never another one
		if [[ "$agents" == "$HOME/Library/LaunchAgents" ]] && command -v launchctl >/dev/null 2>&1; then
			launchctl unload "$agent" 2>/dev/null || true
			launchctl load -w "$agent" || die "could not load $agent"
			say "$agent installed: the grids start when you log in"
		else
			say "$agent written, not loaded"
		fi
		;;
	remove)
		[[ ! -f "$agent" ]] || { [[ "$agents" != "$HOME/Library/LaunchAgents" ]] || launchctl unload -w "$agent" 2>/dev/null || true; rm -f "$agent"; }
		say "$agent removed"
		;;
	esac
	exit 0
fi

# The package has its own unit, enabled by its install: nothing of a clone over it
if [[ "$CLI_ROOT" == /usr/* || -n "${OPENSIM_SERVICE_PACKAGED:-}" ]]; then
	[[ $action == print ]] || die "opensim.service is the package's here."$'\n'"sudo systemctl enable --now opensim"
fi
units=${OPENSIM_UNIT_DIR:-/etc/systemd/system}
unit=$units/opensim.service
# The account of the instances: the one of the install, else whoever asks (root through sudo: the person who asked)
user=$(run system-user 2>/dev/null) || user=
if [[ -z "$user" ]]; then
	user=$(id -un)
	[[ $user != root || -z "${SUDO_USER:-}" ]] || user=$SUDO_USER
fi
[[ $user != root ]] || die "The grids run as an account of their own, never as root."$'\n'"sudo -u ACCOUNT $command service --install"
group=$(id -gn "$user") || die "no account $user here"
text=$(run boot unit "$command" "$user" "$group") || exit $?
case $action in
print) printf '%s\n' "$text" ;;
install)
	as_root mkdir -p "$units" || die "could not make $units"
	printf '%s\n' "$text" | as_root tee "$unit" >/dev/null || die "could not write $unit"
	as_root systemctl daemon-reload || die "systemd did not read $unit"$'\n'"systemctl status opensim"
	as_root systemctl enable opensim.service || die "opensim.service was not enabled"$'\n'"systemctl status opensim"
	say "opensim.service installed: the grids start at boot, as $user"
	;;
remove)
	if [[ -f "$unit" ]]; then
		# Its instances stop with it, in order
		as_root systemctl disable --now opensim.service || true
		as_root rm -f "$unit"
		as_root systemctl daemon-reload || true
	fi
	say "opensim.service removed"
	;;
esac
