#!/bin/bash

echo "do not use this"
echo "use buildGbibAndZip.csh instead"
exit 0

# this scripts starts the main gbib VM on hgwdev, creates an ssh key on it, adds this key to the build account
# the triggers an update on the box using the build account
# it finally cleans the VM, stops and zips it into gbibBeta.zip

set -o errexit
set -o pipefail

#if [ "$1" != "alpha" -a "$1" != "beta" ]; then
    #echo Please provide either alpha or beta as a parameter
    #exit 0
#fi

rel=beta

# make sure that the keys are always removed if this script somehow breaks
trap "echo cleaning keys; sed -i '/browserbox/d' ~/.ssh/authorized_keys" SIGHUP SIGINT SIGTERM EXIT

# start the box
runCount=`ps aux | grep 'VBoxHeadless -s browserbox'`
if [ "$runCount" == 1 ]; then
    VBoxHeadless -s browserbox &
    sleep 15
fi

echo logging into box and creating new public key
ssh box 'sudo shred -n 1 -zu /root/.ssh/{id_dsa,id_dsa.pub}; cat /dev/zero | sudo ssh-keygen -t dsa -f /root/.ssh/id_dsa -N ""'

echo adding the new key to the build key file on hgwdev
ssh box sudo cat /root/.ssh/id_dsa.pub >> ~/.ssh/authorized_keys

echo logging into box and starting the update process via the build account
ssh box sudo /root/updateBrowser.sh hgwdev build $rel

echo removing key from build key file and from box
ssh box 'sudo shred -n 1 -zu /root/.ssh/{id_dsa,id_dsa.pub}'
sed -i '/browserbox/d' ~/.ssh/authorized_keys

boxRelease.csh $rel
