#!/bin/sh
#
# Copyright (c) 2002-2011 Steven Roberts <sroberts@fenderq.com>
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#

TAG="OPENBSD_4_8_BASE"
NAME="GENERIC.MP"
ARCH="i386"
DEST="/home/destdir"
RELEASE="/home/releasedir"
XSRCDIR="/usr/xenocara"
PORTSPATH="/usr/ports"
CVSROOT="anoncvs@anoncvs3.usa.openbsd.org:/cvs"

# See http://www.openbsd.org/anoncvs.html for instructions on fetching the
# sources for the first time.

update_sources() {
echo "---------- Update sources ----------"
echo "Update /usr/src ..."
cd /usr/src && cvs -d$CVSROOT up -r $TAG -Pd
sleep 3
echo "Update $XSRCDIR ..."
cd $XSRCDIR && cvs -d$CVSROOT up -r $TAG -Pd
sleep 3
echo "Update $PORTSPATH ..."
cd $PORTSPATH && cvs -d$CVSROOT up -r $TAG -Pd
sleep 3
}

build_kernel() {
echo "---------- Build and install a new kernel ----------"
cd /usr/src/sys/arch/$ARCH/conf
config $NAME
cd ../compile/$NAME
make clean && make depend && make
cp /bsd /obsd && cp bsd /
}

build_system() {
echo "---------- Build a new system ----------"
cd /usr/src && nice make obj
cd /usr/src/etc && export DESTDIR=/ && make distrib-dirs
cd /usr/src && nice make build
}

make_system_release() {
echo "---------- Make and validate the system release ----------"
export DESTDIR=$DEST; export RELEASEDIR=$RELEASE
rm -rf $DESTDIR
mkdir -p $DESTDIR $RELEASEDIR
cd /usr/src/etc && nice make release
cd /usr/src/distrib/sets && sh checkflist
unset DESTDIR RELEASEDIR
}

build_xenocara() {
echo "---------- Build and install xenocara ----------"
cd $XSRCDIR
make bootstrap
make obj
make build
}

make_xenocara_release() {
echo "---------- Make and validate the xenocara release ----------"
cd $XSRCDIR
export DESTDIR=$DEST RELEASEDIR=$RELEASE
rm -rf $DESTDIR
mkdir -p $DESTDIR $RELEASEDIR
nice make release
unset DESTDIR RELEASEDIR
}

usage() {
echo "  Usage: $0 <options>" 
echo 
echo "Options:"
echo 
echo "  update           - Update sources" 
echo "  kernel           - Build and install a new kernel"
echo "  system           - Build a new system"
echo "  system-release   - Make and validate the system release"
echo "  xenocara         - Build and install xenocara"
echo "  xenocara-release - Make and validate the xenocara release"
echo
}

if [ `whoami` != "root" ]; then
echo "You must be root to build a release." 
exit 1
fi

START=`date`
echo
echo ".: release.sh - building an OpenBSD release :."
echo "----------------------------------------------"
echo
echo "Version: $TAG"
echo " Kernel: $NAME-$ARCH"
echo "   Dest: $DEST"
echo "Release: $RELEASE"
echo

if [ $# = 0 ]; then usage; exit 1; fi

for i in $*
do
case $i in
update)
update_sources
;;
kernel)
build_kernel
;;
system)
build_system
;;
system-release)
make_system_release
;;
xenocara)
build_xenocara
;;
xenocara-release)
make_xenocara_release
;;
*)
echo "---------- Abort! Abort! ----------"
echo "Invalid option encountered: $i"
echo "Exiting......."
echo
exit 1
;; 
esac
done
echo
echo " Start Time : $START"
echo "Finish Time : `date`"
echo


