#!/usr/bin/env cbsd
#v12.0.4
CBSDMODULE="bsdconf"
CIXARG="jname login"
CIXOPTARG="epw pw"
MYDESC="cbsd passwd wrapper"
ADDHELP="
${H3_COLOR}Description${N0_COLOR}:

 Change user password in jail or chroot environment.

${H3_COLOR}Options${N0_COLOR}:

 ${N2_COLOR}jname=${N0_COLOR}  - target jail.
 ${N2_COLOR}login=${N0_COLOR}  - login name.
 ${N2_COLOR}pw=${N0_COLOR}     - plaintext password (for non-interactive mode).
 ${N2_COLOR}epw=${N0_COLOR}    - encrypted password hash (for non-interactive mode).

${H3_COLOR}Examples${N0_COLOR}:

 # cbsd passwd jname=jail1 login=root
 # cbsd passwd jname=jail1 login=root pw='secret'

${H3_COLOR}See also${N0_COLOR}:

 cbsd adduser --help

"

. ${subrdir}/nc.subr
jname=
cixinit

. ${system}

_MYDIR="${CIX_DISTDIR}/modules/bsdconf.d"

set -e
	. ${_MYDIR}/bsdconf.subr
set +e

passwd_multi_init()
{
	local _jname

	while getopts "c:n:o:" opt; do
		case "${opt}" in
			c) cmd="${OPTARG}" ;;
			n) _multiple_consumer_name="${OPTARG}" ;;
			o) task_owner="${OPTARG}" ;;
		esac
		shift $(($OPTIND - 1))
	done

	[ -z "${task_owner}" ] && err 1 "${N1_COLOR}multiple_processing_spawn: empty -o multiple_task_owner${N0_COLOR}"

	. ${subrdir}/multiple.subr
	${ECHO} "${N1_COLOR}Hint: Press ${N2_COLOR}'Ctrl+t'${N1_COLOR} to see last logfile line for active task${N0_COLOR}" 1>&2
	task_id=
	task_id_cur=
	task_owner="${task_owner}"
	for _jname in ${jail_list}; do
		task_id_cur=$( task mode=new logfile=${tmpdir}/${task_owner}.${_jname}.log.$$ client_id=${_jname} autoflush=0 owner=${task_owner} /usr/bin/env NOCOLOR=1 /usr/local/bin/cbsd passwd jname=${_jname} ${cmd} 2>/dev/null )
		sleep 0.1
		if is_number "${task_id_cur}"; then
			task_id="${task_id} ${task_id_cur}"
		fi
	done

	multiple_task_id_all=$( echo ${task_id} | ${TR_CMD} " " "," )
	sleep 0.5
	multiple_processing_spawn -o ${task_owner} -n "${_multiple_consumer_name}"
}

# MAIN
emulator="jail"
jail_list=
jname_is_multiple

if [ -n "${jail_list}" ]; then
	JLIST="${jail_list}"
	task_owner="passwd_multiple_query"
	_args=

	for i in $*; do
		[ "${i%%=*}" = "jname" ] && continue
		if [ -n "${_args}" ]; then
			_args="${_args} ${i}"
		else
			_args="${i}"
		fi
	done

	[ -z "${pw}" -a -z "${epw}" ] && err 1 "${N2_COLOR}pw= ${N1_COLOR}or ${N2_COLOR}epw=${N1_COLOR} is mandatory for multiple operation${N0_COLOR}"
	task_owner="passwd_multiple_add"
	passwd_multi_init -c "${_args}" -o ${task_owner} -n "passwd"

	err 0 "${N1_COLOR}Multiple passwd: ${N2_COLOR}done${N0_COLOR}"
fi

PW_PRE=

if [ -n "${jname}" ]; then
	. ${subrdir}/rcconf.subr
	[ $? -eq 1 ] && err 1 "${N1_COLOR}No such jail: ${N2_COLOR}${jname}${N0_COLOR}"
	[ ${baserw} -eq 1 ] && path=${data}

	. ${subrdir}/universe.subr
	readconf buildworld.conf
	init_target_arch
	init_srcdir
	init_basedir
	init_kerneldir

	[ ${jid} -eq 0 ] && prepare_jail

	if [ "${emulator}" != "jail" -a -n "${emulator}" -a "${emulator}" != "bhyve" ]; then
		. ${subrdir}/emulator.subr
		init_usermode_emul
		CHROOT_EXEC="/usr/sbin/chroot ${path} /bin/${emulator}"
	else
		CHROOT_EXEC="/usr/sbin/chroot ${path}"
	fi

	PW_PRE="${CHROOT_EXEC}"
else
	path=
fi

if [ -z "${pw}" -a -z "${epw}" ]; then
	${PW_PRE} /usr/bin/passwd ${login}
else
	if [ -n "${pw}" ]; then
		echo "${pw}" | ${PW_PRE} ${PW_CMD} usermod "${login}" -h 0
	elif [ -n "${epw}" ]; then
		${PW_PRE} /usr/bin/chpass -p "${epw}" "${login}"
	fi
fi

exit 0
