# shell function library for FreeBSD/TrueOS
# vim: set filetype=sh:
pwescape() { system-control escape --format account -- "$@" ; }
case "`uname`" in
FreeBSD)	;;
*BSD)		pw() { local command="$1" account="$2" ; shift 2 ; "${command}" "$@" "${account}" ; } ;;
esac
del_user() { 
	if getent passwd "$1" > /dev/null 
	then
		printf 1>&2 "Removing user: %s\n" "$1"
		pw userdel "$1" 
	fi
}
del_group() { 
	if getent group "$1" > /dev/null 
	then
		printf 1>&2 "Removing group: %s\n" "$1"
		pw groupdel "$1" 
	fi
}
shut_down() {
	for i
	do
		system-control disable "$i"
		# In theory this can operate foreign daemontools-style service managers.
		if system-control is-loaded "$i"
		then
			system-control unload-when-stopped "$i"
			if system-control is-active "$i"
			then
				printf 1>&2 "Stop: %s\n" "$i"
				system-control stop "$i"
			fi
		fi
	done
}
service_with_dedicated_logger() {
	if test -z "${nosh_run_package}"
	then
		:
	else
		shut_down "$1.service" "cyclog@$1.service"
	fi
}
prefixed_service_with_dedicated_logger() {
	if test -z "${nosh_run_package}"
	then
		:
	else
		shut_down "$1$2.service" "cyclog@$1$2.service"
	fi
}
tty_service_with_dedicated_logger() {
	if test -z "${nosh_run_package}"
	then
		:
	else
		shut_down "$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}"
	then
		:
	else
		shut_down "$1.socket" "cyclog@$1.service"
	fi
}
timer_with_dedicated_logger() {
	if test -z "${nosh_run_package}"
	then
		:
	else
		shut_down "$1.timer" "cyclog@$1.service"
	fi
}
service_only() {
	if test -z "${nosh_run_package}"
	then
		:
	else
		shut_down "$1.service"
	fi
}
service_only_no_run() {
	if test -z "${nosh_run_package}"
	then
		:
	else
		shut_down "$1.service"
	fi
}
fan_in_logger() {
	if test -z "${nosh_run_package}"
	then
		:
	else
		shut_down "$1-log.service"
	fi
}
target() {
	if test -z "${nosh_run_package}"
	then
		:
	else
		shut_down "$1.target"
	fi
}
# Don't delete the log service users because the log files still exist.
logfile_owning_user() {
	:
}
# Don't delete the file owning users because the files still exist.
file_owning_user() {
	:
}
non_file_owning_user() {
	if test -z "${nosh_run_package}"
	then
                local pwescaped="`pwescape "$1"`"
                del_user "$pwescaped"
        else
                :
        fi
}
user_vt_user() {
	:
}
user_vt_group() {
        :
}
user_tty() {
	if test -z "${nosh_run_package}"
	then
                awk -F'\t' '{ if ($1 == "'"$1"'" && $4 != "on") print "WARNING: " $1 "( " $3 ") is still on in " FILENAME " ."; }' /etc/ttys
	fi
}
directory() {
        :
}
