#!/usr/bin/env bash # Author : Allan Christensen # First Created : 17102020 (DD-MM-YYYY) # Description : Installs Postfix and optionally Mailutils on Ubuntu 24.04 # License : MIT License (see LICENSE file for details) # # Are we root # if [[ $(id -u) -ne 0 ]]; then echo "" && echo "Must be root or use sudo" && echo "" ; exit ; fi # # Define variables and functions # install_utils="no" # # Function to check if a service is already running or not # serviceyes () { printf "\n%s" $service ; printf " is already running cannot continue...\n\n"; } servicealive () { status=$(systemctl is-active $service); if [[ "$status" == "active" ]]; then serviceyes ; exit 1; fi; } # # Usage function # usage() { printf "\nUsage:\n" printf " sudo ./postfixinstall [-m yes|no]\n\n" printf "Options:\n" printf " -m Install Mailutils (defaults to no)\n" printf " -h Show this help message\n\n" printf "Examples:\n" printf " sudo ./postfixinstall # install Postfix only\n" printf " sudo ./postfixinstall -m yes # install Postfix + Mailutils\n\n" exit 1 } # # If Postfix is already running then die # service="postfix" ; servicealive # # Let's go # clear # # Parse options # while getopts ":m:h" opt; do case "${opt}" in m) arg="${OPTARG}" if [[ -z "$arg" ]]; then printf "\nOption -m requires an argument (yes/no).\n" usage fi case "${arg,,}" in y|yes) install_utils="yes" ;; n|no) install_utils="no" ;; *) usage ;; esac ;; h) usage ;; :) printf "\nOption -%s requires an argument (yes/no).\n" "$OPTARG" usage ;; \?) printf "\nInvalid option: -%s\n" "$OPTARG" usage ;; esac done # # Require -m flag # if [[ "$OPTIND" -eq 1 ]]; then printf "\nMissing required option: -m \n" usage fi # # Install Postfix (non-interactive) and backup main.cf # printf "\nInstalling Postfix...\n" DEBIAN_FRONTEND=noninteractive apt-get install -y postfix >/dev/null 2>&1 [[ -f /etc/postfix/main.cf ]] && cp -p /etc/postfix/main.cf /etc/postfix/main.cf.bak.$(date +%d-%m-%Y-%H%M%S) systemctl enable postfix >/dev/null 2>&1 && systemctl restart postfix # # Optionally install Mailutils # if [[ "$install_utils" == "yes" ]]; then apt-get install -y mailutils >/dev/null 2>&1 ; fi # # All done # printf "\nAll Done...\n" # # End of script #