#!@l_prefix@/bin/openpkg rc
##
##  rc.fetchmail -- Run-Commands
##

%config
    fetchmail_enable="$openpkg_rc_def"
    fetchmail_daemon="no"
    fetchmail_log_prolog="true"
    fetchmail_log_epilog="true"
    fetchmail_log_numfiles="10"
    fetchmail_log_minsize="1M"
    fetchmail_log_complevel="9"

%common
    fetchmail_rc="@l_prefix@/etc/fetchmail/fetchmailrc"
    fetchmail_master="@l_prefix@/etc/fetchmail/fetchmail.master"
    fetchmail_users="@l_prefix@/etc/fetchmail/fetchmail.users"
    fetchmail_logfile="@l_prefix@/var/fetchmail/fetchmail.log"
    fetchmail_pidfile="@l_prefix@/var/fetchmail/fetchmail.pid"
    fetchmail_signal () {
        [ -f $fetchmail_pidfile ] && kill -$1 `head -1 $fetchmail_pidfile`
    }
    fetchmail_reconfigure () {
        umask 077
        rcTmp -i
        tmpfile1=`rcTmp -f -n tmp1`
        tmpfile2=`rcTmp -f -n tmp2`
        (   echo "## **** AUTOMATICALLY GENERATED FILE -- DO NOT EDIT! ****"
            echo ""
            if [ -s $fetchmail_master ]; then
                cat $fetchmail_master
            fi
            if [ -s $fetchmail_users ]; then
                sed -e '/^[[:space:]]*#.*/d' \
                    -e '/^[[:space:]]*$/d' <$fetchmail_users |\
                while read user comment; do
                    fetchmailrc=`eval echo ~$user`/.fetchmailrc
                    if [ -s $fetchmailrc ]; then
                        sed <$fetchmailrc \
                            -e '/^[[:space:]]*set[[:space:]].*/s;^;X##FORBIDDEN##;' \
                            -e '/.*[[:space:]]preconnect[[:space:]].*/s;^;X##FORBIDDEN##;' \
                            -e '/.*[[:space:]]postconnect[[:space:]].*/s;^;X##FORBIDDEN##;' \
                            -e '/.*[[:space:]]idle[[:space:]].*/s;^;X##FORBIDDEN##;' \
                            -e '/.*[[:space:]]monitor[[:space:]].*/s;^;X##FORBIDDEN##;' \
                            -e '/.*[[:space:]]plugin[[:space:]].*/s;^;X##FORBIDDEN##;' \
                            -e '/.*[[:space:]]plugout[[:space:]].*/s;^;X##FORBIDDEN##;' \
                            -e '/^[[:space:]]*poll[[:space:]].*/s;\(.*\);X\1;' \
                            -e '/^[[:space:]]*server[[:space:]].*/s;\(.*\);X\1;' \
                            -e '/^[[:space:]]*#.*/s;\(.*\);X\1;' \
                            -e '/^X.*/!d' \
                            -e '/^X.*/s;^X;;' \
                            >$tmpfile1
                        if @l_prefix@/bin/fetchmail --configdump -f $tmpfile1 >/dev/null 2>&1; then
                            cat $tmpfile1
                        fi
                    fi
                done
            fi
        ) >$tmpfile2
        if cmp $fetchmail_rc $tmpfile2 >/dev/null 2>&1; then
            :
        else
            cp $tmpfile2 $fetchmail_rc
            chmod 600 $fetchmail_rc
        fi
        rcTmp -k
    }

%status -u @l_susr@ -o
    fetchmail_usable="unknown"
    fetchmail_active="no"
    rcService fetchmail enable yes && \
        fetchmail_signal 0 && fetchmail_active="yes"
    echo "fetchmail_enable=\"$fetchmail_enable\""
    echo "fetchmail_usable=\"$fetchmail_usable\""
    echo "fetchmail_active=\"$fetchmail_active\""

%start -p 200 -u @l_susr@
    rcService fetchmail enable yes || exit 0
    rcService fetchmail active yes && exit 0
    rcVarIsYes fetchmail_daemon || exit 0
    fetchmail_reconfigure
    @l_prefix@/bin/fetchmail \
        -f ${fetchmail_rc} \
        -L ${fetchmail_logfile} \
        --pidfile ${fetchmail_pidfile}

%stop -p 800 -u @l_susr@
    rcService fetchmail enable yes || exit 0
    rcService fetchmail active no  && exit 0
    rcVarIsYes fetchmail_daemon || exit 0
    fetchmail_signal 15
    rm -f ${fetchmail_pidfile} 2>/dev/null || true

%restart -p 200 -u @l_susr@
    rcService fetchmail enable yes || exit 0
    rcService fetchmail active no  && exit 0
    rcVarIsYes fetchmail_daemon || exit 0
    rc fetchmail stop start

%quarterly -p 800 -u @l_susr@
    rcService fetchmail enable yes || exit 0
    rcVarIsYes fetchmail_daemon || exit 0
    fetchmail_reconfigure

%daily -p 800 -u @l_susr@
    rcService fetchmail enable yes || exit 0
    rcVarIsYes fetchmail_daemon || exit 0
    shtool rotate -f \
        -n ${fetchmail_log_numfiles} -s ${fetchmail_log_minsize} -d \
        -z ${fetchmail_log_complevel} -m 644 -o @l_susr@ -g @l_mgrp@ \
        -P "${fetchmail_log_prolog}" \
        -E "${fetchmail_log_epilog}; rc fetchmail restart" \
        ${fetchmail_logfile}

