#!/usr/local/bin/cbsd
#v12.0.4
CBSDMODULE="bsdconf"
MYARG="jname login"
MYOPTARG="epw pw"
MYDESC="cbsd passwd wrapper"
ADDHELP="jname= work in with jail\n\
epw='encrypted hash', set passwd hash, for non-interactive mode\n\
pw='passw' password, for non-interactive mode\n\
login='login' loginname, for non-interactive mode\n"
EXTHELP="modules/bsdconf.d"

. ${subrdir}/nc.subr
. ${cbsdinit}

. ${system}

#defines
_MYDIR=$( /usr/bin/dirname `${REALPATH_CMD} $0` )
SERVICE="passwd"
noname=0

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

# -n "name of the tools" - show <name> in Info string, e.g: -n jexec, -n "pkg install" ...
# -o uniq_name_of_the_task (one world)
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}"
	# spawn command for all jail
	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               # dont bruce taskdb
		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
origjname=

emulator="jail" # for jname_is_multiple
jail_list=
jname_is_multiple

# MAIN for multiple jails
if [ -n "${jail_list}" ]; then
	# multiple
	if [ -n "${jail_list}" ]; then
		JLIST="${jail_list}"
	fi

	task_owner="passwd_multiple_query"
	_args=

	# trim for jname= in "$*"
	for i in $*; do
		prefix=
		prefix=$( substr --pos=0 --len=6 --str="${i}" )
		[ "${prefix}" = "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


TRAP=""
# for chroot/jail env
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}"
	shift
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
