. $certbasedir/config/functions
. $domainbasedir/config/functions
. $apachebasedir/config/functions

. $(dirname $BASH_SOURCE)/config


# certcertdir certkeydir domaindn domain binddn bindpassword mailrelay mailrelayuser mailrelaypassword
function sogo_confprog()
{
  wrappermode=no;
  if [[ "$7" = *":465" ]]; then  wrappermode=yes; fi
  
  echo '/####CERTCERTDIR####/     { gsub(/####CERTCERTDIR####/,"'$1'"); }
        /####CERTKEYDIR####/      { gsub(/####CERTKEYDIR####/,"'$2'"); }
        /####HOST####/            { gsub(/####HOST####/,"'$(hostname)'"); }
        /####DOMAINDN####/        { gsub(/####DOMAINDN####/,"'$3'"); }
        /####DOMAIN####/          { gsub(/####DOMAIN####/,"'$4'"); }
        /####BINDDN####/          { gsub(/####BINDDN####/,"'$5'");  }      
        /####BINDPASSWORD####/    { gsub(/####BINDPASSWORD####/,"'$6'");  }      
        /####MAILRELAY####/       { gsub(/####MAILRELAY####/,"'$7'"); }
        /####MAILRELAYUSER####/   { gsub(/####MAILRELAYUSER####/,"'$8'"); }
        /####MAILRELAYPASSWD####/ { gsub(/####MAILRELAYPASSWD####/,"'$9'"); }
        /####WRAPPERMODE####/     { gsub(/####WRAPPERMODE####/,"'$wrappermode'"); }
                                  { print $0 }'

}

# prog
function write_postfix()
{
    for dir in sasl ldap maps
    do
      mkdir -p           $postfixconf/$dir >&$logfile 2>&$logfile
      chown root:postfix $postfixconf/$dir >&$logfile 2>&$logfile
      chmod 750          $postfixconf/$dir >&$logfile 2>&$logfile
      chmod g+s          $postfixconf/$dir >&$logfile 2>&$logfile
    done

    for file in main.cf master.cf ldap/aliases ldap/canonical ldap/mailbox_maps ldap/uid_maps sasl/passwd
    do
        template="$(findtemplate postfix $file)"
        savefile $postfixconf/$file
        awk "$1" < $template > $postfixconf/$file
        chgrp postfix $postfixconf/$file
        chmod 640 $postfixconf/$file
    done
    
    touch $postfixconf/maps/aliases
    chgrp postfix $postfixconf/maps/aliases
    chmod 640 $postfixconf/maps/aliases
    
    chmod 644 $postfixconf/main.cf

    postmap $postfixconf/sasl/passwd  >&$logfile 2>&$logfile
    postmap $postfixconf/maps/aliases >&$logfile 2>&$logfile
    systemctl restart postfix.service
}

# prog
function write_dovecot()
{
    mkdir -p /var/dovecot 2>&$logfile
    chown root:mail /var/dovecot
    chmod 770 /var/dovecot
    chmod g+s /var/dovecot
       
    for file in dovecot-ldap.conf.ext conf.d/10-auth.conf  conf.d/10-master.conf  conf.d/10-ssl.conf  conf.d/15-mailboxes.conf
    do
        template="$(findtemplate dovecot $file)"
        savefile $dovecotconf/$file
        awk "$1" < $template > $dovecotconf/$file
        chgrp dovecot $dovecotconf/$file
        chmod 640 $dovecotconf/$file
    done

    systemctl restart dovecot.service
}

function write_apache()
{
port=$(get_port)
sport=$(get_sport)

template="$(findtemplate sogo site.conf)"
site_writeconf "$(cat $template)" "$port" "$sport" "sogo" "sogo" "sogo.$1" "" "" "$datadir"

a2ensite sogo >&$logfile 2>&$logfile
systemctl reload apache2 >&$logfile 2>&$logfile

}
