#!/bin/bash
#############################################
# VirtualBox vbox => template converter v0.1
# by ulisse aka A. Livio Beccaria
#
# It replaces some info in a vbox file to
# make it suitable to be used as a template
#############################################

# source folder
SRC_DIR="$(dirname $(readlink -f $0))"

#include common code
. ${SRC_DIR}/settings/common

[ "$#" -eq 1 ] || usage_vbox2template "Error: template file required."

VM_ID=$1

# include settings
. $SRC_DIR/settings/settings

TPL_FILE="$1"-tpl.vbox

# prepare template
cp "$1" "${TPL_FILE}"

sed -i 's:\(Machine.*snapshotFolder="\)\([^"]*\)\("\):\1'"`echo "${TMP_DIR}"`"'\3:g' "${TPL_FILE}"
sed -i 's:\(SharedFolder.*name="'"`echo "${SF_LINUX_NAME}"`"'".*hostPath="\)\([^"]*\)\("\):\1'"`echo "${SF_LINUX_TARGET}"`"'\3:g' "${TPL_FILE}"
sed -i 's:\(SharedFolder.*name="'"`echo "${SF_MEDIA_NAME}"`"'".*hostPath="\)\([^"]*\)\("\):\1'"`echo "${SF_MEDIA_TARGET}"`"'\3:g' "${TPL_FILE}"
sed -i 's:\(MACAddress="\)\([^"]*\)\("\):\1'"`echo ""`"'\3:g' "${TPL_FILE}"
#perl -pi -e 's:\(stateFile="\)\(.*?/\)":\1'"`echo "${IMG_HOME}/"`"'\3:g' "${TPL_FILE}"
perl -pi -e 's:\(stateFile="\)\(.*?/\)":\1'"`echo "${IMG_HOME}/"`"'\3:g' "${TPL_FILE}"


#if [ "${DIFF_DISK_FILE}" != "" ]; then
#    sed -i 's@\(HardDisk.*location="\)\([^"]*diff_[^"]*\)\("\)@\1'"`echo "${TMP_DIR}/${DIFF_DISK_FILE}"`"'\3@g' "${TPL_FILE}"
#fi

exit 0