#!/bin/bash -e

apt-get update
apt-get install -q -y python-software-properties
apt-add-repository 'deb http://archive.ubuntu.com/ubuntu precise-backports main restricted universe multiverse'
apt-get update
apt-get install -q -y 'sks=1.1.3-1ubuntu2~ubuntu12.04.1'

# Enable SKS service
sed -i 's/initstart=no/initstart=yes/' /etc/default/sks

# Write a basic sksconf
if [ -e "/etc/sks/sksconf" ]; then
	mv /etc/sks/sksconf /etc/sks/sksconf.$(date +%s).sks-charm.bak
fi

cat >/etc/sks/sksconf <<EOF
hostname: $(unit-get public-address)

hkp_address: 0.0.0.0
hkp_port: 11371

recon_address: 0.0.0.0
recon_port: 11370

pagesize: 128
ptree_pagesize: 16
cache: 64
diskptree:
EOF

if [ ! -d "/var/lib/sks/DB" ]; then
	su -l debian-sks -c "/usr/sbin/sks build"
fi
