# shell function library for FreeBSD/TrueOS
# vim: set filetype=sh:
old_escape() { system-control escape --format old-alt -- "$@" ; }
pwescape() { system-control escape --format account -- "$@" ; }
case "`uname`" in
FreeBSD)	groupmod_rename() { groupmod "$1" -l "$2"; };;
*BSD)		pw() { local command="$1" account="$2" ; shift 2 ; "${command}" "$@" "${account}" ; } ; groupmod_rename() { groupmod -n "$2" "$1"; } ;;
esac
try_restart() {
	for i
	do
		# In theory this can operate foreign daemontools-style service managers.
		if system-control is-loaded "$i" && 
		   system-control is-active "$i"
		then
			printf 1>&2 "Try-Restart: %s\n" "$i"
			system-control try-restart "$i"
		fi
	done
}
add_user() { 
	if ! getent passwd "$1" > /dev/null 
	then
                if test -n "$2"
                then
                        printf 1>&2 "Adding user: %s with home directory: %s\n" "$1" "$2"
                else
                        printf 1>&2 "Adding user: %s\n" "$1"
                fi
		pw useradd "$1" -s /sbin/nologin ${2+ -d "$2"}
	else
		# printf 1>&2 "Using existing user: %s\n" "$1"
		:
	fi
}
add_group() { 
	if ! getent group "$1" > /dev/null 
	then
		printf 1>&2 "Adding group: %s\n" "$1"
		pw groupadd "$1" 
	fi
}
add_log() {
	install -d -m 0755 /var/log/sv/"$2"
	# setfacl -m g:adm:rx /var/log/sv/"$2" 2>/dev/null || setfacl -m group:adm:rx::allow /var/log/sv/"$2" || :
	setfacl -m u:"$1":rwx /var/log/sv/"$2" 2>/dev/null || setfacl -m user:"$1":rwxpD::allow /var/log/sv/"$2" || { chgrp "$1" /var/log/sv/"$2" && chmod g+w /var/log/sv/"$2" ; } || :
}
rename_user() {
	if getent passwd "$1$3" > /dev/null 
	then
		printf 1>&2 "Renaming user: %s to %s\n" "$1$3" "$2$3"
		pw usermod "$1$3" -l "$2$3"
	fi
}
rename_group() {
	if getent group "$1$3" > /dev/null 
	then
		printf 1>&2 "Renaming group: %s to %s\n" "$1$3" "$2$3"
		groupmod_rename "$1$3" "$2$3"
	fi
}
rename_logfile_owning_user() {
	local opwescaped="`old_escape "$1"`"
	if ! getent passwd "${opwescaped}"-log > /dev/null 
	then
		return
	fi
	local npwescaped="`pwescape "$1"`"
	if test _"${opwescaped}" = _"${npwescaped}"
	then
		return
	fi
	try_restart "cyclog@$1.service"
	sleep 0.1	# To give the log service a chance to stop and try restarting
	rename_user "${opwescaped}" "${npwescaped}" -log
}
fixup_user_account_name() {
	local opwescaped="`old_escape "$1"`"
	if ! getent passwd "${opwescaped}" > /dev/null 
	then
		return
	fi
	local npwescaped="`pwescape "$1"`"
	if test _"${opwescaped}" = _"${npwescaped}"
	then
		return
	fi
	rename_user "${opwescaped}" "${npwescaped}" ""
}
service_with_dedicated_logger() {
	if test -z "${nosh_run_package}"
	else
		rename_logfile_owning_user "$1"
		local pwescaped="`pwescape "$1"`-log"
		add_group "$pwescaped"
		add_user "$pwescaped"
		add_log "$pwescaped" "$1"
	then
		try_restart "$1.service" "cyclog@$1.service"
	fi
}
prefixed_service_with_dedicated_logger() {
	if test -z "${nosh_run_package}"
	then
		rename_logfile_owning_user "$1$2"
		local pwescaped="`pwescape "$1$2"`-log"
		add_group "$pwescaped"
		add_user "$pwescaped"
		add_log "$pwescaped" "$1$2"
	else
		system-control preset --prefix "$1" "$2.service"
		system-control preset --prefix "cyclog@$1" "$2.service"
		start_up "cyclog@$1$2.service" "$1$2.service"
	fi
}
tty_service_with_dedicated_logger() {
	if test -z "${nosh_run_package}"
	else
		rename_logfile_owning_user "$1$2"
		local pwescaped="`pwescape "$1$2"`-log"
		add_group "$pwescaped"
		add_user "$pwescaped"
		add_log "$pwescaped" "$1$2"
	then
		try_restart "$1$2.service" "cyclog@$1$2.service"
	fi
}
login_service_with_dedicated_logger() {
        tty_service_with_dedicated_logger "ttylogin@" "$1"
}
kvt_login_service_with_dedicated_logger() {
	case "`uname`" in
	OpenBSD)	ttybase=ttyC; break ;;
	NetBSD)		ttybase=ttyE; break ;;
	*)		ttybase=ttyv; break ;;
	esac
	tty_service_with_dedicated_logger "ttylogin@" "${ttybase}$1"
}
kvt_realizer_service_with_dedicated_logger() {
	case "`uname`" in
	OpenBSD)	ttybase=ttyC; break ;;
	NetBSD)		ttybase=ttyE; break ;;
	*)		ttybase=ttyv; break ;;
	esac
	prefixed_service_with_dedicated_logger "console-kvt-realizer@" "${ttybase}$1"
}
serial_tty_login_service_with_dedicated_logger() {
	case "`uname`" in
	OpenBSD)	ttybase=tty0; break ;;
	NetBSD)		ttybase=tty0; break ;;
	*)		ttybase=ttyu; break ;;
	esac
	tty_service_with_dedicated_logger "getty@" "${ttybase}$1"
}
serial_tty_callout_service_with_dedicated_logger() {
	case "`uname`" in
	OpenBSD)	ttybase=tty0; break ;;
	NetBSD)		ttybase=dty0; break ;;
	*)		ttybase=cuau; break ;;
	esac
	prefixed_service_with_dedicated_logger "ttycallout@" "${ttybase}$1"
}
socket_with_dedicated_logger() {
	if test -z "${nosh_run_package}"
	else
		rename_logfile_owning_user "$1"
		local pwescaped="`pwescape "$1"`-log"
		add_group "$pwescaped"
		add_user "$pwescaped"
		add_log "$pwescaped" "$1"
	then
		try_restart "$1.socket" "cyclog@$1.socket"
	fi
}
timer_with_dedicated_logger() {
	if test -z "${nosh_run_package}"
	else
		rename_logfile_owning_user "$1"
		local pwescaped="`pwescape "$1"`-log"
		add_group "$pwescaped"
		add_user "$pwescaped"
		add_log "$pwescaped" "$1"
	then
		try_restart "$1.timer" "cyclog@$1.timer"
	fi
}
service_only() {
	if test -z "${nosh_run_package}"
	else
		:
	then
		try_restart "$1.service"
	fi
}
service_only_no_run() {
        :
}
fan_in_logger() {
	if test -z "${nosh_run_package}"
	then
		rename_logfile_owning_user "$1"
		local pwescaped="`pwescape "$1"-log`"
		add_group "$pwescaped"
		add_user "$pwescaped"
		add_log "$pwescaped" "$1"
	else
		try_restart "$1-log.service"
	fi
}
target() {
	:
}
logfile_owning_user() {
	if test -z "${nosh_run_package}"
	then
		rename_logfile_owning_user "$1"
		local pwescaped="`pwescape "$1"`-log"
		add_group "$pwescaped"
		add_user "$pwescaped"
	fi
}
file_owning_user() {
	if test -z "${nosh_run_package}"
	then
		fixup_user_account_name "$1"
		local pwescaped="`pwescape "$1"`"
		add_group "$pwescaped"
		add_user "$pwescaped" "$2"
	fi
}
non_file_owning_user() {
	if test -z "${nosh_run_package}"
	then
		fixup_user_account_name "$1"
		local pwescaped="`pwescape "$1"`"
		add_group "$pwescaped"
		add_user "$pwescaped"
	fi
}
user_vt_user() {
	if test -z "${nosh_run_package}"
	then
		local pwescaped="`pwescape user-vt-"$1"`"
		add_group "$pwescaped"
		add_user "$pwescaped"
	fi
}
user_vt_group() {
	if test -z "${nosh_run_package}"
	then
		local pwescaped="`pwescape user-vt-"$1"`"
		add_group "$pwescaped"
	fi
}
user_tty() {
	:
}
directory() {
	if test -z "${nosh_run_package}"
	then
		install -d -o "$1" -m "$3" -- "$2"
	fi
}
