#! /bin/bash

##- Delete users from a external user password file
##: wiki user [users...]
##+ wiki    Name of the wiki to delete users from
##+ user    Login names of the users to delete

[ "${__twa_bindir}" ] || exec "$(dirname "$0")/twiki-admin" userdel "$@"
[[ $# -ge 1 ]] || __twa_usage_and_exit

[[ -r /dev/urandom ]] || __twa_die "/dev/urandom is not readable"

__twa_run_locked
__twa_config "$1"
shift

if ! [ "${cfg_vhost_passwd}" ] ; then
	__twa_die "Apache password file handling is disabled in configuration file"
fi

__twa_do touch "${cfg_vhost_passwd}"
__twa_do __twa_backup_file "${cfg_vhost_passwd}"


for user in "$@" ; do
	__twa_do htpasswd -D "${cfg_vhost_passwd}" "${user}"
done

__twa_do chown "${cfg_dir_owner}" "${cfg_vhost_passwd}"

if [ "${cfg_userdel_hook}" ] ; then
	eval cmd='${cfg_userdel_hook}'
	# The lock needs to be released, just in case the hook uses twiki-admin
	__twa_unlock
	/bin/bash <<< "${cmd}"
fi
