#!/bin/sh -e
# $Id: nbbuild,v 1.19 2008/11/18 15:01:18 abs Exp $

# Simple script to build NetBSD plus (locally defined) useful kernels
# Cannot use release until problem overflowing tiny boot floppy resolved

TARGET=distribution # sets
VERSION=$(uname -r)

VERSION=$(case "$VERSION" in
    *.99.*) echo current ;;
    *) echo $VERSION | sed 's/\..*//' ;;
esac)

while getopts Chit:uv f; do
  case $f in
	C) VERSION=current	;;
	h) opt_h=1		;;
	i) opt_i=1		;;
	t) VERSION="$OPTARG"	;;
	u) opt_u=-u		;;
	v) opt_v=1		;;
    esac
done
shift `expr $OPTIND - 1`

if [ -n "$opt_h" ] ; then
    cat <<END
Usage: nbbuild [opts] [arch ...]
[opts]	-C     Build 'current' directory
	-h     This help
	-i     Build iso-image
	-t tag Build 'tag' directory
	-u     MKUPDATE=yes
	-v     Verbose (display build log to stdout)
END
    exit
fi

if [ -d /opt/netbsd ] ; then
    FILEDIR=/opt/netbsd
else
    FILEDIR=/home/netbsd
fi
NBHOME=$(cd $FILEDIR ; pwd)
export BSDSRCDIR=$NBHOME/$VERSION

build()
    {
    MACHINE=$1

    DISTFORMAT=$(determine_distformat $MACHINE)

    cd $BSDSRCDIR

    LASTMOD=$(stat -t %F -f %Sm CVS/Entries)
    OBJBASE=/var/obj/nbbuild-$VERSION-$MACHINE
    ARCHDIR=$NBHOME/arch/$VERSION-$MACHINE-$LASTMOD
    LOG=$OBJBASE/nbbuild.log
    EXTRA_KERNELS="$(ls sys/arch/$MACHINE/conf | grep '^_' || true)"

    if [ -z "$opt_u" ]; then
	rm -rf $OBJBASE
    fi
    mkdir -p $OBJBASE

    BUILDSH="./build.sh -m $MACHINE \
	    -D $OBJBASE/destdir \
	    -O $OBJBASE/objdir \
	    -T $OBJBASE/tooldir \
	    -R $OBJBASE/releasedir"
    BUILD="$BUILDSH -U $opt_u release"
    if [ -n "$opt_i" ]; then
        BUILD="$BUILD iso-image"
    fi
    for extra_kernel in $EXTRA_KERNELS; do
	BUILD="$BUILD kernel=$extra_kernel"
    done
    echo $BUILD
    if [ -n "$opt_v" ] ; then
	COMPLETE=$OBJBASE/ok
	($BUILD 2>&1 && touch $COMPLETE) | tee $OBJBASE/nbbuild.log
	[ ! -f $COMPLETE ] && exit
    elif ! $BUILD > $OBJBASE/nbbuild.log 2>&1 ; then
	tail -60 $LOG
	exit 1
    fi

    echo -n "Recompress to $ARCHDIR:"
    mkdir -p $ARCHDIR

    for extra_kernel in $EXTRA_KERNELS; do
	cd $OBJBASE/objdir/sys/arch/$MACHINE/compile/$extra_kernel
        tar cf - ./netbsd | gzip -9 > $OBJBASE/releasedir/$MACHINE/binary/sets/kern-$extra_kernel.tgz
    done

    cd $OBJBASE/releasedir/$MACHINE/binary/sets
    for f in *.tgz ; do
	copy_compress $f $ARCHDIR/$(echo $f|sed "s/\.tgz$/.$DISTFORMAT/")
    done

    if [ -n "$opt_i" ]; then
      cp $OBJBASE/releasedir/images/*.iso $ARCHDIR
    fi

    echo

    if [ -z "$opt_u" ]; then
	rm -rf $OBJBASE
    fi
    }

copy_compress()
    {
    from=$1
    to=$2
    echo -n " $(basename $to)"
    case $from in
	*.tgz) from_cat="zcat $from" ;;
	*)     from_cat="cat $from"  ;;
    esac
    rm -f $to
    case $to in
	*xz) $from_cat | xz -9    > $to ;;
	*bz) $from_cat | bzip2 -9 > $to ;;
	*gz) $from_cat | gzip -9  > $to ;;
	*)     echo "Unable to determine format of $to"; exit 1 ;;
    esac
    }

determine_distformat()
    {
    if [ $1 = vax ]; then
	echo tgz
	return
    fi

    if [ $VERSION = current ] || [ $VERSION -gt 5 ] ; then
        mem=`echo $(( $(sysctl -n hw.usermem64) / 1024 / 1024 ))`
        if [ $mem -gt 1500 ] ; then
	    echo txz
	    return
	fi
    fi
    echo tbz
    }

if [ -z "$1" ] ; then
    build $(uname -m)
else
    for m ; do
	build $m
    done
fi
