#!/bin/sh
# vim: set noexpandtab tabstop=4 shiftwidth=4 softtabstop=4 :

set -e

# shellcheck disable=SC1091
. /lib/overthebox

remote_access_id="$(otb_json_get "$1" 'arguments.remote_access_id')"
uci_id=$(echo "$remote_access_id" | tr - _)

[ -e /etc/dropbear/authorized_keys ] && \
	sed -i "/$uci_id/d" /etc/dropbear/authorized_keys

luci_user=$(uci -q get otb-remote."${uci_id}".luci_user) || true

if [ -n "$luci_user" ]; then
	deluser "$luci_user"

	uci -q batch <<-EOF
		delete rpcd.${uci_id}
		commit rpcd
	EOF

	/etc/init.d/rpcd restart
fi

uci -q batch <<-EOF
	delete otb-remote.${uci_id}
	commit otb-remote
EOF

/etc/init.d/otb-remote restart
