# $Copyright:	$
# Copyright (c) 1984, 1985, 1986, 1987, 1988, 1989, 1990 
# Sequent Computer Systems, Inc.   All rights reserved.
#  
# This software is furnished under a license and may be used
# only in accordance with the terms of that license and with the
# inclusion of the above copyright notice.   This software may not
# be provided or otherwise made available to, or used by, any
# other person.  No title to or ownership of the software is
# hereby transferred.
#ident	"$Header: modadduser 1.3 89/10/10 $@(#)sadmin:admin/menu/usermgmt/modadduser	2.3"
#menu# modify defaults used by adduser
#help# 
#help#	Modadduser allows you to change some of the defaults used when
#help#	adduser creates a new login.  Changing the defaults does not
#help#	effect any existing logins, only ones made from now on.

defgroup=1	# Default group, "other"
defparent=/usr	# Default parent of login directory
loginlen=8	# Maximum login ID length
minid=100	# Minimum user and group ID
maxid=50000	# Maximum user and group ID

if [ -r ${MENUTOP:?}/defadduser ]
then	# This file allows easy modification of the defaults.
	. ${MENUTOP}/defadduser
fi
trap 'exit 0' 1 2 15
flags="-qq -k$$"

echo '
Anytime you want to quit, type "q".
If you are not sure how to answer any prompt, type "?" for help.
'
defgroupname=`cut -d: -f1,3 /etc/group  |  grep ":${defgroup}$"  |  cut -d: -f1`
if [ -n "${defgroupname}" ]
then
	defgroupname="(${defgroupname})"
fi
echo "Current defaults for adduser:
group ID		${defgroup}	${defgroupname}
parent directory	${defparent}"

if  checkyn ${flags} -H'
	This is the "name" that the computer uses to identify users belonging
	to the same group that share common project data files and directories.
	' \
	-f 'Do you want to change the default group ID?'
then
	while true
	do
		groupid=`checkre ${flags} -H'
	The group ID should be either a group number
	or an existing group name.
	Group names can contain only numbers and lower case letters.' \
			-fe "Enter group ID number or group name [?, q]:" \
			'^[0-9a-z]*$' 'Answer contains an illegal character.
	Only numbers and lower case letters are permitted.'`
		
		ERR=0
		if num "${groupid}"
		then
			x=`cut -d: -f3 /etc/group | grep "${groupid}"`
			if [ -n "${x}" ]
			then
				groupname=`cut -d: -f1,3 /etc/group | 
					grep ":${groupid}$" | cut -d: -f1`
			else
				echo "\\tUnknown groupid number."
				ERR=1
			fi
		else
			x=`grep "^${groupid}:" /etc/group`
			if [ -n "${x}" ]
			then
				groupname=${groupid}
				groupid=`echo "$x" | cut -d: -f3`
			else
				echo "\\t${groupid} is not a known group name."
				ERR=2
			fi
		fi
		if [ $ERR -ne 0 ]
		then
			echo "\\tKnown groups (group ID):" \
			"\\n`cut -d: -f1,3 /etc/group  |  sort  |
				sed 's/:\(.*\)/	(\1)/'  |  pr -o4 -t -3`" \
			'\nq  for quit'
			continue
		else
			if [ "${groupid}" -gt ${maxid} -o \( "${groupid}" -lt ${minid} -a "${groupid}" -ne 1 \) ]
			then
				echo "\\tGroupid number out of range."
				echo "\\tMust be between ${minid} and ${maxid}."
				continue
			fi
		fi
	break
	done
	defgroup=${groupid}
	defgroupname=${groupname}
fi

if checkyn ${flags} -H'
	This is the directory under which new login directories
	will be added.' -f 'Do you want to change the default parent directory?'
then
	while true
	do
		pardir=`checkre ${flags} \
			-fe "Enter parent directory name [q]:" \
			'^/' 'Directory name must begin with a "/".' \
			'^//*[^/]*' 'Directory must not be the "/" directory.' \
			'^[-./a-zA-Z0-9]*$' "Directory name contains an illegal character.
	Only letters, numbers, '.', '-', and '/' are permitted."`
		if [ -f "${pardir}" ]
		then
			echo "\\t${pardir} is already a file name.  Choose another."
			continue
		fi
		if [ ! -d "${pardir}" ]
		then
			echo "\\t${pardir} directory does not exist.  Choose another."
			continue
		fi

		# Grab characters beginning with the first slash (/) up to
		# but not including the second slash -- assume this is a
		# file system.

		fs=`expr "X${pardir}" : 'X\(/[^/]*\)'`
		x=`/etc/mount  |  grep "^${fs} "`
		case "${x}" in
		*'read only'* )
			echo "\\tFile system ${fs} is not writable.  Choose another.
	Possible file systems:
	" `/etc/mount  |  cut -d' ' -f1  |  egrep -v '^/$|^/tmp$'`
			continue
			;;
		'/ '*  |  '/tmp '* )
			echo "\\tCannot put login directories into / or /tmp.  Choose another."
			continue
			;;
		'' )
			echo "\\t${fs} is not a file system.  Choose another.
	Possible file systems:
	" `/etc/mount  |  cut -d' ' -f1  |  egrep -v '^/$|^/tmp$'`
			continue
			;;
		esac
		break
	done
	defparent=${pardir}
fi
if [ -n "${defgroupname}" ]
then
	defgroupname="(${defgroupname})"
fi
echo "\\nThese will be the new defaults:
group ID:		${defgroup}	${defgroupname}
parent directory:	${defparent}"
if checkyn ${flags} -f 'Do you want to keep these values?'
then
	/bin/sh -c "
		trap '' 1 2 3 15
		set -e
		umask 022
		date +'#	Default values for adduser.  Changed %D %T.
defgroup=${defgroup}
defparent=${defparent}' >${MENUTOP}/defadduser
		echo Defaults installed.
	"  ||  {
		admerr $0 'Failure in attempting to create adduser defaults.'
		exit 1
	}
fi
