#!/bin/bash

[ ! -f /etc/phion/barracudadefs ] && /opt/phion/bin/barracuda-key -b
source /etc/phion/barracudadefs

##########################################################
# implantVMSerial: write serial to disk for a VM
##########################################################
function implantVMSerial()
{
	SERIAL=${1}
	/etc/phion/bin/boxconf_rollout.net /opt/phion/run/appliance.state /tmp/.applstate.rollout.conf
	source /tmp/.applstate.rollout.conf
	/bin/rm -f /tmp/.applstate.rollout.conf

	# Attention: This check is removed in favor of ActivationAPI "Enter new License token"
	#            When re-enabling "closing" fi must be appended at the end of function
	# serial may be empty (non managed box!)
	# if [ -z "${box_SN}" ]; then
	
	# get the root partition and the disk
	DEV=""
	if [ -L /dev/disk/by-label/\\x2f ]; then
		DEV=$(readlink /dev/disk/by-label/\\x2f)
		DEV=${DEV:$((${#DEV}-4))}
		[ ! -z "${DEV}" ] && DEV="/dev/${DEV:0:3}"
		[ ! -b ${DEV} ] && DEV="" # use fallback
	fi
	# fallback
	if [ -z "${DEV}" ]; then
		DEV=$(cat /etc/mtab |grep -e '[ \t]/phion0[\t ]')
		DEV=${DEV:0:8}
	fi

	/opt/phion/bin/hwtool -w ${SERIAL} -d ${DEV}
	/opt/phion/bin/hwtool -e > /opt/phion/run/appliance.state 2>/dev/null
	[ -f ${BARRACUDA_DIR}/barrsec-${SERIAL}.key ] && /opt/phion/bin/barracuda-key -k ${BARRACUDA_DIR}/barrsec-${SERIAL}.key -d ${DEV}

	# for xen, the serial is not written onto the harddisk, but into a
	# separate file
	# in the future, the 3 lines above, might be included in the "else"
	# path of this if
	if [[ "${box_VIRTMETHOD:0:3}" == "XEN" ]]; then
		mkdir -p ${BARRACUDA_DIR}
		echo $SERIAL > ${BARRACUDA_DIR}/serial
		/opt/phion/bin/hwtool -e > /opt/phion/run/appliance.state 2>/dev/null
	fi
}

##########################################################
# implantSFSerial: write serial to /etc/barracuda/sfserial
##########################################################
function implantSFSerial()
{
	if [ ! -z "$1" ]; then
		# serial may be empty (non managed box!)
		echo "$1" > ${BARRACUDA_SFLIC_SERIALFILE}
		/opt/phion/bin/barracuda-key --writesflic
		echo "${box_SFMODEL}" > ${BARRACUDA_SFLIC_MODELFILE}
	fi
}

function cleanup()
{
	[ -f ${BARRACUDA_SFLIC_SERIALFILE} ] && rm -f ${BARRACUDA_SFLIC_SERIALFILE}
	[ -f ${BARRACUDA_SFLIC_KEYFILE} ] && rm -f ${BARRACUDA_SFLIC_KEYFILE}
}

function main()
{
	/etc/phion/bin/boxconf_rollout.net /opt/phion/config/active/box.conf /tmp/.boxconf.rollout.conf
	source /tmp/.boxconf.rollout.conf
	/bin/rm -f /tmp/.boxconf.rollout.conf

	# do nothing for single boxes, but only for main admin boxes
	# keys and serials for single boxes are written by implantVMSerial which is called from a control worker (triggered by a ngadmin call)
	[ "${box_MAINADMIN:-n}" != "y" ] && exit 0

	cleanup

	[ ! -d ${BARRACUDA_DIR} ] && mkdir ${BARRACUDA_DIR}

	if [ "${box_APPLIANCE:0:2}" = "VF" -o "${box_APPLIANCE:0:2}" = "VC" ]; then
		implantVMSerial ${box_SN}
	elif [ "${box_SFMODEL:0:2}" = "SF" -o "${box_SFMODEL:0:2}" = "SC" ]; then
		implantSFSerial ${box_SN}
	else
		exit 0
	fi
}

main

