#! /bin/sh
#
# Generate systemd service file from /etc/default
# Copyright 2016 Scott Kitterman
# All rights reserved.

# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
# 3. Neither the name of the University nor the names of its contributors
#    may be used to endorse or promote products derived from this software
#    without specific prior written permission.

# THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.

NAME=opendmarc

# Include defaults if available
if [ -f /etc/default/$NAME ] ; then
        . /etc/default/$NAME
else
RUNDIR=/var/run/$NAME
USER=$NAME
GROUP=$NAME
# Other package specific defaults can be added for if the defaults file is
# missing:
SOCKET=local:$RUNDIR/$NAME.sock
PIDFILE=$RUNDIR/$NAME.pid
fi

SERVICEFILE=$NAME.service
echo "# Automatically Generated by $NAME systemd service file generator." > $SERVICEFILE.new
echo "# To change the editable parameters, edit /etc/default/$NAME and then do" >> $SERVICEFILE.new
echo "# systemctl restart $NAME." >> $SERVICEFILE.new
echo "" >> $SERVICEFILE.new
echo "# If you are using OpenDMARC with SQL datasets it might be necessary to start" >> $SERVICEFILE.new
echo "# OpenDMARC after the database servers. For example, if using both MariaDB and" >> $SERVICEFILE.new
echo "# PostgreSQL, edit /etc/default/$NAME to add the needed definitions to" >> $SERVICEFILE.new
echo "# EXTRAAFTER. If used, mariadb.service and postgresql.service would have to be" >> $SERVICEFILE.new
echo "# added." >> $SERVICEFILE.new
echo "" >> $SERVICEFILE.new
echo "[Unit]" >> $SERVICEFILE.new
echo "Description=OpenDMARC Milter" >> $SERVICEFILE.new
echo "Documentation=man:opendmarc(8) man:opendmarc.conf(5)" >> $SERVICEFILE.new
echo "After=network.target nss-lookup.target $EXTRAAFTER" >> $SERVICEFILE.new
echo "" >> $SERVICEFILE.new
echo "[Service]" >> $SERVICEFILE.new
echo "Type=forking" >> $SERVICEFILE.new
echo "PIDFile=$PIDFILE" >> $SERVICEFILE.new
echo "PermissionsStartOnly=true" >> $SERVICEFILE.new
echo "User=$USER" >> $SERVICEFILE.new
echo "Group=$GROUP" >> $SERVICEFILE.new
echo "ExecStartPre=-/bin/sh /lib/$NAME/$NAME.service.generate" >> $SERVICEFILE.new
echo "ExecStartPre=-/bin/mkdir -p $RUNDIR" >> $SERVICEFILE.new
echo "ExecStartPre=-/bin/chown $USER.$GROUP $RUNDIR" >> $SERVICEFILE.new
echo "ExecStartPre=-/bin/chmod 770 $RUNDIR" >> $SERVICEFILE.new
echo "ExecStart=/usr/sbin/opendkim -p $SOCKET $DAEMON_OPTS -x /etc/$NAME.conf -u $USER -P $PIDFILE" >> $SERVICEFILE.new
echo "Restart=on-failure" >> $SERVICEFILE.new
echo "ExecReload=/bin/kill -USR1 \$MAINPID" >> $SERVICEFILE.new
echo "" >> $SERVICEFILE.new
echo "[Install]" >> $SERVICEFILE.new
echo "WantedBy=multi-user.target" >> $SERVICEFILE.new
