#!/bin/bash
#
# Create a basic but functional kxd configuration.
#
# This script creates the /etc/kxd directory, and generates a certificate for
# the server to use.
#
# It should be run under the same user as kxd itself.

if [ "$1" == "" ]; then
	echo "Usage: $0 <hostname>"
	exit 1
fi

set -e

# Create the base configuration directory.
echo "Creating directories (/etc/kxd/)"
mkdir -p /etc/kxd/

# And the data directory where the keys are stored.
mkdir -p /etc/kxd/data

# Create a private key for the server.
if ! [ -e /etc/kxd/key.pem ]; then
	kxgencert \
		-host "${1?}" \
		-organization "kxd@$HOSTNAME" \
		-key /etc/kxd/key.pem \
		-cert /etc/kxd/cert.pem
	chmod 400 /etc/kxd/key.pem
else
	echo "Private key already exists (/etc/kxd/key.pem)"
fi
