#!/bin/bash

. ${TOOLKIT}-functions.sh


postfixdir="/etc/postfix"
kusalocal="/etc/kusa/modules/local"
postfixlocal="$kusalocal/postfix"
dovecot_user="dovecot:dovecot"



relink_dir()
{
	local dir=$1
	local work=$2

	[ -L $dir ] || {
		echo " 	moving original $dir -> $dir.orig"
		mv $dir $dir.orig				|| exit $?
		[ -d $work ] || {
			create_dir $work root:root		|| exit $?
			chown --reference $dir.orig $work	|| exit $?
			chmod --reference $dir.orig $work	|| exit $?
			cp -afr $dir.orig/* $work/.		|| exit $?
		}
		echo " 	link $dir -> $work"
		ln -s $work $dir || exit $?
		SOMETHING_CHANGED=true
	}
}





# (MAIN)


# SANITY CHECKS

here=`pwd`
cd /etc/postfix

workdir=`jtconf path.workdir`	|| exit_missing_define path.workdir
dir=$workdir/postfix/virtual.d

[ -L virtual.d ] || {
	if [ -d virtual.d ]
	then
		relink_dir /etc/postfix/virtual.d $dir || exit $?
	else
		create_dir $dir root:root || exit $?
		ln -s $dir .
	fi
}

# aliases file is a different beast
#
[ -L aliases ] || {
	[ -f aliases -a -s aliases ] && {
		echo " WARNING: saving `pwd`/aliases to aliases.save"
		mv aliases aliases.save || exit $?
	}
	rm -f aliases
	ln -s ../aliases .	# link system wide /etc/aliases here
}

# initialize empty mapfiles
#
for file in aliases virtual
do
	[ -f $file ] || {
		cp /dev/null $file
		postmap $file || exit_err $? "running postmap $file"
	}
done

cd $here


# this is a complex module, prone to be splitted in many
# submodules in the future; at the moment we need an hach
# to avoid unnecessary daemons restarts and speed up things
#
# at a logical block start:
#
# - save_SOMETHING_CHANGED holds $SOMETHING_CHANGED status locally
# - SOMETHING_CHANGED is reset to 'false'
#
# at a logical block end:
#
# - dedicated changed_* var is set to SOMETHING_CHANGED value
# - SOMETHING_CHANGED is set back to 'true' if the previous saveed
#   value was true as well, or left unchanged, so reflecting any
#   change occurred in the logical block
#
# for easy maintenance we extracted the logical blocks code to
# external files
#
save_SOMETHING_CHANGED=

changed_postfix=false
changed_content_filters=false
changed_fetchmail=false
changed_dkim=false

. $0_postfix
. $0_content_filters
. $0_fetchmail
. $0_dkim



# default file for ku-all-mail-sync script (cron job)
#
file="ku-all-mail-sync.default"
dest="/etc/default/ku-all-mail-sync"
[ -f $dest ] || {
	installfile $file $dest root:root 664
}


# missing rsyslog files (devuan?)
#
file=/var/log/mail.log
[ -f $file ] || {
	echo "  creating $file ..."
	:> $file
	chown syslog:adm $file
	restart_service rsyslog rsyslogd || :
}

# 2025-02-23
# - virtual users database
#
user=$(jtconf dovecot.vmail_user)
uid="2902"
group=$(jtconf dovecot.vmail_gid)
vhome=$(jtconf dovecot.vmail_homes)

getent passwd $user >/dev/null || {
	echo "  adding new user: $user ($uid)"
	useradd -c "dovecot virtual mail" -d "$vhome" -g $group -u $uid \
			-s /usr/sbin/nologin $user
	usermod --lock $user
}

[ -d "$vhome" ] || {
	create_dir "$vhome" $user:$group 700
}

file="/etc/dovecot/users"
[ -f $file ] || {
	installfile "dovecot2.0/users" $file "$dovecot_user" 660
}


# 2025-03-05
#
file="99-ku-debug.conf"
[ -f "/etc/dovecot/conf.d/$file" ] || {
	installfile "dovecot2.0/$file" "/etc/dovecot/conf.d/" "root:root" 664
}


# remove exim4 package if still installed
#
exim_pkgs="
	exim4
	exim4-base
	exim4-config
	exim4-daemon-light
"
remove_pkgs $exim_pkgs


# cleanup (old version files) and other crappy files
#
purgefiles \
	/root/bin/archivia-email \
	/root/bin/list-email-by-year \
	/usr/bin/ku-mail-list-by-year

# 2014.01
# cleanups
uninstallfiles \
	/etc/postfix/crea_virtual.sh \
	/usr/bin/ku-maildirs-create \
	/etc/cron.d/ku-fetchmail-restart



$changed_postfix && {
	restart_service saslauthd saslauthd
	restart_service dovecot dovecot
	restart_service kuwatcher

	[ -f /etc/postfix/Makefile ] && {
		cd /etc/postfix
		ls -d virtual.d/* 2>/dev/null >/dev/null && make || :
	}
	restart_service postfix qmgr
}

$changed_content_filters && {
	reload_apparmor_profile usr.sbin.clamd
	reload_apparmor_profile usr.bin.freshclam

	/etc/cron.daily/sa-update-cron

	cd /
	restart_service clamav-freshclam freshclam
	restart_service clamav-daemon clamd
	restart_service amavis 
}

$changed_fetchmail && { 
	restart_service fetchmail
	[ -f /etc/init.d/user-fetchmail ] && {
		restart_service user-fetchmail
	}
}

$changed_dkim && { 
	restart_service opendkim
}

#$SOMETHING_CHANGED && {
#}

exit 0
