#!/bin/sh -x

# usage: xen-clone hg_repository dest_dir orig_linux_dir
# 
# this script contains some CL site specific details, but can easily be adapted
#

# test which site we're on
[ -d /usr/groups/xeno/ -a -d /usr/groups/srgboot ] && SITE=UCCL

case "$SITE" in
UCCL)
	BK_REP=${1:-http://hg.srg.cl.cam.ac.uk/xen-unstable.hg}
	LINUX_DIR=${3:-/usr/groups/xeno/archive/}
	;;
*)
	BK_REP=${1:-http://xenbits.xensource.com/xen-unstable.hg}
	LINUX_DIR=${3:-.:..}
;;
esac

DEST_DIR=${2:-xeno-clone}
DEST_BK_REP=`basename "${BK_REP}"`
DEST_VER=`basename ${DEST_BK_REP} .hg`

echo usage: xen-clone hg dest_dir orig_linux_dir
echo Source BK Repository : ${BK_REP}
echo Destination Dir/Repository : ${DEST_DIR}/${DEST_BK_REP}
echo Pristine Linux Source directory : ${LINUX_DIR}

mkdir -p ${DEST_DIR}
cd ${DEST_DIR}
TOP=`/bin/pwd`

# site-specific set up of installation directories
case "$SITE" in
UCCL)
	PATH=$PATH:/usr/groups/xeno/build_tools/bin
	mkdir -p install/boot
	cd install/boot
	ln -sf ../../../xeno-roots/roots .
	ln -sf ../../../xeno-roots/usr .
	ln -sf ../lib .
	ln -sf ../bin .
	ln -sf /usr/groups/srgboot/${USER}/xenoboot.sh .
	ln -sf `pwd` /usr/groups/srgboot/${USER}/${DEST_DIR}
	ln -sf xen.gz image.gz
	cd ../..
	;;
esac

# clone the master repository (now checked-out by default)
if [ ! -d ${DEST_BK_REP} ] 
then 
mkdir -p ${DEST_BK_REP} ; cd ${DEST_BK_REP} ; hg init ${BK_REP} ; hg co ; cd ${TOP}
else
cd ${DEST_BK_REP}
hg pull ; hg co
cd ${TOP}
fi


if [ -d ${DEST_BK_REP}/linux-2.4*-xen-sparse ]
then
 # this is a new style Xen repository so building is dead easy

 export LINUX_SRC_PATH=${LINUX_DIR}

 cd ${DEST_BK_REP} 

 # Recent repositories install into 'dist/install' rather than 'install'.
 if [ -f install.sh ]
 then
  mkdir -p dist
  ln -sf ../../install dist/install
 else
  ln -sf ../install install
 fi

 make -j4 KERNELS=linux-* world
 #make -j4 linux24
 cd ../install/boot
 if [ -r vmlinuz-2.6-xen0 ]
 then
  ln -s vmlinuz-2.6-xen0 xenolinux.gz
 else
  kern=`ls vmlinuz-2.6.*-xen0 | head -1`
  [ -r "$kern" ] && ln -s "$kern" xenolinux.gz
 fi  

else
 # old style repository without 'make world'


 # identify this version of linux
 LINUX_VER=`( /bin/ls -ld ${DEST_BK_REP}/*xenolinux-sparse || /bin/ls -ld ${DEST_BK_REP}/*xenolinux-*-sparse ) 2>/dev/null | sed -e 's!^.*xenolinux-\(.\+\)-sparse!\1!'`

 if [ -z "${LINUX_VER}" ]
 then
 echo Unable to identify Linux version. Bailing.
 exit -1
 fi

 # copy in the master Linux tree for this kernel
 if [ ! -d linux-${LINUX_VER} ]
 then
 tar -jxf ${LINUX_DIR}/linux-${LINUX_VER}.tar.bz2 || tar -zxf ${LINUX_DIR}/linux-${LINUX_VER}.tar.gz || tar -zxf ${LINUX_DIR}/linux-${LINUX_VER}.tgz || cp -a ${LINUX_DIR}/linux-${LINUX_VER} . ||  wget ftp://ftp.kernel.org/pub/linux/kernel/v2.4/linux-${LINUX_VER}.tar.gz -O- | tar -zxf - || exit -1
 fi

 # build and install Xen and tools
 cd ${DEST_BK_REP}
 make dist || make install

 # Turn linux into xenolinux then build it
 cd xenolinux-${LINUX_VER}-sparse
 bash ./mkbuildtree ../../linux-${LINUX_VER}
 cd ../..
 mv linux-${LINUX_VER} xenolinux-${LINUX_VER}
 cd xenolinux-${LINUX_VER}

 # cope with the change from ARCH=xeno to ARCH=xen
 cd arch; XEN=`/bin/ls -d xen*`; cd ..

 # built it all
 ARCH=$XEN make oldconfig
 ARCH=$XEN make dep
 ARCH=$XEN make bzImage
 ARCH=$XEN make dist || ARCH=xen make install
 ARCH=$XEN make modules
 ARCH=$XEN make INSTALL_MOD_PATH=${TOP}/install modules_install
 cd ..

fi

