#!/bin/sh
#	NewSys new [old]
# Make a new SNMP build directory for the "new" system.  If "old" is given,
# it will be used as a prototype; else "sys" will be used as a default.
#
if [ $# -lt 1 ];then echo Usage: $0 new '[old]'; exit 1; fi
case "$1" in
	-[Ll]) L=1; shift ;;
esac
new=$1
old=$2
test -n "$old" || old=sys
umask 2

echo New system: $new
echo Old system: $old
#
# Make sure the new system has a directory.
cd /usr/nm/snmp
if [ -d $new ]
then echo The $new directory already exists.
else mkdir $new
fi
#
# There are a few subdirectories that are needed:
for d in c d h SCCS sh
do	if [ -d $old/$d ]
	then echo The $old system has a $d directory.
		test -d  $new/$d || mkdir $new/$d
		if [ -n "$L" ]	# Link or copy?
		then 
			echo "Link contents of $old/$d to $new/$d ..."
			(cd $old/$d; find . -print | cpio -pdkl ../../$new/$d)
		else 
			echo "Copy contents of $old/$d to $new/$d ..."
			(cd $old/$d; find . -print | cpio -pdk  ../../$new/$d)
		fi
	fi
done
#
# Next, run thru the files in $old, and handle all the interesting ones:
(	cd $old
	ls -altr \
	| sed -e 's/.*[0-9][0-9]:*[0-9][0-9][ 	]//'
) | {
	while read l		# One line from listing.
	do	# echo "      '"$l"'"
		case "$l" in	# Branch on line format.
		  'total '*)	# Drop the "total " line.
			echo 'Ignore '$l
			continue;;
		  *.[aio])		# These types should be ignored.
			echo 'Ignore '$l
			continue;;
		  .login|.profile)	# Carry these along.
			echo 'Copy:  '$l
			cp $old/$l $new/$l
			continue;;
		  *' -> '*)		# Symbolic link.
			f=`expr "$l" : '\(.*\) -> .*'`
			g=`expr "$l" : '.* -> \(.*\)'`
			case $g in
			  $old'_'*) 
				g=$new'_'`expr "$l" : '.* -> '$old'_\(.*\)'`
				;;
			esac
			echo 'Link:  '$f' -> '$g
			(cd $new; ln -s $g $f)
			continue;;
		  $old'_'*)		# Prefixed with system type.
			g=$new'_'`expr "$l" : $old'_\(.*\)'`
			echo 'Copy:  '$l' -> '$g
			cp $old/$l $new/$g
			continue;;
		  *)			# Miscellaneous files.
			echo 'Ignore '$l
			;;
		esac
	done
}
# The makefile should have appropriate embedded system types modified.
if [ -f $new/Makefile ]
then
	{	echo 'g/'$old'/s//'$new'/gp'
		echo 'w'
		echo 'q'
	} | ed - $new/Makefile
else
	echo Warning: $new contains no Makefile.
fi

echo '+----------------------------------------------------+'
echo '| Please edit Makefile and local.h before compiling. |'
echo '| In particular, local.h may need many changes.      |'
echo '+----------------------------------------------------+'
