#!/bin/bash

. exec/system/config/allg.sh
. exec/system/shell/allg/file.sh

for dir in pgsql sasl ldap
do
  mkdir -p           $postfixconf/$dir >&$logfile 2>&1
  chown root:postfix $postfixconf/$dir >&$logfile 2>&1
  chmod 750          $postfixconf/$dir >&$logfile 2>&1
  chmod g+s          $postfixconf/$dir >&$logfile 2>&1
done

prog='/####CERTCERTDIR####/     { gsub(/####CERTCERTDIR####/,"'$certcertdir'"); }
      /####CERTKEYDIR####/      { gsub(/####CERTKEYDIR####/,"'$certkeydir'"); }
      /####HOST####/            { gsub(/####HOST####/,"'$(hostname)'"); }
      /####DOMAIN####/          { gsub(/####DOMAIN####/,"'$domain'"); }
      /####DOMAINDN####/        { gsub(/####DOMAINDN####/,"'$domaindn'"); }
      /####BINDUSER####/        { gsub(/####BINDUSER####/,"'$sogouser'");  }      
      /####BINDPASSWORD####/    { gsub(/####BINDPASSWORD####/,"'$bindpassword'");  }      
      /####MAILRELAY####/       { gsub(/####MAILRELAY####/,"'$vamailrelayInput'"); }
      /####MAILRELAYUSER####/   { gsub(/####MAILRELAYUSER####/,"'$vamailrelayuserInput'"); }
      /####MAILRELAYPASSWD####/ { gsub(/####MAILRELAYPASSWD####/,"'$vamailrelaypasswordInput'"); }
                             { print $0 }'

for file in main.cf master.cf smtp_header_checks pgsql/aliases pgsql/canonical sasl/passwd ldap/mailbox_maps ldap/uid_maps
do
  find_template conffile sogo/postfix $file
  save_file $postfixconf/$file
  awk "$prog" < $conffile > $postfixconf/$file
  chgrp postfix $postfixconf/$file
  chmod 640 $postfixconf/$file
done

postmap $postfixconf/sasl/passwd  >/dev/null 2>&1

systemctl restart postfix.service
