#!/bin/sh
#
# Shell script to generate netboot distribution
#
# $Id: makedist,v 1.6 2006/07/28 10:56:44 gkminix Exp $

# We need the bash because of some shell variable substitutions
if [ -z $BASH_VERSION ]; then
	echo "$0 requires the GNU bash"
	exit 1
fi

# Check if we have to avoid recreating some files
NOCREATE=""
if [ "$1" = "--nocreate" ]; then
	NOCREATE="yes"
fi

# Determine the netboot base directory
case "$0" in
  */misc/makedist)	ROOTDIR="${0%/misc/makedist}"
			;;
  */makedist)		ROOTDIR="${0%/makedist}/.."
			;;
  *)			ROOTDIR=".."
			;;
esac
if [ ! -d $ROOTDIR -o ! -d $ROOTDIR/misc/config ]; then
	echo "$0: unable to determine netboot base directory"
	exit 1
fi
cd $ROOTDIR
ROOTDIR=`pwd`

# Check if we have to recreate the binary files
if [ "$NOCREATE" != "yes" ]; then
	# Determine current configuration and cleanup 
	./configure --sysconfdir=/etc --enable-bootrom \
		--cache-file=config.cache.tmp
	trap "rm -f $ROOTDIR/config.cache.tmp" 0
	make realclean

	# Restore configuration information
	cd $ROOTDIR/misc/config
	make all
	cd $ROOTDIR

	# Make everything
	./configure --sysconfdir=/etc --enable-bootrom \
		--cache-file=config.cache.tmp
	make distrib

	# Cleanup at the end
	make distclean
fi

# Create the distribution archive
. $ROOTDIR/version
if [ -n "$PATCHLEVEL" -a "$PATCHLEVEL" -gt 0 ]; then
	VERSION="$VER_MAJOR.$VER_MINOR.$PATCHLEVEL"
else
	VERSION="$VER_MAJOR.$VER_MINOR"
fi
cd $ROOTDIR/..
HOMEDIR=`pwd`
TARFILE="$HOMEDIR/netboot-$VERSION.tar.gz"
NBLINK="netboot-$VERSION"
ln -s netboot $NBLINK
tar --create --gzip --owner=0 --group=0 --numeric-owner \
	--exclude="CVS" --exclude="CVSROOT" \
	--dereference --file=$TARFILE $NBLINK

# Create the LSM file
DATE="`date +%m-%d-%Y`"
SIZE="`ls -l -h $TARFILE | cut -d" " -f 5 -s`"
sed "s/%version%/$VERSION/g;s/%date%/$DATE/g;s/%size%/$SIZE/g" \
	$ROOTDIR/misc/netboot.lsm.in >$HOMEDIR/netboot-$VERSION.lsm

