#!/bin/sh
# proxy-agent-setup: show the consent text, ask for agreement, then register
# the agent as a system service. The .deb and .rpm only lay down files; this
# script is the step that starts sharing, so nothing shares a connection
# before its owner has agreed.
#
#   sudo proxy-agent-setup                  ask (needs a terminal)
#   sudo proxy-agent-setup --accepted       the caller already showed the text
#                                           and got an explicit yes (install.sh)
#   sudo proxy-agent-setup --show-consent   print the text and exit
#
# Environment:
#   PROXY_AGENT_LANG            en or vi (default: from LANG)
#   PROXY_AGENT_CONTROL_USERS   accounts that may pause and limit sharing
#                               (default: the account running sudo)
set -eu

PREFIX=/usr/share/proxy-agent
# release.env is written when the package is built (deploy/agent-pkg).
# shellcheck source=/dev/null
. "$PREFIX/release.env"

die() {
	echo "proxy-agent-setup: $*" >&2
	exit 1
}

lang=${PROXY_AGENT_LANG:-}
case "$lang" in
en | vi) ;;
*) case "${LC_ALL:-${LC_MESSAGES:-${LANG:-}}}" in vi* | VI*) lang='vi' ;; *) lang='en' ;; esac ;;
esac
consent=$PREFIX/consent/$CONSENT_VERSION.$lang.txt
[ -f "$consent" ] || consent=$PREFIX/consent/$CONSENT_VERSION.en.txt

accepted=0
case "${1:-}" in
--show-consent)
	cat "$consent"
	exit 0
	;;
--accepted) accepted=1 ;;
"") ;;
*) die "unknown option $1" ;;
esac

[ "$(id -u)" -eq 0 ] || die "run with sudo"

if [ "$accepted" -eq 0 ]; then
	cat "$consent"
	echo
	if [ -r /dev/tty ]; then
		printf 'Do you agree? Bạn có đồng ý không? [y/N] '
		read -r answer </dev/tty || answer=
	else
		die "no terminal to ask on; read the text above, then run: sudo proxy-agent-setup --accepted"
	fi
	case "$answer" in
	y | Y | yes | YES | c | C | co | có | Có) ;;
	*) die "not accepted: nothing was configured and the agent shares nothing" ;;
	esac
fi

# A record for the owner and for support. The evidence that counts is stored
# by the pairing API when "agent pair" is run, which asks again.
mkdir -p /etc/proxy-agent
umask 022
cat >/etc/proxy-agent/consent-accepted <<EOF
version=$CONSENT_VERSION
language=$lang
accepted_at=$(date -u +%Y-%m-%dT%H:%M:%SZ)
agent_version=$AGENT_VERSION
EOF

users=${PROXY_AGENT_CONTROL_USERS:-${SUDO_USER:-}}
if [ -z "$users" ]; then
	users=$(logname 2>/dev/null || true)
fi
case "$users" in root | "") users="" ;; esac

# -control-group is given explicitly: the agent's Linux default is
# "proxy-agent", the same name the unit's DynamicUser= wants for its
# transient user and group, and systemd refuses to start a unit whose
# dynamic name is already taken (status=217/USER). Keep this in step with
# CONTROL_GROUP_LINUX in deploy/agent-pkg/release.conf and with the group
# that postremove.sh deletes.
set -- service install \
	-server "$EDGE_SERVER" \
	-ca "$PREFIX/edge-ca.pem" \
	-pair-api "$PAIR_API" \
	-update-url "$UPDATE_URL" \
	-update-channel "$UPDATE_CHANNEL" \
	-control-group proxy-agent-users
[ -n "$users" ] && set -- "$@" -control-users "$users"

echo "== registering the proxy-agent service"
/usr/bin/proxy-agent "$@"
/usr/bin/proxy-agent service start || true

cat <<EOF

Next: pair this machine with your partner account.

  sudo proxy-agent pair -api $PAIR_API -code <PAIRING CODE>

Get the code in the partner portal ($PORTAL_URL).
EOF
