#!/bin/sh
# @(#)usrinstall.cpp	1.15 revision of 98/02/09  19:29:13
# 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



nidump passwd . | grep "^$user:" >/dev/null 2>&1

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

The demo user $user does already exist.

eof
	exit 0
fi

echo
p="Ok to add a demo user named \"$user\" and a demo group \"$grop\"" d="y" $gets
if test "$line" != "y"
then
	exit 1
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 ) `




# put first users homedir to filesys with max space avail
while test -z "$homedir"
do
cat <<eof

Please select the home directory for the demo user.

eof
	df $fstyp
	if test "$mntpoint" = "/"
	then
		mntpoint=""
	fi
	p="Home directory for user $user" d="$mntpoint/$user" $gets
	if test X"$line" = Xq
	then
		exit 1
	fi
	homedir="$line"
	if test -d "$homedir"
	then
		echo "The directory $homedir does already exist."
		p="OK to use this directory" d="n" $gets
		if test X"$line" = Xy -o X"$line" = XY
		then
			:
		else
			homedir=""
		fi
	fi
done 


line=`nidump group .|grep "^$grop:" 2>/dev/null`

if test $? -ne 0
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\" for Macintosh users."
else
	nextgid=`echo $line|awk -F: '{ print $3 }'`
fi


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

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

echo "$user::$nextuid:$nextgid::0:0:Example Mac User:$homedir:/bin/sh" | niload passwd .

if test ! -d "$homedir"
then
	mkdir $homedir
	chown $user $homedir
	chgrp $grop $homedir
fi
if test ! -d "$homedir/.rsrc"
then
	mkdir $homedir/.rsrc
	chown $user $homedir/.rsrc
	chgrp $grop $homedir/.rsrc
fi

if test -f $ESDIR/conf/afppasswd
then
	grep "^$user:" $ESDIR/conf/afppasswd >/dev/null 2>&1
	if test $? -ne 0
	then
		echo "$user:" >>$ESDIR/conf/afppasswd
	fi
fi

cat <<eof

The test user "$user" has now been created. This user has no password
at present. This user should be deleted as soon as you have created
real users, for example by using EtherShare Admin.

eof

exit 0
