#!/bin/sh /etc/rc.common
# Copyright (C) 2014 OpenWrt.org

START=72
STOP=50
EXTRA_COMMANDS="status abort flush postinst"
EXTRA_HELP="	status	Display the service status
	abort	Stop the service abruptly. Running processes are signaled to stop immediately
	flush	Force delivery: attempt to deliver every message in the deferred mail queue
	postinst Force running a script that checks for users, group, configuration, permissions, etc"


postinst() {
	if [ -z "$(postconf -nh myhostname)" ]; then
		postconf -e "myhostname = $(uci get system.@system[0].hostname)"
	fi
	if [ -z "$(postconf -nh mydomain)" ]; then
		postconf -e "mydomain = $(uci get dhcp.@dnsmasq[0].domain)"
	fi
	if [ -z "$(postconf -nh mynetworks_style)" ]; then
		postconf -e "mynetworks_style = subnet"
	fi
	mail_spool_directory=$(postconf -h mail_spool_directory)
	if [ ! -d $mail_spool_directory ]; then
		mkdir -p -m 0755 $mail_spool_directory
		chown -R postfix $mail_spool_directory
	fi

	postfix set-permissions
	postfix post-install upgrade-source
	postfix upgrade-configuration
	newaliases
	postmap $(postconf -h config_directory)/virtual
	postfix check
}

start() {
	if [ -z "$(postconf -nh myhostname)" ]; then
		postinst
	fi
	postfix start
}

stop() {
	postfix stop
}

reload() {
	postfix reload
}

status() {
	postfix status
}

abort() {
	postfix abort
}

flush() {
	postfix flush
}