#!/bin/sh

DOVECOT_CONFIG="/etc/dovecot/dovecot.conf"

current_list()
{
    sed -nr "/^[[:space:]]*$1[[:space:]]*=/ {s,^[[:space:]]*$1[[:space:]]*=[[:space:]],,;p}" "$DOVECOT_CONFIG"
}

read_list()
{
    local obj="$(current_list "$1")"
    [ -n "$obj" ] || obj="$2"
    for i in $obj;do
	printf "$1_%s #t "  "$i"
    done
}

write_list()
{
    sed -r "s,^([[:space:]]*)(#)?([[:space:]]*)?$1[[:space:]]*=.*,\1\3$1 =$2," -i "$DOVECOT_CONFIG"
}
    

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

set -f

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

on_message()
{
	case "$in_action" in
		constraints)
			echo '('
			printf 'protocols_pop3 (label "%s")' "`_ "POP3"`"
			printf 'protocols_pop3s (label "%s")' "`_ "POP3 over TLS/SSL"`"
			printf 'protocols_imap (label "%s")' "`_ "IMAP4"`"
			printf 'protocols_imaps (label "%s")' "`_ "IMAP4 over TLS/SSL"`"

			printf 'mechanisms_plain (label "%s")' "`_ "PLAIN"`"
			printf 'mechanisms_login (label "%s")' "`_ "LOGIN"`"
			printf 'mechanisms_cram_md5 (label "%s")' "`_ "CRAM-MD5"`"
			printf 'mechanisms_digest_md5 (label "%s")' "`_ "DIGEST-MD5"`"
			printf 'mechanisms_apop (label "%s")' "`_ "APOP"`"
			printf 'mechanisms_ntlm (label "%s")' "`_ "NTLM"`"
			printf 'mechanisms_gssapi (label "%s")' "`_ "GSSAPI"`"
			printf 'mechanisms_rpa (label "%s")' "`_ "RPA"`"
			printf 'mechanisms_anonymous (label "%s")' "`_ "ANONYMOUS"`"
			printf 'mechanisms_otp (label "%s")' "`_ "OTP"`"
			printf 'mechanisms_skey (label "%s")' "`_ "SKEY"`"

			printf 'auth_port (label "%s")' "`_ "Auth port for Postfix"`"

			echo ')'
			;;
		read)
		    echo '('
		    read_list "protocols" "imap imaps"
		    read_list "mechanisms" "plain" | sed 's,-,_,g'
		    
		    if control dovecot-auth|grep -qs '^postfix$';then
			printf ' auth_port #t'
		    else
			printf ' auth_port #f'
		    fi
		    echo ')'
		    ;;
		write)
		    local p_args=
		    [ "$in_protocols_pop3" = "#t" ] && p_args="$p_args pop3"
		    [ "$in_protocols_pop3s" = "#t" ] && p_args="$p_args pop3s"
		    [ "$in_protocols_imap" = "#t" ] && p_args="$p_args imap"
		    [ "$in_protocols_imaps" = "#t" ] && p_args="$p_args imaps"
		    [ -n "$p_args" ] && write_list "protocols" "$p_args"


		    local m_args=
		    [ "$in_mechanisms_plain" = "#t" ] && m_args="$m_args plain"
		    [ "$in_mechanisms_login" = "#t" ] && m_args="$m_args login"
		    [ "$in_mechanisms_cram_md5" = "#t" ] && m_args="$m_args cram-md5"
		    [ "$in_mechanisms_digest_md5" = "#t" ] && m_args="$m_args digest-md5"
		    [ "$in_mechanisms_apop" = "#t" ] && m_args="$m_args apop"
		    [ "$in_mechanisms_ntlm" = "#t" ] && m_args="$m_args ntlm"
		    [ "$in_mechanisms_gssapi" = "#t" ] && m_args="$m_args gssapi"
		    [ "$in_mechanisms_rpa" = "#t" ] && m_args="$m_args rpa"
		    [ "$in_mechanisms_anonymous" = "#t" ] && m_args="$m_args anonymous"
		    [ "$in_mechanisms_otp" = "#t" ] && m_args="$m_args otp"
		    [ "$in_mechanisms_skey" = "#t" ] && m_args="$m_args skey"
		    [ "$m_args" ] && write_list "mechanisms" "$m_args"

		    if [ "$in_auth_port" = "#t" ];then
			control dovecot-auth postfix >&2
		    else
			control dovecot-auth none >&2
		    fi

		    #control already call service reload
		    #service dovecot condreload >&2
		    echo '()'
		    ;;
		*)
		    echo '#f'
		    ;;
	esac
}

message_loop
