#!/usr/bin/env cbsd
CIXARG=""
CIXOPTARG="controlmaster"
MYDESC="Ncurses based dialog for adduser"
CBSDMODULE="bsdconf"
ADDHELP="
${H3_COLOR}Description${N0_COLOR}:

Ncurses based dialog for creating new user accounts.
Prompts for login, full name (gecos), member groups and password,
then generates a configuration snippet consumed by 'cbsd adduser'.

${H3_COLOR}Options${N0_COLOR}:

 ${N2_COLOR}controlmaster=${N0_COLOR} - path to file where generated config is written.

${H3_COLOR}Examples${N0_COLOR}:

 # cbsd adduser-tui
 # cbsd adduser-tui controlmaster=/tmp/user.conf

${H3_COLOR}See also${N0_COLOR}:

 cbsd adduser --help

"

. ${subrdir}/nc.subr

cixinit
cixinit

. ${subrdir}/settings-tui.subr
. ${dialog}

f_include $BSDCFG_SHARE/strings.subr
f_include $BSDCFG_SHARE/usermgmt/group_input.subr
f_include $BSDCFG_SHARE/usermgmt/user_input.subr

: ${login:=""}
: ${defaultclass:=""}
: ${defaultshell:="/bin/csh"}
: ${homeprefix:="/home"}
: ${passwdtype:="yes"}
: ${udotdir:="/usr/share/skel"}
: ${uexpire:=""}
: ${gecos:=""}
: ${upwexpire:=""}
: ${member_groups:="wheel"}
case "$uexpire" in *[mhdwoy])
	f_isinteger "${uexpire%[mhdwoy]}" && uexpire="+$uexpire"
esac
case "$upwexpire" in *[mhdwoy])
	f_isinteger "${upwexpire%[mhdwoy]}" && upwexpire="+$upwexpire"
esac

get_login()
{
	local _ok _message _input

	_message="Use alpha-numeric, punctuation, TAB or ENTER"
	f_dialog_title " login "

	_ok=0

	while [ ${_ok} -ne 1 ]; do
		f_dialog_input _input "Login:" "${login}" \
			"${_message}" || return $?

		validate_jname "${login}"
		case $? in
		0)
			_ok=1
			;;
		*)
			_message="ERROR: Bad name. Try again."
			;;
		esac
	done

	[ -n "${_input}" ] && login="${_input}"
	return 0
}

get_gecos()
{
	local _message _input

	_message="Use alpha-numeric, punctuation, TAB or ENTER"
	f_dialog_title " gecos "

	f_dialog_input _input "Full Name:" "${gecos}" \
			"${_message}" || return $?

	[ -n "${_input}" ] && gecos="${_input}"
	return 0
}

get_member_groups()
{
	local _message _input

	_message="Use numbers, punctuation, TAB or ENTER"
	f_dialog_title " member groups "

	f_dialog_input _input "Groups (Separated by commas):" "${member_groups}" \
		"${_message}" || return $?

	[ -n "${_input}" ] && member_groups="${_input}"
	return 0
}

gen_cfg()
{
	local _out="/dev/stdout"

	if [ -n "${controlmaster}" ]; then
		_out="${controlmaster}"
		${TRUNCATE_CMD} -s0 ${controlmaster}
	fi

	${CAT_CMD} >> ${_out} <<EOF
user_add='${login}'
user_gecos_${login}='${gecos}'
user_home_${login}='${home}'
user_shell_${login}='${shell}'
user_member_groups_${login}='${member_groups}'
EOF

	if [ -n "${user_password_crypt}" ]; then
		${CAT_CMD} >> ${_out} <<EOF
user_pw_${login}_crypt='${user_password_crypt}'
EOF
	elif [ -n "${user_password}" ]; then
		${CAT_CMD} >> ${_out} <<EOF
user_pw_${login}='${user_password}'
EOF
	fi
}

f_user_add()
{
	local funcname=f_user_add
	local title
	local alert=f_show_msg no_confirm=

	get_login
	[ -z "${login}" ] && return 1
	get_gecos
	[ -z "${gecos}" ] && return 1
	get_member_groups

	local user_password= user_password_crypt=

	home="${homeprefix}/${login}"
	shell="${defaultshell}"

	if get_password; then
		user_password="${mtag}"
		capture user_password_crypt ${miscdir}/pwcrypt ${user_password}
		if [ $? -eq 0 ]; then
			unset user_password
		else
			unset user_password_crypt
		fi
	else
		user_password=
	fi

	getyesno "Add new user ${login}?"
	[ $? -eq 0 -o $? -eq 3 ] && gen_cfg

	return $SUCCESS
}

[ -z "${out}" ] && out="${tmpdir}/adduser-tui.$$"
capture CONTROLMASTER ${MKTEMP_CMD}
${TOUCH_CMD} ${CONTROLMASTER}

f_user_add

f_die
