#!/bin/csh -f
# Partially Athenize an HP running AFS

echo "Making link for srvd..."
rm -f /srvd
ln -s /afs/athena.mit.edu/astaff/project/hp/srvd /srvd

echo "Linking /usr/athena..."
rm -f /usr/athena
ln -s /srvd/usr/athena /usr/athena

echo "Copying /etc/athena, /bin/athena, /usr/ucb..."
foreach dir (bin/athena etc/athena usr/ucb)
	rm -rf $dir
	(cd /srvd; tar cf - $dir) | (cd /; tar xf -)
	chown -R root /$dir
end

echo "Setuid programs..."
set echo
cp /usr/athena/bin/rcp /usr/ucb/rcp
chmod 4755 /bin/athena/attach /usr/ucb/rcp
chgrp daemon /usr/ucb/{lpq,lpr,lprm}
chmod 6755 /usr/ucb/{lpq,lpr,lprm}
unset echo

echo "Checking /etc/rc..."
set newrc = /etc/rc.Athenized
set oldrc = /etc/rc.preAthena
if (`grep -c /etc/athena/rc.athena /etc/rc`) then
	echo "/etc/rc already runs rc.athena"
else
	echo "Editing /etc/rc..."
	# Get first line of localrc() function
	set line1 = `grep -n ^localrc /etc/rc | awk -F: '{ print $1 }'`
	@ line2 = $line1 + 1;
	@ line3 = $line2 + 1;
	rm -f $newrc
	head -$line2 /etc/rc > $newrc
	cat >> $newrc << EOF
	if [ -f /etc/athena/rc.athena ]
	then
		sh /etc/athena/rc.athena
	fi
EOF
	tail +$line3 /etc/rc >> $newrc
	mv /etc/rc $oldrc && mv $newrc /etc/rc
endif

echo -n "Replacing files in /etc..."
foreach file (inetd.conf named.{boot,mit,root} resolv.conf services)
	cmp /srvd/etc/$file /etc/$file && continue
	echo -n $file ...
	if (-f /etc/$file) mv /etc/$file{,.preAthena}
	cp /srvd/etc/$file /etc/$file || mv /etc/$file{.preAthena,}
end
echo done

if (! -e /usr/lib/sendmail.cf) then
	echo "Copying sendmail.cf..."
	cp -p /usr/athena/lib/sendmail.cf /usr/lib/sendmail.cf
endif
if (! -x /usr/lib/sendmail) then
	echo "chmod 4755 /usr/lib/sendmail..."
	chmod 4755 /usr/lib/sendmail
endif

echo "Athenization finished."
