#!/bin/sh

# Build the XenCloudPlatform repos in a build appliance

set -e

UPSTREAM_URL=http://xenbits.xensource.com/XCP
REPO=/root/repo

if [ -z "$(cat /etc/redhat-release | grep 'XenCloudPlatform')" ]; then
   echo WARNING: this does not appear to be a XenCloudPlatform build appliance
fi

echo "Placing repositories in ${REPO}"
mkdir -p ${REPO}

for repo in xen-dist-ocaml.hg xen-api-libs xen-api; do
    if [ ! -e "$REPO/$repo" ]; then
		echo Cloning $UPSTREAM_URL/$repo to $REPO/$repo;
		if echo $UPSTREAM_URL/$repo | grep -E "\.hg$" > /dev/null; then
			hg clone $UPSTREAM_URL/$repo $REPO/$repo;
		else
			git clone $UPSTREAM_URL/$repo $REPO/$repo;
		fi;
	else
		echo "$REPO/$repo already exists; no need to re-clone";
	fi;
done

echo "Building ocaml packages"
cd $REPO/xen-dist-ocaml.hg
make
make clean
echo "Building XCP-specific libraries"
cd $REPO/xen-api-libs
./rebuild
echo "Building XCP toolstack"
cd $REPO/xen-api
COMPILE_JAVA=no make
COMPILE_JAVA=no make install
echo "XCP toolstack in $REPO/xen-api/dist/staging"
