#!@l_prefix@/lib/openpkg/bash @l_prefix@/etc/rc
##
##  rc.sendmail -- Run-Commands for Sendmail Daemon
##  Copyright (c) 2000-2001 Cable & Wireless Deutschland GmbH
##  Copyright (c) 2000-2001 Ralf S. Engelschall <rse@engelschall.com>
##
##  Permission to use, copy, modify, and distribute this software for
##  any purpose with or without fee is hereby granted, provided that
##  the above copyright notice and this permission notice appear in all
##  copies.
##
##  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED 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 AUTHORS AND COPYRIGHT HOLDERS AND THEIR
##  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. 
##

%config
    mta_name="sendmail"
    mta_aliases_file="@l_prefix@/etc/sendmail/t.aliases"
    mta_aliases_update="cd @l_prefix@/etc/sendmail && make t.aliases.db"
    sendmail_enable="yes"
    sendmail_flags_in="-bd"
    sendmail_flags_out="-q60s"
    sendmail_flags=""
    sendmail_rotate_prolog=""
    sendmail_rotate_epilog=""
    sendmail_rotate_numfiles="10"
    sendmail_rotate_minsize="1M"
    sendmail_rotate_complevel="9"

%common 
    sendmail_start () {
        if opServiceEnabled sendmail; then
            @l_prefix@/sbin/sendmail $sendmail_flags_in $sendmail_flags \
                -OPidFile=@l_prefix@/var/sendmail/sendmail.in.pid
            @l_prefix@/sbin/sendmail $sendmail_flags_out $sendmail_flags \
                -OPidFile=@l_prefix@/var/sendmail/sendmail.out.pid
        fi
    }
    sendmail_signal () {
        if opServiceEnabled sendmail; then
            if [ -f @l_prefix@/var/sendmail/sendmail.in.pid ]; then
                rpmtool signal -c -d2 \
                    -p `head -1 @l_prefix@/var/sendmail/sendmail.in.pid` "$@"
            fi
            if [ -f @l_prefix@/var/sendmail/sendmail.out.pid ]; then
                rpmtool signal -c -d2 \
                    -p `head -1 @l_prefix@/var/sendmail/sendmail.out.pid` "$@"
            fi
        fi
    }

%start -p 200 -u root
    sendmail_start

%stop -p 200 -u root
    sendmail_signal TERM KILL

%restart -u root
    sendmail_signal TERM KILL
    sleep 2
    sendmail_start

%reload -u root
    #   sendmail does not like SIGHUP on queue-only processes,
    #   so we have to perform actually a restart...
    sendmail_signal TERM KILL
    sleep 2
    sendmail_start

%daily -u root
    if opServiceEnabled sendmail; then
        opTmpDirGen sendmail
        opTmpDirFile sendmail hint hintfile
        prolog="true"
        if [ ".$sendmail_rotate_prolog" != . ]; then
            prolog="$prolog; $sendmail_rotate_prolog"
        fi
        epilog="echo 'epilog' >$hintfile"
        if [ ".$sendmail_rotate_epilog" != . ]; then
            epilog="$epilog; $sendmail_rotate_epilog"
        fi
        shtool rotate -f \
            -n${sendmail_rotate_numfiles} -s${sendmail_rotate_minsize} \
            -d -z${sendmail_rotate_complevel} \
            -o@l_musr@ -g@l_mgrp@ -m644 \
            -P "$prolog" \
            -E "$epilog" \
            @l_prefix@/var/sendmail/sendmail.log
        if [ ".`cat $hintfile`" = ".epilog" ]; then
            sendmail_signal TERM KILL
            sleep 2
            sendmail_start
        fi
        opTmpDirDel sendmail
    fi

