#!/bin/bash

echo -e "\e[1;34m[ℹ]\e[0m Updating system..."
apt update -y && apt upgrade -y
echo -e "\e[1;32m[✔]\e[0m System updated successfully."

echo -e "\e[1;34m[ℹ]\e[0m Installing Postfix and Dovecot..."
apt install -y postfix dovecot-core dovecot-imapd dovecot-pop3d
echo -e "\e[1;32m[✔]\e[0m Postfix and Dovecot installed."

echo -e "\e[1;34m[ℹ]\e[0m Configuring Postfix..."
postconf -e "smtpd_tls_cert_file=/etc/letsencrypt/live/mail.example.com/fullchain.pem"
postconf -e "smtpd_tls_key_file=/etc/letsencrypt/live/mail.example.com/privkey.pem"
postconf -e "smtpd_use_tls=yes"
postfix reload
echo -e "\e[1;32m[✔]\e[0m Postfix configured."

echo -e "\e[1;34m[ℹ]\e[0m Configuring Dovecot..."
echo "ssl = required" >> /etc/dovecot/conf.d/10-ssl.conf
echo "ssl_cert = </etc/letsencrypt/live/mail.example.com/fullchain.pem" >> /etc/dovecot/conf.d/10-ssl.conf
echo "ssl_key = </etc/letsencrypt/live/mail.example.com/privkey.pem" >> /etc/dovecot/conf.d/10-ssl.conf
systemctl restart dovecot
echo -e "\e[1;32m[✔]\e[0m Dovecot configured."

echo -e "\e[1;32m[✔]\e[0m Mail server setup completed successfully!"
