#!/usr/bin/perl -w

use esmith::Build::CreateLinks qw(:all);
use File::Basename;
use File::Path;

# email-conf - console-save, bootstrap-console-save, domain-create, domain-delete,
# ip-change, email-update, host-create, host-delete, host-modify, post-upgrade


foreach (qw(
		bouncefrom
		bouncehost
	   	concurrencylocal
	   	concurrencyremote
		databytes
		defaultdomain
		defaulthost
		doublebounceto
		envnoathost
		helohost
		locals
		me
		plusdomain
		rcpthosts
		smtproutes
		virtualdomains
	))
{
    templates2events("/var/qmail/control/$_", qw(
	console-save
	bootstrap-console-save
	domain-create
	domain-delete
	ip-change
	email-update
	host-create
	host-delete
	host-modify
	post-upgrade
	e-smith-qmail-update
	));
}

foreach (qw(
    /var/qmail/alias/.qmail-default
    /var/qmail/alias/.qmail-localdelivery-default
    ))
{
    templates2events($_, qw(
	console-save
	bootstrap-console-save
	domain-create
	domain-delete
	ip-change
	email-update
	host-create
	host-delete
	host-modify
	post-upgrade
	e-smith-qmail-update
	));
}

templates2events("/var/qmail/alias/.qmail-shared" , qw(bootstrap-console-save user-create user-modify user-delete));

#--------------------------------------------------
# actions for console-save event:
# write config files and create startup link
#--------------------------------------------------
$event = "console-save";

# If qmail-send is running, it will receive the SIGHUP and ignore the
# start command. If it is not running then the signal will be ignored
# and qmail-send will read its new configuration files anyway.

safe_symlink("sighup", "root/etc/e-smith/events/$event/services2adjust/qmail");

#--------------------------------------------------
# actions for bootstrap-console-save event:
# write config files and create startup link
#--------------------------------------------------
$event = "bootstrap-console-save";

event_link("pseudonym-cleanup", $event, "03");
event_link("qmail-update-group", $event, "20");
event_link("qmail-update-user", $event, "55");
templates2events("/var/qmail/users/assign", $event);
safe_symlink("/var/qmail/bin/qmail-newu", "root/etc/e-smith/events/$event/S55email-assign");

#--------------------------------------------------
# actions for domain-create event:
# rewrite config files and restart server
#--------------------------------------------------
$event = "domain-create";

# If qmail-send is running, it will receive the SIGHUP and ignore the
# start command. If it is not running then the signal will be ignored
# and qmail-send will read its new configuration files anyway.

safe_symlink("sighup", "root/etc/e-smith/events/$event/services2adjust/qmail");

#--------------------------------------------------
# actions for domain-delete event:
# rewrite config files and restart server
#--------------------------------------------------
$event = "domain-delete";


# If qmail-send is running, it will receive the SIGHUP and ignore the
# start command. If it is not running then the signal will be ignored
# and qmail-send will read its new configuration files anyway.

safe_symlink("sighup", "root/etc/e-smith/events/$event/services2adjust/qmail");

#--------------------------------------------------
# actions for email-update event:
# email parameters have been changed in the e-smith
# manager; update system security, rewrite email config
# files, configure other system files (crontab is the
# important one), and restart server
#--------------------------------------------------
$event = "email-update";

event_link("qmail-update-user", $event, "03");
event_link("pseudonym-cleanup", $event, "30");

# If qmail-send is running, it will receive the SIGHUP and ignore the
# start command. If it is not running then the signal will be ignored
# and qmail-send will read its new configuration files anyway.

safe_symlink("sighup", "root/etc/e-smith/events/$event/services2adjust/qmail");
safe_symlink("adjust", "root/etc/e-smith/events/$event/services2adjust/masq");
safe_symlink("restart", "root/etc/e-smith/events/$event/services2adjust/smtp-auth-proxy");

#--------------------------------------------------
# actions for group-create event:
#--------------------------------------------------
$event = "group-create";

event_link("pseudonym-cleanup", $event, "03");
event_link("qmail-update-group", $event, "20");
templates2events("/var/qmail/users/assign", $event);
safe_symlink("/var/qmail/bin/qmail-newu", "root/etc/e-smith/events/$event/S55email-assign");
safe_symlink("sighup", "root/etc/e-smith/events/$event/services2adjust/qmail");

#--------------------------------------------------
# actions for group-delete event:
#--------------------------------------------------
$event = "group-delete";

event_link("pseudonym-cleanup", $event, "03");
event_link("qmail-delete-group", $event, "20");
templates2events("/var/qmail/users/assign", $event);
safe_symlink("/var/qmail/bin/qmail-newu", "root/etc/e-smith/events/$event/S55email-assign");
safe_symlink("sighup", "root/etc/e-smith/events/$event/services2adjust/qmail");

#--------------------------------------------------
# actions for group-modify event:
#--------------------------------------------------
$event = "group-modify";

event_link("pseudonym-cleanup", $event, "03");
event_link("qmail-update-group", $event, "20");
templates2events("/var/qmail/users/assign", $event);
safe_symlink("/var/qmail/bin/qmail-newu", "root/etc/e-smith/events/$event/S55email-assign");
safe_symlink("sighup", "root/etc/e-smith/events/$event/services2adjust/qmail");

#--------------------------------------------------
# actions for user-create event:
#--------------------------------------------------
$event = "user-create";

event_link("qmail-update-user", $event, "20");
event_link("qmail-update-group", $event, "20");

#--------------------------------------------------
# actions for user-modify event
#--------------------------------------------------
$event = "user-modify";

event_link("qmail-update-user", $event, "20");
event_link("qmail-update-group", $event, "20");

#--------------------------------------------------
# actions for user-modify-admin event
#--------------------------------------------------
$event = "user-modify-admin";

event_link("qmail-update-user", $event, "20");

#--------------------------------------------------
# actions for user-delete event
#--------------------------------------------------
$event = "user-delete";

event_link("qmail-update-group", $event, "25");

#--------------------------------------------------
# actions for ip-up event:
#--------------------------------------------------
$event = "ip-up";

event_link("qmail-ipup", $event, "20");

#--------------------------------------------------
# actions for ip-change event:
#--------------------------------------------------
$event = "ip-change";

safe_symlink("sighup", "root/etc/e-smith/events/$event/services2adjust/qmail");

#--------------------------------------------------
# actions for e-smith-qmail-update event:
#--------------------------------------------------
$event = "e-smith-qmail-update";

safe_symlink("restart", "root/etc/e-smith/events/$event/services2adjust/qmail");
event_link("systemd-reload", $event, "89");
event_link("systemd-default", $event, "88");
event_link("pseudonym-cleanup", $event, "03");

#--------------------------------------------------
# actions for pseudonym-{create,delete,modify}
#--------------------------------------------------
foreach $event ( qw(pseudonym-create pseudonym-modify pseudonym-delete
		    user-create user-modify user-delete
		))
{
    event_link("pseudonym-cleanup", $event, "03");
    templates2events("/var/qmail/control/virtualdomains", $event);
    templates2events("/var/qmail/users/assign", $event);
    safe_symlink("/var/qmail/bin/qmail-newu", "root/etc/e-smith/events/$event/S55email-assign");
    safe_symlink("sighup", "root/etc/e-smith/events/$event/services2adjust/qmail");
}

safe_symlink(".qmail", "root/home/e-smith/.qmail-default");
safe_symlink(".qmail-shared", "root/var/qmail/alias/.qmail-shared-default");