#!/bin/sh
# @(#)prginstall.cpp	1.71 revision of 98/02/13  11:47:14
# This is an unpublished work copyright (c) 1989 HELIOS Software GmbH
# 30827 Garbsen, Germany
set +vx

# @(#)HeliosVersion 2.5.0a2 Copyright 1989-1998 HELIOS Software Garbsen

# @(#)setup.cpp	1.28 revision of 98/02/09  19:27:44
PATH=$ESDIR:$ESDIR/etc:$PATH
export PATH
grop="macusers"
user="macuser"
hostupper=`hostname|sed 's/\..*//'|tr '[a-z]' '[A-Z]'` 2>/dev/null
if test -z "$hostupper"
then
	hostupper=`uname -n|sed 's/\..*//'|tr '[a-z]' '[A-Z]'`
fi
hostid=`machid`
# test how we can suppress the new line on echo
if test "X`echo -n`" = "X-n"
then
	en=''
else
	en='-n'
fi
if test "X`echo '\c'`" = "X\c"
then
	ec=''
else
	ec='\c'
fi










fstyp="-k -l"
Slevel=3000
Klevel=3000
rcdir=/etc










# @(#)line.cpp	1.9 revision of 93/04/20  22:29:16
# Berkeley has a stoneage bourne shell, so this mess is really necessary.
# this is also the reason not to use cat <<-eof to keep the code nicely
# indented.
status=0
line=""
gets=". /tmp/gets$$"
trap "rm -f /tmp/gets$$; exit \$status" 0 1 2 3 15
cat >/tmp/gets$$ <<\eof
line="!"
while test x"$line" = x"!"
do
	echo $en "* $p ? [$d] "$ec
	read line
	if test x"$line" = x"!"
	then
		$SHELL
	fi
done
if test -z "$line"
then
	line="$d"
fi
eof


cat <<eof
Setting up configuration files ...
eof

if test ! -f $ESDIR/conf/addressbook
then
	cp /dev/null $ESDIR/conf/addressbook
	chmod 0666 $ESDIR/conf/addressbook
fi

# create accounting files
if test ! -f $ESDIR/stmp
then
	cp /dev/null $ESDIR/stmp
	chmod 0666 $ESDIR/stmp
fi
if test ! -f $ESDIR/server.acct
then
	cp /dev/null $ESDIR/server.acct
	chmod 0664 $ESDIR/server.acct
fi
if test ! -f $ESDIR/printer.acct
then
	cp /dev/null $ESDIR/printer.acct
	chown daemon $ESDIR/printer.acct

	chgrp daemon $ESDIR/printer.acct

	chmod 0664 $ESDIR/printer.acct
fi

if test ! -f /etc/printcap
then
	cp /dev/null /etc/printcap
	chmod 0664 /etc/printcap
fi





# build a list of installed servers, this needs updating if we
# invent servers not ending in srv.
if test -f $ESDIR/conf/servers
then
	# make sure the new servers are in any existing servers file
	for i in ndsrv timesrv lpd
	do
		if test -f $ESDIR/$i
		then
			grep $i $ESDIR/conf/servers >/dev/null 2>&1
			if test $? -ne 0
			then
				echo $i >>$ESDIR/conf/servers
			fi
		fi
	done
else
	echo `eval echo "$ESDIR/*srv"` 2>/dev/null | tr ' ' '\012' | 		sed -e 's/.*\/\(.*\)/\1/' >$ESDIR/conf/servers
	if test -f $ESDIR/lpd
	then
		echo lpd >>$ESDIR/conf/servers
	fi
fi

max=0
mnt="/tmp"



mntpoint=`df $fstyp | sed 1d | ( while read x
do
	set $x
	if test "$4" -gt "$max"
	then
		max="$4"
		mnt="$6"
	fi
done
echo $mnt ) `




cat <<eof
Changing ESDIR variable in various shell scripts ...
eof


for i in `file * etc/* |egrep 'script|commands' |sed -n 's/\([^:]*\):.*/\1/p'`
do
	if test -f $i -a $i != install
	then
		sed -e "s,^ESDIR=.*,ESDIR=$ESDIR,g" $i >tmp$$
		mv tmp$$ $i
		chmod +x $i
	fi
done




# /bin/sh does not properly do getopts, but bash does...
for i in start-atalk stop-atalk etc/stop-afp etc/logrotate
do
	sed -e "1s,^#!/bin/sh,#!/bin/bash," $i >tmp$$
	mv tmp$$ $i
	chmod +x $i
done


if test -d $ESDIR/lib
then
	echo Performing symlinks from files in $ESDIR/lib to /usr/lib ...
	for i in $ESDIR/lib/*
	do
		case $i in
		*.helios)
			rm -f /usr/lib/libtoolkit_s.a
			ln -s $ESDIR/lib/libtoolkit_s.a /usr/lib
			;;
		*.apple|*.no*|*.bak)
			;;
		*)
			rm -f /usr/lib/`basename $i`
			ln -s $i /usr/lib
			;;
		esac
	done

fi

cat <<eof
Arranging for EtherShare to start automatically after the next reboot ...
eof





INITD=$rcdir/startup
rm -f $INITD/*EtherShare
cp "$ESDIR/etc/rc.atalk.rhappc" $INITD/${Slevel}_EtherShare





if test ! -f $ESDIR/conf/afpvolumes
then
cat <<eof

To install a shared volume, we recommend that you choose the local
partition from the following list with the biggest available free
space. Then use that partition's mount point (the rightmost column) as
the prefix to the ethershare directory to be created. Please make sure
that volumes do not overlap, e.g. do not make /home a volume if you 
want to access your home directory under /home/username.

eof
	df $fstyp
	if test "$mntpoint" = "/"
	then
		mntpoint=""
	fi
	p="Directory for shared volume" d="$mntpoint/ethershare" $gets
	sharevol="$line"
	if test ! -d "$sharevol"
	then
		p="Ok to create $sharevol" d="y" $gets
		if test "$line" = "y"
		then
			mkdir "$sharevol" "$sharevol/.rsrc"

			find "$sharevol" -exec chmod 0777 {} \;

			chmod g+s "$sharevol" "$sharevol/.rsrc"
		fi
	fi
	if test -d "$sharevol"
	then
		echo "$sharevol:EtherShare::fixed:readwrite" >$ESDIR/conf/afpvolumes
	fi
	echo "$ESDIR/macapps:EtherShare Applications::fixed:readwrite" >>$ESDIR/conf/afpvolumes
	chmod 0644 $ESDIR/conf/afpvolumes
else
ed - $ESDIR/conf/afpvolumes >/dev/null <<eof
/EtherShare Applications/d
a
$ESDIR/macapps:EtherShare Applications::fixed:readwrite
.
w
q
eof
fi

eif=`$ESDIR/etc/lsif`

if test ! -f $ESDIR/conf/atalk.conf
then
cat <<eof

Creating EtherShare configuration file $ESDIR/conf/atalk.conf.

eof

cat >$ESDIR/conf/atalk.conf <<eof
# Main AppleTalk configuration file
#
# The format is 
# prgname: parm=val, parm="val with spaces", flag
#
eof
	aif="atalkd:"
	first=y
	for i in $eif
	do
		if test $first = y
		then
			aif="$aif if=$i"
		else
			aif="$aif, if=$i"
		fi
		first=n
	done
cat >>$ESDIR/conf/atalk.conf <<eof
$aif
eof
	chmod 0644 $ESDIR/conf/atalk.conf
fi

# Setup an example suffixes file, just so it does not show up empty
if test ! -f $ESDIR/conf/suffixes
then
	echo "'PDF '  'CARO'  .pdf" >$ESDIR/conf/suffixes
fi

# Setup a minimal IP access list
if test ! -f $ESDIR/conf/afpipaccess
then
(
cat <<EOF
# AFP IP access control file
#
# The following statements are understood:
#
# allow ipaddr/mask
# deny ipaddr/mask
# allowdomain do.main
# denydomain do.main
#
# The domain forms are slow as they require a reverse DNS
# lookup, so use only if really necessary. If you leave out
# the mask, it is assumed to be 255.255.255.255.
#
EOF
$ESDIR/etc/ifstat -l
echo "deny 0.0.0.0/0.0.0.0"
) >$ESDIR/conf/afpipaccess
fi

# Setup PrnAdm and SysAdm
for grop in PrnAdm SysAdm
do

	nidump group . | grep "^$grop:" >/dev/null 2>&1

	if test $? -ne 0
	then
cat <<eof

The group $grop to allow non-root members to perform system
administration tasks using the EtherShare Admin program does
not exist.

eof
		p="Ok to create group $grop" d="y" $gets
		if test "$line" = "y"
		then

			nextgid=`nidump group .|awk -F: '{ if ($3 > x && $3 < 30000) x = $3 }; END { print x + 1 }'`

			if test "$nextgid" -lt 500
			then
				nextgid="500"
			fi

			echo "$grop:*:$nextgid:" | niload group .

			echo "Created group \"$grop\", use EtherShare Admin to add members."
		fi
	fi
done

# Check if the daily crontab entry to trim the printer.acct and
# server.acct files is present.


tabfile=/usr/lib/crontab
cline="0 0 * * * root $ESDIR/etc/es.daily"

grep 'es\.daily' $tabfile >/dev/null 2>&1
if test $? -ne 0
then
cat <<eof

The crontab entry to periodically clean up EtherShare log files does
not exist. This entry will make sure that the server.acct and
printer.acct files will not grow without bounds.
 
eof
		p="Ok to create the crontab entry" d="y" $gets
		if test "$line" = "y"
		then
			echo "$cline" >>$tabfile

		fi
else
	echo "Fixing crontab entry for the daily EtherShare cleanup ..."
ed - $tabfile >/dev/null <<eof
/es.daily/c
$cline
.
w
q
eof

fi


# Check for obsolete or moved files from earlier installations

OBS="LWFonts LWPlusFonts banner.ps binmode1.ps binmode2.ps binmodequery.ps"
OBS="$OBS fontlist.ps fontname fontname.c hide.ps forall mkafppasswd"
OBS="$OBS mount-afp pagecount.ps pstext.ps restart-pap rmtrash stop-adm"
OBS="$OBS stop-afp stop-pap unhide.ps yp-update etc/afpvolumes etc/atalk.conf"
OBS="$OBS etc/printcap.papif etc/printcap.psif etc/printcap.tcpif etc/what"
OBS="$OBS dicts/LinoDict_1_0_0 dicts/HeliosDict_1_0_0 dicts/HeliosDict_1_0_1"
OBS="$OBS dicts/HeliosDict_1_0_2"





DIDOBSDIR="n"

for i in $OBS
do
	if test -f $ESDIR/$i -o -d $ESDIR/$i
	then
		if test ! -d $ESDIR/obsolete
		then
			DIDOBSDIR="y"
			mkdir $ESDIR/obsolete
		fi
		mv $ESDIR/$i $ESDIR/obsolete
	fi
done

if test $DIDOBSDIR = "y"
then
cat <<eof

Moved obsolete files from previous EtherShare versions to
$ESDIR/obsolete. If you did not modify any of these, you
can safely delete this directory.

eof
fi
exit 0
