#!/bin/sh

PASSWD_LDAP="files ldap"
SHADOW_LDAP="tcb files ldap"
GROUP_LDAP="files ldap"

PASSWD_LOCAL="files"
SHADOW_LOCAL="tcb files"
GROUP_LOCAL="files"

LDAP_CONFIG="/etc/nss_ldap.conf"

NSS_CONFIG="/etc/nsswitch.conf"


read_profile()
{
    if grep -qs "^passwd:[[:space:]]$PASSWD_LDAP" "$NSS_CONFIG";then
	echo "ldap"
    else
	echo "local"
    fi
}

_()
{
LANG=${in_language%%;*}.utf8 gettext "alterator-nsswitch" "$1"
}

#turn off auto expansion
set -f

. /usr/share/alterator/build/backend3.sh

on_message()
{
	case "$in_action" in
		#information for renderer
		template)
			local profile="$(read_profile)"
			echo '('
			echo ' template "form"'
			echo ' help "nsswitch.html"'
			
			if [ "$profile" = "ldap" ];then
			    echo 'url "nsswitch-ldap.html"'
			else
			    echo 'url "nsswitch-local.html"'
			fi

			echo ')'
			;;
		info)
			echo '('
			printf ' title "%s"' "`_ "Database source"`"
			printf ' description "%s"' "`_ "Users database source setup"`"
			printf ' group "%s"' "`_ "Users"`"
			echo ')'
			;;
		*)
			echo '#f'
	esac
}

message_loop
