#!/bin/sh
#
# Copyright (c) 2004 - 2007 Michael Telahun Makonnen <mtm@FreeBSD.Org>
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#
# $Id$
#
# Initially obtained from jwd@FreeBSD.Org
#

exec 2>&1

#
# The following two directories determine where the release
# is built and where the ftp distribution and CD images
# will be copied, respectively.
#
RDIR=/var/scratch/release/build-chrootdir
TDIRPREFIX=/home/ftp/pub/FreeBSD/iso

CVSROOT=/home/ncvs; export CVSROOT
STARTTIME=`date`
NODOC="NODOC=YES"
NOPORTS="NOPORTS=YES NOPORTREADMES=YES"
LPDIR=/usr/a/relbuild/local_diff
LSF=/usr/a/relbuild/relscript.sh
LOCAL_PATCHES="LOCAL_PATCHES=${LPDIR}"
LOCAL_SCRIPT="LOCAL_SCRIPT=${LSF}"
_RD="RELEASEDISTFILES=/usr/ports/distfiles"
_NOPFD="NO_PREFETCHDISTFILES=YES"
_NORELUP=
_TGTARCH=`uname -p`
sflag=

# Exported variables to control release building
#
export EXTSRCDIR=/a/build/src
export PACKAGESITE=ftp://192.168.0.4/pub/FreeBSD/packages/amd64/

# Default to a -CURRENT build
RELINFO=
DASHROPT=
NODATE=""

bail() {
	exit $1
}

fixperms() {
	[ -n "$sflag" ] && return 0
	echo "Fixing permissions"
	find . -type f | xargs chmod 644
	find . -type d | xargs chmod 755
	return 0
}

mdir() {
	local dirs
	dirs="$*"

	echo -n "Making directories:"
	for d in $dirs ; do
		if [ ! -d "$d" ]; then
			if mkdir -p "$d" ; then
				echo -n " $d"
			else
				exit 1
			fi
		fi
	done
	echo '.'
}

### MAIN ###

# Parse command-line arguments
#
args=`getopt rsa:b: $*`
if [ $? -ne 0 ]; then
	echo "usage: $ME [-rs] [-b BRANCH]"
	echo "       -a target_arch : target architecture. Defaults to"
	echo "                        architecture of host machine."
	echo "       -b BRANCH : the branch/release to build. Default to HEAD."
	echo "       -r : make rerelease instead of make release"
	echo "       -s : sanitize by removing execute permission from binaries"
	echo "            that will end up on the release cdrom"
	exit 1
fi
set -- $args
for i ; do
	case "$i" in
	-a)
		_TGTARCH=$2
		shift;shift;;
	-b)
		RELINFO="RELEASETAG=$2"
		DASHROPT="-r $2"
		NODATE=true
		shift;shift;;
	-r)
		_NORELUP="RELEASENOUPDATE=YES"
		shift;;
	-s)
		sflag=yes
		shift;;
	esac
done

set -- `cvs -R -q co -p $DASHROPT src/sys/conf/newvers.sh | \
   grep -E '^REVISION=|^BRANCH=' | cut -f2 -d= | tr -d '"'`
REVISION="$1"
BRANCH="$2"
if ${NODATE:-false}; then
	TITLE="${REVISION}-$BRANCH"
else
	TITLE="${REVISION}-`date '+%Y%m%d'`-$BRANCH"
fi
LOGFILE=/tmp/$TITLE.log

KTRACECMD="ktrace -dif /tmp/$TITLE.ktrace.out" # if something needs tracing

TDIR="${TDIRPREFIX}/${_TGTARCH}"

update_release_cmd="echo 'Updating /usr/src/release source tree...' && cvs -R update -Pd"
_release="release"
if [ -n "$_NORELUP" ]; then
	update_release_cmd="true"
	_release="rerelease"
fi
echo "make $_release started: `date`" &&
mdir "$RDIR" "$TDIR" "$LPDIR"
[ ! -e "$LOCAL_PATCHES" ] && LOCAL_PATCHES=
[ ! -e "$LOCAL_SCRIPT" ] && LOCAL_SCRIPT=
(  echo "Doing nightly build attempt for $TITLE at `date`"
   cd /usr/src/release &&
   eval $update_release_cmd &&
   echo "Making ${_release}..." &&
   make         $_release                                    \
                ${NODOC}                                   \
		${NOPORTS}				   \
                CHROOTDIR=$RDIR                            \
                BUILDNAME=$TITLE                           \
                MAKE_ISOS=TRUE                             \
                __MAKE_CONF=NONE                           \
                $RELINFO                                   \
                TARGET_ARCH=${_TGTARCH}                         \
                $LOCAL_PATCHES				   \
		$LOCAL_SCRIPT				   \
		${_RD}					   \
		${_NOPFD}				   \
		${_NORELUP}				   \
)               > $LOGFILE          2>&1 &&
   echo "make release complete: `date`" &&
   echo exit 0 &&
   set -x &&
  (rm -rf $TDIR/$TITLE;
   mv $RDIR/R/ftp $TDIR/$TITLE) &&
   mv ${RDIR}/R/cdrom ${TDIR}/${TITLE} &&
   cd $TDIR/$TITLE &&
   fixperms &&
   echo ln -s ../../../extras/XF86336 XF86336 &&
   echo ln -s ../../../ports/i386/packages-4-stable packages &&
   tail -20 $LOGFILE &&
   echo "Release build of $TITLE was a success at `date`" &&
   bail 0

echo "FAILURE:" && echo "FAILURE:" && echo "FAILURE:" &&
echo "FAILURE: Release build of $TITLE failed." &&
echo "FAILURE:" && echo "FAILURE:" && echo "FAILURE:" &&
    tail -100 $LOGFILE &&
    bail 1
