#!/bin/bash
#
# FETCHMAIL
# -------------------------------------------------------------------
#
echo -e "\n  start $0_fetchmail"

[ "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

# 2012.06
# activate fetchmail restart if installed and configured
#
# 2014.05
# removed, this job is now handled by logrotate
#
# 2014.12
# fix, user-fetchmail was missing from logrotate job, restored as cronjob
#
##if [ -f /etc/fetchmailrc ]
##then
	##installfile fetchmail.cron "/etc/cron.d/ku-fetchmail-restart" root:root 700
##else
	##uninstallfiles \
		##/etc/cron.d/ku-fetchmail-restart
##fi
##
if [ -f /etc/fetchmailrc ]
then
	installfile fetchmail-logrotate	   /etc/logrotate.d/fetchmail	root:root 700
	installfile ku-check-fetchmail-ssl /etc/cron.hourly/		root:root 755
else
	uninstallfiles \
		/etc/logrotate.d/fetchmail \
		/etc/cron.hourly/ku-check-fetchmail-ssl
fi


# 2014.01
# user fetchmail support 
needs_users_fetchmail=false
file_default="/etc/default/user-fetchmail"
file_cron="/etc/cron.daily/ku-user-fetchmail-restart"

for userdir in $(getent passwd | cut -d':' -f6)
do
	[ -f $userdir/.fetchmailrc ] && {
		needs_users_fetchmail=true
		break
	}
done
if $needs_users_fetchmail
then
	[ -f $file_default ] || installfile user-fetchmail.default $file_default root:root 550
	installfile ku-user-fetchmail-restart.cron $file_cron root:root 755
	add_init_script 99 user-fetchmail
else
	uninstallfiles \
		$file_cron \
		/etc/init.d/ku-user-fetchmail \
		/etc/rc*.d/???-ku-user-fetchmail
fi

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

echo "  done $0_fetchmail"
# run-install_fetchmail (eof)
