#!/bin/bash
# info: delete sys vesta user ssl certificate
# options: NONE
#
# The script disables user domain ssl synchronization


#----------------------------------------------------------#
#                  Variable & Function                     #
#----------------------------------------------------------#

# Includes
source $VESTA/func/main.sh
source $VESTA/conf/vesta.conf


#----------------------------------------------------------#
#                    Verifications                         #
#----------------------------------------------------------#


#----------------------------------------------------------#
#                       Action                             #
#----------------------------------------------------------#

vst_crt="$VESTA/ssl/certificate.crt"
vst_key="$VESTA/ssl/certificate.key"

# Updating mail certificate
case $MAIL_SYSTEM in
    exim)           conf='/etc/exim/exim.conf';;
    exim4)          conf='/etc/exim4/exim4.conf.template';;
esac
if [ -e "$conf" ]; then
    sed -e "s|^tls_certificate.*|tls_certificate = $vst_crt|" \
        -e "s|^tls_privatekey.*|tls_privatekey = $vst_key|" -i $conf
fi

# Updating imap certificate
conf="/etc/dovecot/conf.d/10-ssl.conf"
if [ ! -z "$IMAP_SYSTEM" ] && [ -e "$conf" ]; then
    sed -e "s|ssl_cert.*|ssl_cert = <$vst_crt|" \
        -e "s|ssl_key.*|ssl_key = <$vst_key|" -i $conf
fi

# Moving old certificates
if [ -e "$VESTA/ssl/mail.crt" ]; then
    mv -f $VESTA/ssl/mail.crt $VESTA/ssl/mail.crt.old
fi
if [ -e "VESTA/ssl/mail.key" ]; then
    mv $VESTA/ssl/mail.key VESTA/ssl/mail.key.old
fi

# Updating vesta.conf value
sed -i "/MAIL_CERTIFICATE=/ d" $VESTA/conf/vesta.conf


#----------------------------------------------------------#
#                       Vesta                              #
#----------------------------------------------------------#

# Restarting services
if [ "$restart" != 'no' ]; then
    if [ ! -z "$MAIL_SYSTEM" ]; then
        $BIN/v-restart-service $MAIL_SYSTEM
    fi
    if [ ! -z "$IMAP_SYSTEM" ]; then
        $BIN/v-restart-service $IMAP_SYSTEM
    fi
fi

# Logging
log_event "$OK" "$ARGUMENTS"

exit
