#!/bin/sh
##
# build-NetBSD-domU.iso.sh -- build a complete, current and, or any
#    NetBSD distribution and a ready to use install CD/DVD  image, the
#    is know to work on Linux/*BSD :)
#
# features:
#    - includes GENERIC netbsd kernel
#    - includes a customized XEN DOMU kernel
#    - optimized XEN DOMU for high load internet servers
#    - includes or excludes X11
#
# (expected to run under each linux distribution and, or most unixes)
#
# Author: Niels Dettenbach <nd@syndicat.com>, http://dettenbach.de
#    v1.0rc2, Licensed under the GPL
# Modified: Balwinder S Dheeman <bdheeman@gmail.com> http://werc.homelinux.net
#    v1.0rc3, Refined the code, Updated mirrors list

## --- Local Configuration (Edit, if needed)
# target path of the netbsd build
#build within home directory (~/NetBSD-domU/)
BUILDPATH=$HOME

# select editor, if you want to further edit and, or review the kernel
# confiuration files, or comment the following line out ;)
#MEDITOR=${EDITOR:="nano"}

# select a CVS host
CVSHOST=anoncvs.NetBSD.org	# Master, IPv6
#CVSHOST=anoncvs.be.NetBSD.org	# Belgium
#CVSHOST=anoncvs.fr.NetBSD.org	# France, IPv6
#CVSHOST=anoncvs.de.NetBSD.org	# Germany, IPv6
#CVSHOST=anoncvs.jp.NetBSD.org	# Japan
#CVSHOST=anoncvs.ro.NetBSD.org	# Romania
#CVSHOST=anoncvs.se.NetBSD.org	# Sweden
#CVSHOST=anoncvs4.us.NetBSD.org	# US, IPv6

# target machine
MARCH=i386

# select NetBSD release, change this to select a particular netbsd
# release or branch builds a stable NetBSD 4.0 (latest stable)
#RELEASE=netbsd-4-0-RELEASE	# Latest in 4.0
#RELEASE=netbsd-4-0-1-RELEASE	# Oct 14, 2008
#RELEASE=netbsd-5-0-RELEASE	# Latest in 5.0
#RELEASE=netbsd-5-0-1-RELEASE	# Aug 02, 2009
RELEASE=netbsd-5-0-2-RELEASE	# Feb 12, 2010
# NetBSD current (devel branch)
#RELEASE=.

if [ 0 -eq 1 ]; then
## --- Main Code (MODIFY, ONLY IF YOU KNOW WHAT YOU'RE DOING)
echo "==> Preparing builddir $BUILDPATH/NetBSD-domU..."
mkdir -p $BUILDPATH/NetBSD-domU
cd $BUILDPATH/NetBSD-domU
mkdir -p kernel obj release src tools xsrc

# get extra kernel config for XEN DOMU internet servers
cd $BUILDPATH/NetBSD-domU/kernel
if [ ! -f XEN2_DOMU_NIELS ]; then
    echo "==> Downloading kernel/XEN2_DOMU_NIELS..."
    wget http://www.syndicat.com/pub/netbsd/kernel/XEN2_DOMU_NIELS
fi
if [ ! -f XEN3_DOMU_NIELS ]; then
    echo "==> Downloading kernel/XEN3_DOMU_NIELS..."
    wget http://www.syndicat.com/pub/netbsd/kernel/XEN3_DOMU_NIELS
fi

if [ -n "$MEDITOR" ]; then
    $MEDITOR $BUILDPATH/NetBSD-domU/kernel/XEN2_DOMU_NIELS
    $MEDITOR $BUILDPATH/NetBSD-domU/kernel/XEN3_DOMU_NIELS
fi

# cvs checkpout the current cvs sources
cd $BUILDPATH/NetBSD-domU
export CVSROOT=anoncvs@$CVSHOST:/cvsroot
export CVS_RSH=ssh
if [ ! -d src/CVS ]; then
    echo "==> Checking out src..."
    cvs co -r $RELEASE -P src || exit $?
else
    echo "==> Updating src..."
    cd src && cvs up -Pd || exit $?
fi
if [ ! -d xsrc/CVS ]; then
    echo "==> Checking out xsrc..."
    cvs co -r $RELEASE -P xsrc || exit $?
else
    echo "==> Updating out xsrc..."
    cd xsrc && cvs up -Pd || exit $?
fi

# link in custom kernel config
    echo "==> Symlinking kernel/XEN[23]_DOMU_NIELS..."
cd $BUILDPATH/NetBSD-domU
ln -fs $BUILDPATH/NetBSD-domU/kernel/XEN2_DOMU_NIELS \
    $BUILDPATH/NetBSD-domU/src/sys/arch/$MARCH/conf/
ln -fs $BUILDPATH/NetBSD-domU/kernel/XEN3_DOMU_NIELS \
    $BUILDPATH/NetBSD-domU/src/sys/arch/$MARCH/conf/

# run build.sh, see $BUILDPATH/NetBSD-domU/src/BUILDING for details about
# all the nice build.sh options
echo "==> Running src/build.sh..."
cd $BUILDPATH/NetBSD-domU/src
sh build.sh -m $MARCH -O ../obj -T ../tools -X ../xsrc/ -R ../release -j2 -U \
    -u -r tools kernel=XEN3_DOMU_NIELS release releasekernel=XEN3_DOMU_NIELS \
    iso-image || exit $?
fi
# show all generated kernels
echo "==> Listing generated kernels..."
cd $BUILDPATH/NetBSD-domU/release/$MARCH/binary/kernel/
pwd && ls -alh

# show all generated sets
echo "==> Listing generated insatll sets..."
cd $BUILDPATH/NetBSD-domU/release/$MARCH/binary/sets/
pwd && ls -alh

# show the install CD images
echo "==> Listing generated CD images (iso)..."
cd $BUILDPATH/NetBSD-domU/release/$MARCH/installation/cdrom
pwd && ls -alh
cd $BUILDPATH/NetBSD-domU/release/iso
pwd && ls -alh

# that's all
echo "==> Congratulations!"
echo "Burn a CD-ROM or boot from ISO and have fun with NetBSD-dom0 and domU!"
echo "Install notes $BUILDPATH/NetBSD-domU/release/$MARCH/INSTALL.\*"
