#!/bin/sh
#
#ident		"@(#)mkclassproto.sh 1.3     94/03/09 SMI"
#
#  mkclassproto.sh
#
#	Copyright (c) 1994 by Sun Microsystems, Inc.

NAME=`basename $0`
FLIST=/tmp/$NAME.$$
BASEDIR=
SRCDIR=
PKGDIR=
ARCH=
VERBOSE=

#
# usage
#
usage () {
	echo "\
Usage:
	$NAME -n <newproto> -s <srcproto> -p <pkgdir> -a <arch> [-v]
"
	exit 1;
}

quit () {
	rm -f $FLIST
	exit $1
}

#
# main
#
while [ -n "$1" ]
do
	case $1 in
	  -n*)	BASEDIR=`expr $1 : '-b\(.*\)'`
		if [ -z "$BASEDIR" ]
		then
			shift; BASEDIR=$1
		fi
		shift;; 
	  -s*)	SRCDIR=`expr $1 : '-s\(.*\)'`
		if [ -z "$SRCDIR" ]
		then
			shift; SRCDIR=$1
		fi
		shift;;
	  -p*)	PKGDIR=`expr $1 : '-p\(.*\)'`
		if [ -z "$PKGDIR" ]
		then
			shift; PKGDIR=$1
		fi
		shift;;
	  -a*)	ARCH=`expr $1 : '-a\(.*\)'`
		if [ -z "$ARCH" ]
		then
			shift; ARCH=$1
		fi
		shift;;
	  -v)	VERBOSE=yes
		shift;;
	  *)	usage;;
	esac
done
test -z "$BASEDIR" -o -z "$SRCDIR" -o -z "$PKGDIR" -o -z "$ARCH" && usage

CPATH=`dirname $0`
test "$CPATH" != "." && PATH=$PATH:$CPATH

chkclasses -b. -c. -p$PKGDIR -a$ARCH -lq > $FLIST || quit 1
test -n "$VERBOSE" && echo "`wc -l $FLIST|awk '{ print $1 }'` files found."
test ! -d $BASEDIR && mkdir $BASEDIR
cat $FLIST | while read FILE
do
	set `echo $FILE | tr '/' ' '`
	P=$BASEDIR
	while [ -n "$2" ]
	do
		P="$P/$1"
		if [ ! -d $P ]
		then
			test -n "$VERBOSE" && echo "mkdir: $P"
			mkdir $P
		fi
		shift
	done
	test -n "$VERBOSE" && echo "cp: $SRCDIR/$FILE"
	cp $SRCDIR/$FILE $P
	chmod +r $P
done

quit 0
