#!/bin/sh

_()
{
LANG=${in_language%%;*}.utf8 gettext "alterator-users" "$1"
}


#turn off auto expansion
set -f

. /usr/share/alterator/build/backend3.sh


UID_MIN=$(grep '^UID_MIN' /etc/login.defs 2>/dev/null|sed -r 's,UID_MIN[[:space:]]+,,')
[ -z "$UID_MIN" ] && UID_MIN=500

list_users()
{
    local IFS=:
    
    getent passwd|
    while read name password uid gid gecos home shell; do
        [ "$uid" -ge "$UID_MIN" ] || continue
	[ "$shell" == "/sbin/nologin" ] || grep -qs "^$shell$" /etc/shells || continue
        printf '("%s" gecos "%s" home "%s" shell "%s")' "$name" "$gecos" "$home" "$shell"
    done
}


read_user()
{
    local IFS=:
    
    getent passwd "$1"|
	(read name password uid gid gecos home shell;
	 printf ' gecos "%s" home "%s" shell "%s" ' "$gecos" "$home" "$shell")
}

error_message()
{
	echo "(error \"$1\")"
}


is_defined()
{
	set |grep -qs "^$1="
}

user_args()
{
	local args=
	is_defined "in_gecos" && args="$args -c \"$(quote_shell_arg "$in_gecos")\""
	[ -n "$in_home" ] && args="$args -d \"$(quote_shell_arg "$in_home")\""
	[ -n "$in_shell" ] && args="$args -s \"$in_shell\""
	echo "$args"
}


user_error_message()
{
	case "$1" in
		1) error_message "`_ "can't update password file"`" ;;  #'
	        2) error_message "`_ "invalid command syntax"`" ;; 
       		3) error_message "`_ "invalid argument to option"`" ;; 
		4) error_message "`_ "uid already in use"`" ;; 
	        6) error_message "`_ "specified user doesn't exist"`" ;; #'
		8) error_message "`_ "user currently logged in"`" ;;
		9) error_message "`_ "username already in use"`" ;;
		10) error_message "`_  "can't update group file"`" ;; #'
		12) error_message "`_ "can't create or remove home directory"`" ;;#'
		13) error_message "`_ "can't create mail spool"`" ;; #'
		*) error_message "retcode=$1" ;;
	esac
}


user_chpasswd()
{
	echo "$in__objects:$in_passwd1"|/usr/sbin/chpasswd
	return $?
}

# quote argument for shell.
quote_shell_arg()
{
	local out="$*"
	if [ -z "${out##*[\"\$\`\\]*}" ]; then
		out="$(printf %s "$out" |sed -e 's/["$`\]/\\&/g')" ||
			return 1
	fi
	printf %s "$out"
}

on_message()
{
	case "$in_action" in
		#object manipulations
		list)
		    echo '('
			if [ "$in__objects" = "/" ];then
			    list_users
			else
			    sed 's,.*,("&"),' /etc/shells
			    printf '("/sbin/nologin")'
			fi
		    echo ')'
		    ;;
		read)
			echo '('
			[ "${in__objects}" == "/" ] || read_user "$in__objects"
			echo ')'
			;;
		write)
			local args=$(user_args)
			local retcode=0
			if [ -n "$args" ]; then
				eval /usr/sbin/usermod $args "$in__objects"
				retcode=$?
			fi

			if [ "$retcode" -eq 0 -a -n "$in_passwd1" ];then 
				user_chpasswd
				retcode=$?
			fi
			
			if [ "$retcode" -ne 0 ]; then
				user_error_message "$retcode"
			else
				echo '()'
			fi
			;;
		new)
			local args=$(user_args)
			local retcode=0
			eval /usr/sbin/useradd $args "$in__objects"
			retcode=$?

			if [ "$retcode" -eq 0 -a -n "$in_passwd1" ];then 
				user_chpasswd
				retcode=$?
			fi

			if [ "$retcode" -ne 0 ]; then
				user_error_message "$retcode"
			else
				echo '()'
			fi
			;;
		delete)
			local retcode=0
			/usr/sbin/userdel "$in__objects"
			retcode=$?
			if [ "$retcode" -ne 0 ]; then
				user_error_message "$retcode"
			else
				echo '()'
			fi
			;;
		*)
			echo '#f'
			;;
	esac
}


message_loop
