#!/bin/sh

set -e

. debian/tests/functions.inc
trap 'service_status NOFAIL isnsd' EXIT

# (This script is loosely based on the isnssetup example in
# open-isns's source repository.)

# make sure isnsadm sees the own server's pubkey (the key
# was autogenerated at package installation)
cp /etc/isns/auth_key.pub /etc/isns/server_key.pub

hostname=127.0.0.1

rm -f /etc/isns/control.key

echo "Registering control node policy."
isnsadm --local \
	--keyfile=/etc/isns/control.key \
	--enroll isns.control \
	node-type=ALL functions=ALL object-type=ALL

echo "Registering control node."
isnsadm --local \
	--register control

# FIXME: do we actually need a restart here?
echo "Restarting isnsd."
service isnsd restart
# Unfortunately, isnsd forks before it's 100% initialized, so wait a
# bit before trying to connect to it again, otherwise this is subject
# to a race condition.
sleep 1

echo "Registering server policy (usiung DSA auth)."
isnsadm --control \
	--server $hostname \
	--enroll $hostname \
	key=/etc/isns/auth_key.pub \
	node-type=target+initiator

trap - EXIT
echo "Success."
