#! /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=opendkim

# 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=/lib/$NAME/$NAME.service

# Create the new service file
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 OpenDKIM with SQL datasets it might be necessary to start" >> $SERVICEFILE.new
echo "# OpenDKIM 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=OpenDKIM DomainKeys Identified Mail (DKIM) Milter" >> $SERVICEFILE.new
echo "Documentation=man:opendkim(8) man:opendkim.conf(5) man:opendkim-genkey(8) man:opendkim-genzone(8) man:opendkim-testadsp(8) man:opendkim-testkey http://www.opendkim.org/docs.html" >> $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 "ExecStart=/usr/sbin/opendkim -p $SOCKET $DAEMON_OPTS -x /etc/$NAME.conf -u $USER -P $PIDFILE" >> $SERVICEFILE.new
echo "TimeoutStartSec=10" >> $SERVICEFILE.new
echo "ExecReload=/bin/kill -USR1 \$MAINPID" >> $SERVICEFILE.new
echo "" >> $SERVICEFILE.new
echo "[Install]" >> $SERVICEFILE.new
echo "WantedBy=multi-user.target" >> $SERVICEFILE.new

if [ "$(md5sum < /lib/systemd/system/$NAME.service)" != "$(md5sum < $SERVICEFILE.new)" ] ; then
    mv $SERVICEFILE.new /lib/systemd/system/$NAME.service
    systemctl daemon-reload
    systemctl restart $NAME
fi

