#!/bin/sh
#
#

[ $# -ne 2 ] && {
    echo "Usage: $0 proto-area archive-home"
    exit 1
}
proto=$1
ahome=$2

[ -d $proto ] || {
	echo "Proto area directory does not exist."
	exit 1
}

[ -d "$ahome" ] || {
    echo "Archive target directory needs to exist."
    exit 1
}

cd $proto

echo "Creating generic root archive:\t\c"
(	FILELIST=`ls . | grep -v usr | grep -v platform | sed -e "s@^@./@`
	find $FILELIST -depth -print
	echo "./usr"
	echo "./platform"
) | cpio -ocB >${ahome}/generic.root

echo "Creating generic usr archive:\t\c"
(	FILELIST=`ls ./usr | grep -v platform | sed -e "s@^@./usr/@"`
	find $FILELIST -depth -print | grep -v "^./usr/share/lib/terminfo/"
	echo "./usr/platform"
) | cpio -ocB >${ahome}/generic.usr

echo "Creating terminfo archive (tar):"
tar cf ${ahome}/generic.terminfo ./usr/share/lib/terminfo

for i in `(cd platform; ls -l | grep '^d' | awk '{ print $NF }' )`
do
	echo "Creating $i root archive:\t\c"
	(	FILELIST=`ls -l ./platform | grep -w $i | \
		    sed -e "s/  */ /g" | cut -d' ' -f 9 | \
		    sed -e "s@^@./platform/@"`
		find $FILELIST -depth -print
	) | cpio -ocB >${ahome}/${i}.root
	echo "Creating $i usr archive:\t\c"
	(	FILELIST=`ls -l ./usr/platform | grep -w $i | \
		    sed -e "s/  */ /g" | cut -d' ' -f 9 | \
		    sed -e "s@^@./usr/platform/@"`
		if [ "$i" = "sun4m" ]
		then
			FILELIST="$FILELIST ./usr/platform/SUNW,SPARCstation-10,SX"
		fi
		find $FILELIST -depth -print
	) | cpio -ocB >${ahome}/${i}.usr
done
