:
#	@(#) mkchpasswd 23.2 91/08/29 
#
#	Copyright (C) 1991 The Santa Cruz Operation, Inc.
#		All Rights Reserved.
#	The information in this file is provided for the exclusive use of
#	the licensees of The Santa Cruz Operation, Inc.  Such users have the
#	right to use, modify, and incorporate this code into other products
#	for purposes authorized by the license agreement provided they include
#	this notice and the associated copyright notice with any such product.
#	The information in this file is provided "AS IS" without warranty.
#
# chpasswd	--  Helper script for unretire to change the password
#		    field in the password or shadow password file.
#
# Usage: chpasswd file username string
#
# where file is /etc/passwd or /etc/shadow, and string is the string to
# be substituted for the encrypted password.
#
# NOTE this script is not intended to be called directly but via unretire
# so that the necessary lock file is created beforehand.
#

file="$1"
uname="$2"
str="$3"

# check the lock file has been created by ale
[ -f "$file-t" ] || { /bin/echo "$0:$file-t not created"; exit 1; }

# change the users password string
if /bin/grep "^$uname:" "$file" >/dev/null
then
	if [ "$str" = "*UNRETIRED*" ]
	then
		/bin/grep "^$uname:\*RETIRED\*" "$file" >/dev/null || {
		/bin/echo "$0: $uname is not retired"; exit 2; }
	else
		if /bin/grep "^$uname:\*RETIRED\*" "$file" >/dev/null
		then
			/bin/echo "$0: $uname is already retired"
			exit 2
		fi
	fi
	/bin/sed "s/^$uname:[^,:]*/$uname:$str/" <"$file" >"$file-t"
else
	/bin/echo "$0: $uname is not listed in $file"
	exit 2
fi

exit 0
