#!/bin/sh

#export LOGLEVEL=DEBUG

pkg install -y `cat bootstrap/freebsd/pkg-requirements.txt`

grep "^cloud:" /etc/passwd > /dev/null
ret=$?
if [ $ret -ne 0 ]
then
	# create the required backdoor user
	pw user add cloud -m
	pw group mod wheel -m cloud
fi

sed -i '.orig' -e 's/# \(%wheel ALL=(ALL) ALL\)/\1/g' /usr/local/etc/sudoers

if [ ! -d /usr/ports ]
then
	git clone https://github.com/HardenedBSD/freebsd-ports.git /usr/ports
fi

if [ ! -d /store ]
then
	mkdir -p /store
fi

if [ ! -d /root/agent ]
then
	cd /root
	git clone https://github.com/opntr/bme-cloud-circle-agent.git agent
fi

cp -r bootstrap/freebsd/rc.conf.d /etc

cd /root/agent
if [ -d /usr/local/etc/rc.d ]
then
	cp bootstrap/freebsd/rc.d/agent /usr/local/etc/rc.d
fi

if [ ! -f /etc/rc.conf.d/agent ]
then
	echo 'agent_enable="YES"' > /etc/rc.conf.d/agent
fi

service agent start