#!/bin/bash
# Postfix hook for ee-ips
# This hook generate a CIDR map file /etc/postfix/ee_ips that allow access from Easter-eggs IP
# addresses. This CIDR map file could be used for instance in mynetworks value:
#
# mynetworks = 127.0.0.0/8, [::1], cidr:/etc/postfix/ee_ips
#
# cSpell:words mynetworks postmap

set -e

if [[ ! -d /etc/postfix ]] || ! which postmap > /dev/null 2>&1; then
    [[ "$1" != "detect" ]] && echo "Postfix seem not installed"
    exit 1
fi
[[ "$1" == "detect" ]] && exit 0

# shellcheck source=/dev/null
source /usr/share/ee-ips/data/ee-ips.bash

cat << EOF > /etc/postfix/ee_ips
# Auto-generated file by ee-ips debian package
# All changes will be override on next install/upgrade

EOF
for ip in "${EE_IP4[@]}" "${EE_IP6[@]}"; do
    echo -e "$ip\tOK" >> /etc/postfix/ee_ips
done
postmap /etc/postfix/ee_ips || exit 1
echo "Postfix CIDR map file /etc/postfix/ee_ips updated"
