trap "" 1
DESTDIR=${DESTDIR-/usr/dist3}
CC=cc

: make sure directory hierarchy exists
(	cd $DESTDIR
	for i in bin etc lib sys usr
	do	if [ ! -d $i ]
		then rm -f $i; mkdir $i
		fi
	done
	cd $DESTDIR/usr
	for i in bin dict games lib net spool ucb vpd
	do	if [ ! -d $i ]
		then rm -f $i; mkdir $i
		fi
	done
	cd $DESTDIR/usr/lib
	for i in learn lex lisp refer struct term tmac uucp
	do	if [ ! -d $i ]
		then rm -f $i; mkdir $i
		fi
	done
	cd $DESTDIR/usr/net
	for i in bin network
	do	if [ ! -d $i ]
		then rm -f $i; mkdir $i
		fi
	done
	cd $DESTDIR/usr/spool
	for i in lpd mail secretmail uucp uucppublic vpd
	do	if [ ! -d $i ]
		then rm -f $i; mkdir $i
		fi
	done
)
: cd ..

for i in $*
do 	echo "	======== $i"

	CFLAGS='-O -d2'
	case $i in
	512restor.c|analyze.c|colcrt.c|dump.c|dumpdir.c|egrep.y|fgrep.c|ncheck.c|ps.c|quot.c|restor.c|sa.c|sort.c) CFLAGS=-O ;;
	esac

	case $i in

	clean)	rm -f *.o core y.tab.c lex.yy.c  ;: Clean unwanted files.
		;;

	*.y)	B=`basename $i .y`
		eval D=`grep " $B\$" Admin/destinations`
		   yacc $B.y  \
		&& $CC $CFLAGS -o $B y.tab.c -lln \
		&& install -s $B $DESTDIR$D/$B
		rm -f y.tab.[co] $B
		;;

	*.l)	B=`basename $i .l`
		eval D=`grep " $B\$" Admin/destinations`
		   lex $B.l  \
		&& $CC $CFLAGS -o $B lex.yy.c -lln \
		&& install -s $B $DESTDIR$D/$B
		rm -f lex.yy.[co] $B
		;;

	clear.c|more.c|ul.c)
		B=`basename $i .c`
		eval D=`grep " $B\$" Admin/destinations`
		   $CC $CFLAGS -o $B $B.c -ltermlib \
		&& install -s $B $DESTDIR$D/$B
		rm -f $B.o $B
		;;

	*.c)	B=`basename $i .c`
		eval D=`grep " $B\$" Admin/destinations`
		   $CC $CFLAGS -o $B $B.c \
		&& install -s $B $DESTDIR$D/$B
		rm -f $B.o $B
		;;

	*.s)	B=`basename $i .s`
		eval D=`grep " $B\$" Admin/destinations`
		   as -o $B.o $B.s \
		&& $CC -o $B $B.o \
		&& install -s $B $DESTDIR$D/$B
		rm -f $B.o $B
		;;

	*.sh)	B=`basename $i .sh`
		eval D=`grep " $B\$" Admin/destinations`
		install -c $B.sh $DESTDIR$D/$B
		;;

	Admin)	echo Do nothing.
		;;


	MODES)
		BINF="	df login mail mkdir mv passwd rmdir su"
		UBINF="	at newgrp"
		UCBF="	chfn chsh netrm netq net renice"
		LIBF="	atrun ex3.2preserve ex3.2recover"
		UULIBF="uucp/uuxqt uucp/uucico"
		UUBINF="uucp uux uulog uuname"
		TMPF="	/tmp /usr/tmp /usr/lib/uucp/.XQTDIR /usr/msgs"
		TSPOOL="lpd mail secretmail uucp uucppublic"
		cd $DESTDIR/bin; chown root $BINF; chmod 4755 $BINF
		cd $DESTDIR/usr/bin; chown root $UBINF; chmod 4755 $UBINF
		cd $DESTDIR/usr/ucb; chown root $UCBF; chmod 4755 $UCBF
		cd $DESTDIR/usr/lib; chown root $LIBF; chmod 4755 $LIBF
		cd $DESTDIR/usr/lib; chown uucp $UULIBF; chmod 4755 $UULIBF
		cd $DESTDIR/usr/bin; chown uucp $UUBINF; chmod 4755 $UUBINF
		chmod 777 $DESTDIR/$TMPF 
		cd $DESTDIR/usr/spool; chmod 777 $TSPOOL ${DESTDIR}/usr/vpd

		;;

	ALIASES) echo Establish alias names.
		rm -f $DESTDIR/usr/bin/[ $DESTDIR/bin/rmail
		ln $DESTDIR/usr/bin/test $DESTDIR/usr/bin/[
		ln $DESTDIR/bin/mail $DESTDIR/bin/rmail
		rm -f $DESTDIR/usr/ucb/l $DESTDIR/usr/ucb/f $DESTDIR/usr/ucb/u
		ln $DESTDIR/usr/ucb/ls $DESTDIR/usr/ucb/l
		ln $DESTDIR/usr/ucb/finger $DESTDIR/usr/ucb/f
		ln $DESTDIR/usr/ucb/users $DESTDIR/usr/ucb/u
		;;

	*)	if [ ! -d $i ]
		then	echo "Don't know what to do with $i."
		else
			date
			cd $i
			   make CC=$CC DESTDIR=$DESTDIR \
			&& make install DESTDIR=$DESTDIR \
			&& make clean
			date
			cd ..
		fi

	esac
done
