#!/bin/bash
#
# POSTFIX
# -------------------------------------------------------------------
#
echo -e "\n  start $0_postfix"

[ "X${SOMETHING_CHANGED:-}" = "X" ] && {
	echo -e "\nerror: this script cannot be run standalone, is part," >&2
	echo -e "   and is sourced, from main 'run-install' script" >&2
	exit 1
}

save_SOMETHING_CHANGED=$SOMETHING_CHANGED; SOMETHING_CHANGED=false

# SASL
#
dir="/var/spool/postfix/var/run/saslauthd"
[ -d $dir ] || {
	create_dir $dir root:root || exit $?
	dpkg-statoverride --force --update --add root sasl 755 $dir || exit $?
	SOMETHING_CHANGED=true
}


# INSTALL EMTPY LOCAL TEMPLATES, IF NEEDED


# POSTFIX

[ -d $postfixlocal ] || {
	mkdir -p $postfixlocal
	putwarning "$postfixlocal (dir)" \
		"a new config directory has been created to store" \
		"custom (machine dependent) postfix files"
}


egrep -q "postfix/*" $kusalocal/install || {
	(
	  echo
	  echo "# srv-mail module"
	  echo "#"
	  echo ":default_owner	root:root"
	  echo ":default_mode	440"
	  echo
	  echo "postfix/*		/etc/postfix/"
	  echo
	  getconfirm srv-mail.use_content_filter && {
		echo
	  	echo ":default_mode	444"
	  	echo "#80-local-rules.cf	$sa_config_dir/"
	  } || :
	) >>$kusalocal/install

	putwarning "$kusalocal (dir)" \
		"a new section was created in $kusalocal/install for srv-mail module;" \
		"please review also $kusalocal/run-install file and uncomment relevant" \
		"commands for srv-mail module"
}

# kusa < 1.1
# access should be splitted into access_client and access_sender
#
if [ -f $postfixlocal/access ]
then
	putwarning "$postfixlocal/access" \
		"starting from kusa 1.1 'access' is no longer used, instead" \
		"3 separate files should be used: access_client, access_sender " \
		"and access_relay; the original file will be copied into the" \
		"new 3 files, please check them and purge unnecessary entries"

	for file in access_client access_sender access_relay
	do
		if [ -f $postfixlocal/$file ]
		then
			putwarning "$postfixlocal/$file" \
				"cannot rename 'access' to '$file' (already present);" \
				"'access' file will be rename to 'access.old'"
			cp -a $postfixlocal/access $postfixlocal/access.old
		else
			cp -a $postfixlocal/access $postfixlocal/$file
		fi
	done
	mv $postfixlocal/access $postfixlocal/access.obsoleted
fi # -f $postfixlocal/access

[ -f $postfixdir/access ] && {
	mv $postfixdir/access $postfixdir/access.obsoleted
}
[ -f $postfixdir/helo_access ] && {
	mv $postfixdir/helo_access $postfixdir/access_helo
}
[ -f $postfixlocal/helo_access ] && {
	mv $postfixlocal/helo_access $postfixlocal/access_helo
}
purgefiles $postfixdir/helo_acces.db $postfixdir/access.db
#
# end of kusa < 1.1 patches


for file in access_helo access_sender access_recipient access_client access_relay transport sasl_passwd
do
	[ -f $postfixdir/$file ] || {
		installfile postfix/$file $postfixdir/ root:root 440 || exit_err $? "installing $file"
		postmap $postfixdir/$file
	}
	# one copy in kusa localdir, should be used as source and installed when run "kusa-reconf local"
	[ -f $postfixlocal/$file ] || {
		installfile postfix/$file $postfixlocal/ root:root 660 || exit_err $? "installing $file"
	}
done

# install empty config files if needed -- not mapped
#
for file in header_checks body_checks aliases-local
do
	[ -f $postfixdir/$file ] || {
		installfile postfix/$file $postfixdir/ root:root 440 || exit_err $? "installing $file"
	}
	# one copy in kusa localdir
	[ -f $postfixlocal/$file ] || {
		installfile postfix/$file $postfixlocal/ root:root 660 || exit_err $? "installing $file"
	}
done

# -------------------------------------------------------------------
changed_postfix=$SOMETHING_CHANGED; $save_SOMETHING_CHANGED && SOMETHING_CHANGED=true

# exception, any change made outside this script by 'install' file are assigned to postfix
$save_SOMETHING_CHANGED && changed_postfix=true

echo "  done $0_postfix"
# run-install_postfix (eof)
