#!/bin/sh

. ./scripts/parse-config

if [ "$arch" = any ] ; then
	echo
	echo "ERROR: No architecture configured."
	echo
	echo "Type './scripts/Help Config' for details."
	echo
	exit 1
fi

if ! expr "$1" : '[12345Ee]' > /dev/null ; then
	echo
	echo "Usage: ./scripts/Build-Stage  1 | 2 | 3 | 4 | 5 "
	echo
	exit 1
fi

rm -f src/temp.stage-abort

# Build the base packages
# (z are the dRock 2nd CD packages
#
grep '^[xoz]' scripts/packages | cut -d ' ' -f 1,2,5 | \
while read isopt stages pkg ; do
    if [ $buildoptpkgs = 1 -o $isopt = x ] &&
       expr "$stages" : ".*$1.*" > /dev/null ; then
	    if [ $makerebuild != 0 ] ; then
		last=`echo $stages | tr -d '-' | sed 's,.*\(.\),\1,'`
	    else
		last=`echo $stages | tr -d '5-' | sed 's,.*\(.\),\1,'`
	    fi
	    if [ $last = $1 -o $crossnative = cross ]
	    then maketar="-maketar" ; else maketar="" ; fi
	    echo ""
	    ./scripts/Build-Pkg -$1 -norebuild $maketar $pkg
	    if [ $? -ne 0 -a $abortonerror = 1 ] ; then
		touch src/temp.stage-abort ; break
	    fi
    fi
done

if [ -f src/temp.stage-abort ] ; then
	rm -f src/temp.stage-abort ; exit 1
fi

exit 0
