#!/bin/bash
#
# This is an sdm plugin for: postfix
#
# The plugin is called three times: for Phase 0, Phase 1, and post-install.
#

function loadparams() {
    source $SDMPT/etc/sdm/sdm-readparams
}

function converty() {
    [ -v enablesvc ] && enablesvc="y"
}

# $1 is the phase: "0", "1", or "post-install"
# $2 is the argument list: arg1=val1|arg2=val2|arg3=val3| ...
#
# Main code for the Plugin
#
phase=$1
pfx="$(basename $0)"     #For messages
args="$2"
vldargs="|enablesvc|relayhost|main_mailer_type|mailname|maincf|domain|rootmail|"
loadparams
assetdir="$SDMPT/etc/sdm/assets/postfix"

if [ "$phase" == "0" ]
then
    #
    # In Phase 0 all references to directories in the image must be preceded by $SDMPT
    #
    logtoboth "* Plugin $pfx: Start Phase 0"

    plugin_getargs $pfx "$args" "$vldargs" || exit
    plugin_printkeys

    mkdir -p $assetdir

    if [ "$maincf" != "" ]
    then
	if [ -f $maincf ]
	then
	    logtoboth "> Plugin $pfx: Copy postfix maincf file '$maincf' into IMG $assetdir/$(basename $maincf)"
	    cp -a $maincf $assetdir/$(basename $maincf)
	else
	    logtobothex "? Plugin $pfx: Cannot find maincf file '$maincf'"
	fi
    fi
    logtoboth "* Plugin $pfx: Complete Phase 0"

elif [ "$phase" == "1" ]
then
    #
    # Phase 1 (in nspawn)
    #
    logtoboth "* Plugin $pfx: Start Phase 1"
    #logfreespace "at start of Plugin $pfx Phase 1"
    plugin_getargs $pfx "$args" "$vldargs"
    [ "$relayhost" == "" ] && relayhost="NoRelayHost"
    [ "$main_mailer_type" == "" ] && main_mailer_type="Satellite system"
    [ "$mailname" == "" ] && mailname="NoDomain.com"
    debconf-set-selections <<< "postfix postfix/main_mailer_type select $main_mailer_type"
    debconf-set-selections <<< "postfix postfix/mailname string $mailname"
    debconf-set-selections <<< "postfix postfix/relayhost string $relayhost"

    logtoboth "> Plugin $pfx: Install postfix and bsd-mailx"
    installpkgsif "postfix bsd-mailx libsasl2-modules"
    #logfreespace "at end of $pfx Phase 1"
    logtoboth "* Plugin $pfx: Complete Phase 1"
else
    #
    # Plugin Post-install edits
    #
    logtoboth "* Plugin $pfx: Start Phase post-install"
    #logfreespace "at start of Plugin $pfx Phase post-install"

    plugin_getargs $pfx "$args" "$vldargs"
    [ "$relayhost" == "" ] && relayhost="NoRelayHost"
    [ "$main_mailer_type" == "" ] && main_mailer_type="Satellite system"
    [ "$mailname" == "" ] && mailname="NoDomain.com"

    cp -a /etc/postfix/main.cf /etc/postfix/main.cf.orig
    if [ "$maincf" != "" ]
    then
	if [ -f $assetdir/$(basename $maincf) ]
	then
	    logtoboth "> Plugin $pfx: Replace /etc/postfix/main.cf with $assetdir/$(basename $maincf)"
	    mv /etc/postfix/main.cf /etc/postfix/main.cf.sdm
	    cp -a $assetdir/$(basename $maincf) /etc/postfix/main.cf
	    setfileownmode /etc/postfix/main.cf 644
	fi
    else
	#
	# Using provided main.cf, so modify what we can
	#
	logtoboth "> Plugin $pfx: Set postfix completion script to run in sdm FirstBoot"
	pf01="/etc/sdm/0piboot/080-complete-postfix.sh"
	rm -f $pf01
	[ "$domain" != "" ] && dmn=".${domain}" || dmn=""
	cat > $pf01 <<EOF
#!/bin/bash
source /etc/sdm/sdm-readparams
logger "sdm FirstBoot: Complete postfix configuration"
hostname="\$(hostname)"
hostname="\${hostname%.*}"
sed -i "s/myhostname = .*/myhostname = \${hostname}${dmn}/" /etc/postfix/main.cf
#Rerun make-ssl-cert now that host name is known
make-ssl-cert generate-default-snakeoil --force-overwrite
newaliases
[ "$enablesvc" != "" ] && systemctl enable --now postfix
EOF
    fi

    if [ "$rootmail" != "" ]
    then
	logtoboth "> Plugin $pfx: Update /etc/aliases to forward root mail to '$rootmail'"
	echo "root: $rootmail" >> /etc/aliases
	echo "#$myuser:  $rootmail" >> /etc/aliases
    fi

    plugin_addnote ""
    plugin_addnote "> Plugin $pfx: *** postfix service notes ***"
    if [ "$maincf" == "" ]
    then
	systemctl disable postfix > /dev/null 2>&1
	plugin_addnote ""
	plugin_addnote "*** postfix service notes ***"
	plugin_addnote ""
	plugin_addnote " * You WILL need to modify /etc/postfix/main.cf once you burn and boot"
	plugin_addnote "   The hostname in /etc/postfix/main.cf will be modified when known, during sdm FirstBoot"
	plugin_addnote ""
	plugin_addnote " * Tip: Once postfix working correctly:"
	plugin_addnote "   Save /etc/postfix.main.cf and use it with option maincf to this plugin"
	plugin_addnote ""
	plugin_addnote " * Tip: Make sure that your email clients always use 'send from' the email service account you're routing through"
	plugin_addnote "   (e.g., myaccount@gmail.com, myaccount@outlook.com, etc.)"
    fi

    plugin_addnote ""
    plugin_addnote "* If needed use 'sudo systemctl enable [--now] postfix' to enable/start the service"
    plugin_addnote ""
    #logfreespace "at end of $pfx Custom Phase post-install"
    logtoboth "* Plugin $pfx: Complete Phase post-install"
fi
