#!/bin/bash

#
# Simplistic script to install build environment for XCP xapi in a CentOS VM
# 
# Note that this does *not* build Xen nor a kernel suitable for XCP - for
# Xen a 64 bit build environment is required. 
# 
# Assumes root privileges. Only tested on a vanilla CentOS 5.4 install
# 
set -e
set -x

# Firstly install the required rpms and mercurial
# The particular version of mercurial is unimportant

yum install -y gcc autoconf automake tetex ghostscript java-1.6.0-openjdk java-1.6.0-openjdk-devel ant pam-devel python-devel zlib-devel openssl-devel dev86 rpm-build texinfo flex bison sharutils elfutils-devel ncurses-devel
rpm -e libaio || true

if [ ! -e mercurial-1.3.1.tar.gz ]; then
  wget http://mercurial.selenic.com/release/mercurial-1.3.1.tar.gz
fi
if [ ! $(which hg) ]; then
  echo Building hg
  tar xzf mercurial-1.3.1.tar.gz
  cd mercurial-1.3.1
  make install
  cd ..
fi

# Enable mercurial patch queue extension
if [ ! -e ~/.hgrc ]; then
  cat >> ~/.hgrc << EOF
[extensions]
hgext.mq = 
EOF
fi

# Set up bash_profile for mercurial and for xapi
cat >> ~/.bash_profile << EOF
PYTHONPATH=/usr/local/lib/python2.4/site-packages:${PYTHONPATH}
export PYTHONPATH
export PATH=${PATH}:/opt/xensource/bin
EOF

. ~/.bash_profile


REPOS="xen-3.4-testing.hg XCP/xen-3.4.pq.hg XCP/xen-dist-ocaml.hg \
       XCP/xen-api-libs.hg XCP/xen-api.hg"
for repo in ${REPOS}; do
  if [ ! -e $(basename ${repo}) ]; then
    hg clone http://xenbits.xensource.com/${repo}
  fi
done
cd xen-3.4-testing.hg/.hg
rm -f patches || true
ln -s ../../xen-3.4.pq.hg patches
cd ../..

SRPMS="x86_64-linux-binutils-2.17.50.0.6-6.xs81.src.rpm \
      x86_64-linux-gcc-4.1.2-42.xs81.src.rpm \
      x86_64-linux-gdb-6.5-37.xs81.2.src.rpm \
      x86_64-linux-glibc-2.5-24.xs81.src.rpm"
for srpm in ${SRPMS}; do
  if [ ! -e ${srpm} ]; then
    wget http://xenbits.xen.org/XCP/SRPMS/${srpm}
  fi
done

if [ ! -e xen-3.4-testing.build.patch ]; then
  echo I need the xen-3.4-testing.build.patch
  exit 1
fi 

if [ ! -e /usr/src/redhat/SOURCES/libconfig-1.3.2.tar.gz ]; then
  wget http://www.hyperrealm.com/libconfig/libconfig-1.3.2.tar.gz
  mv libconfig-1.3.2.tar.gz /usr/src/redhat/SOURCES
fi

if [ ! -e /usr/src/redhat/SOURCES/gdb-6.2.1.tar.bz2 ]; then
  wget http://ftp.gnu.org/gnu/gdb/gdb-6.2.1.tar.bz2
  mv gdb-6.2.1.tar.bz2 /usr/src/redhat/SOURCES
fi

echo All sources are available

# We need to build a 64-bit xen so we need a cross-compiler
if [ ! $(rpm -qa | grep x86_64-linux | grep gcc) ]; then
  rpmbuild --rebuild x86_64-linux-binutils-2.17.50.0.6-6.xs81.src.rpm
  rpmbuild --rebuild x86_64-linux-glibc-2.5-24.xs81.src.rpm
  rpm -ihv /usr/src/redhat/RPMS/noarch/*.rpm
  rpm -ihv /usr/src/redhat/RPMS/i386/*binutils*.rpm
  rpmbuild --rebuild x86_64-linux-gdb-6.5-37.xs81.2.src.rpm
  rpmbuild --rebuild x86_64-linux-gcc-4.1.2-42.xs81.src.rpm
  rpm -ihv /usr/src/redhat/RPMS/i386/*gcc*.rpm /usr/src/redhat/RPMS/i386/*cpp*.rpm
  rpm -ihv /usr/src/redhat/RPMS/i386/*gdb*.rpm
fi

if [ ! $(rpm -qa | grep xen-devel) ]; then
  cd xen-3.4-testing.hg
  hg revert --all
  rm -f mk/Makefile.xcp
  hg qpop -a
  hg update RELEASE-3.4.2
  hg qpush -a
  patch -p1 < ../xen-3.4-testing.build.patch

  cd mk
  make -f Makefile.xcp all
  rpm -ihv /usr/src/redhat/RPMS/i686/xen-{hypervisor,devel,firmware,tools}*.rpm
  cd ../..
fi

#export WGET=wget
#XEN_TARGET_ARCH=x86_64 CROSS_COMPILE=x86_64-linux- make xen
#make dist-tools
#cd dist/install
# Rsync removes the /etc/init.d symlink unless we do the
# next 2 lines
#mkdir -p etc/rc.d
#mv etc/init.d etc/rc.d || true
##rsync -av . /
#cd ../../..

# Clone the repository with ocaml and a few libraries
cd xen-dist-ocaml.hg
make
cd ..

# xen-api-libs.hg
cd xen-api-libs.hg
chmod 755 autogen.sh
./autogen.sh
./configure
./rebuild
cd ..

# xen-api.hg
cd xen-api.hg
make
cd ..

# At this point, we've built a xapi that can be put onto an XCP host
# We can also run it locally in SDK mode. This is useful for testing
# Uncomment this bit if you want this set up

#cd xen-api.hg
#make install
#cd dist/staging
#rsync -av . /
#cd ../..
#cat > /etc/xensource-inventory.skel << EOF
#PRODUCT_BRAND='XenCloudPlatform'
#PRODUCT_NAME='xcp'
#PRODUCT_VERSION='0.1.1'
#BUILD_NUMBER='1'
#EOF
#modprobe dummy 
#./scripts/init.d-sdkinit start
#/etc/init.d/fe start
#/etc/init.d/xenservices start
#/etc/init.d/xapi start
