#!/bin/sh
#
#ident	"@(#)pm.sh	1.15	93/12/21 SMI"
#
# Copyright (c) 1993 by Sun Microsystems, Inc.
#
# This script is called by the nightly build script to build packages.
#

USAGE='	usage: pm <env_file>'
#

# test that the path to the environment-setting file was given
if [ $# -ne 1 ]
then
	echo "$USAGE"
	exit 1
fi

# 	Setup environmental variables
. $1

if [ -d $PKGARCHIVE ]; then
	echo "Clearing out $PKGARCHIVE ..."
	rm -rf $PKGARCHIVE/*
else
	mkdir -p $PKGARCHIVE
	echo "Created $PKGARCHIVE"
fi

# check for source for make-driven package build
if [ -f $PKGDEFS/Makefile.com ]
then
	# prepare any i386 proto area with compensating binaries
	if [ "$MACH" = i386 ]
	then
		[ -f $ROOT/usr/lib/fs/ufs/bootblk ] || \
		  cp $CPIOTEMPLATE/blueboot $ROOT/usr/lib/fs/ufs/bootblk
		for F in mboot pboot
		do
			[ -f $ROOT/usr/lib/fs/ufs/$F ] || \
			  cp $CPIOTEMPLATE/$F $ROOT/usr/lib/fs/ufs
		done
		[ -f $ROOT/shlib/libc_s ] || \
		  (cd $ROOT; tar xpBf $CPIOTEMPLATE/shlib.tar)
		[ -d $ROOT/usr/lib/fs/nfs/drv.i86pc ] || \
		  mkdir $ROOT/usr/lib/fs/nfs/drv.i86pc
		for F in gluecode.com smc.com
		do
			[ -f $ROOT/usr/lib/fs/nfs/drv.i86pc/$F ] || \
			  cp $CPIOTEMPLATE/$F $ROOT/usr/lib/fs/nfs/drv.i86pc
		done
	fi
	# build packages using the makefiles
	PATH="/usr/sbin:/usr/ccs/bin:/usr/bin"
	export PATH
	cd $PKGDEFS
	make -e install
else
	# build packages the old way through a for-loop
	cd $PKGDEFS/pkgproto
	for PK in *
	do
BASEDIR=`grep "^BASEDIR=" $PKGDEFS/pkgproto/$PK/pkginfo | sed -e "s/^BASEDIR=//"`
	    SAVE=$ROOT
	    if [ $BASEDIR != "/" ]
	    then
		ROOT=$ROOT/$BASEDIR
	    fi
	    echo; echo "Packaging $PK"; echo
	    pkgmk -r $ROOT -d $PKGARCHIVE -f $PKGDEFS/pkgproto/$PK/prototype \
		-o $PK
	    ROOT=$SAVE
	done
fi
