#!/bin/sh

MM_CONF="/etc/mailman/mm_config.py"

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

#turn off auto expansion
set -f

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

shell_add_or_subst()
{
	local name="$1" && shift
	local value="$1" && shift
	local file="$1" && shift

	[ "$value" = "fqdn" -o "$value" = "None" ] || value="'$value'"
	[ -f "$file" ] || touch "$file"
	if grep -qs "^$name" "$file"; then
		sed -r -i "s,^$name[[:space:]=].*,$name = $value," -- "$file"
		return 0
	fi
	printf %s\\n "$name = $value" >> "$file"
}

shell_get_and_print()
{
	local name="$1" && shift
	local keyword="$1" && shift
	local file="$1" && shift

	sed -rn "s,^$name\s*=\s*'?([^']*)'?$,$keyword \"\1\",p" -- "$file"
}

shell_get()
{
	local name="$1" && shift
	local file="$1" && shift

	sed -rn "s,^$name\s*=\s*'?([^']*)'?$,\"\1\",p" -- "$file"
}

on_message()
{
	case "$in_action" in
		constraints)
			echo '('
			printf 'autodetect (exclude (#t urlhost) exclude (#t emailhost) label "%s" default #f)' "$(_ "Autodetect")"
			printf ' passwd1  (equal passwd2 label "%s")' "$(_ "Administrator password")"
			printf ' passwd3  (equal passwd4 label "%s")' "$(_ "List creator password")"
			printf ' lang (default "en" label "%s")' "$(_ "Site language")"
			printf ' mta (default "Postfix" label "%s")' "$(_ "Site MTA")"
			printf ' urlhost (hostname #t label "%s")' "$(_ "URL host")"
			printf ' emailhost (hostname #t label "%s")' "$(_ "Email host")"
			printf ' chkconfig (label "%s")' "$(_ "start, stop or restart service")"
			echo ')'
			;;
		list)
			echo '('
			case "$in__objects" in
			    virtualhost)
			    sed -rn "s/^add_virtualhost\s*\(\s*'?([^),']+)'?\s*,\s*'?([^),']+)'?\).*/(\"\1\" urlhost \"\1\" emailhost \"\2\")/p" "$MM_CONF"
			    sed -rn "s/^add_virtualhost\s*\(\s*'?([^),']+)'?\).*/(\"\1\" urlhost \"\1\")/p" "$MM_CONF"
			    ;;
			    mta)
				rpm -q postfix &> /dev/null && echo '("Postfix")' ||:
				rpm -q exim &> /dev/null && echo '("Exim")' ||:
				rpm -q qmail &> /dev/null && echo '("Qmail")' ||:
				rpm -q sendmail &> /dev/null && echo '("Sendmail")' ||:
				rpm -q courier &> /dev/null && echo '("Courier")' ||:
				;;
			    lang)
				awk -F \' '/^add_language/ { print $2 " " $4 }' /usr/share/mailman/Mailman/Defaults.py | \
				while read lang desc; do
				    printf '( "%s" label "%s" )' $lang "$(_ "$desc")"
				done
				;;
			esac
			echo ')'
			;;
		read)
			local urlhost
			local emailhost
			echo '('
			urlhost=$(shell_get "DEFAULT_URL_HOST" "$MM_CONF")
			emailhost=$(shell_get "DEFAULT_EMAIL_HOST" "$MM_CONF")
			if [ "$urlhost" = '"fqdn"' -a "$emailhost" = '"fqdn"' ]; then
				echo 'autodetect #t urlhost "" emailhost ""'
			else
				echo "autodetect #f urlhost $urlhost emailhost $emailhost"
			fi
			shell_get_and_print "MTA" "mta" "$MM_CONF"
			shell_get_and_print "MAILMAN_SITE_LIST" "sitelist" "$MM_CONF"
			shell_get_and_print "DEFAULT_SERVER_LANGUAGE" "lang" "$MM_CONF"
			echo ')'
			;;
		write)
			if [ "$in_autodetect" = "#t" ]; then
				in_urlhost="fqdn"
				in_emailhost="fqdn"
			fi
			[ -n "$in_urlhost" ] && shell_add_or_subst "DEFAULT_URL_HOST" "$in_urlhost" "$MM_CONF"
			[ -n "$in_emailhost" ] && shell_add_or_subst "DEFAULT_EMAIL_HOST" "$in_emailhost" "$MM_CONF"
			[ -n "$in_mta" ] && shell_add_or_subst "MTA" "$in_mta" "$MM_CONF"
			[ -n "$in_sitelist" ] && shell_add_or_subst "MAILMAN_SITE_LIST" "$in_sitelist" "$MM_CONF"
			[ -n "$in_lang" ] && shell_add_or_subst "DEFAULT_SERVER_LANGUAGE" "$in_lang" "$MM_CONF"
			[ -n "$in_passwd1" ] && /usr/share/mailman/bin/mmsitepass "$in_passwd1" &> /dev/null
			[ -n "$in_passwd3" ] && /usr/share/mailman/bin/mmsitepass -c "$in_passwd3" &> /dev/null
			echo '()'
			;;
		new)
			case "$in_type" in
			    virtualhost)
			    if egrep -qs "^add_virtualhost\s*\(\s*'?$in_urlhost'?[[:space:],)]" "$MM_CONF"; then
				printf '(error "%s")' "$(_ "same virtual host is already defined")"
			    else
				if [ -n "$in_emailhost" ]; then
				    echo "add_virtualhost('$in_urlhost', '$in_emailhost')" >> "$MM_CONF"
				else
				    echo "add_virtualhost('$in_urlhost')" >> "$MM_CONF"
				fi
			    fi
			    ;;
			esac
			echo '()'
			;;
		delete)
			if [  "$in__objects" != "/" ]; then
			    if [ "${in__objects%/*}" = "virtualhost" ];then
				sed -r "/^add_virtualhost\s*\(\s*'?${in__objects#*/}'?/ d" -i "$MM_CONF"
			    fi
			fi
			echo '()'
			;;
		*)
			echo '#f'
			;;
	esac
}

message_loop
