#!@l_prefix@/lib/openpkg/bash @l_prefix@/etc/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"
    fetchmail_log_file="@l_prefix@/var/fetchmail/fetchmail.log"
    fetchmail_rc="@l_prefix@/etc/fetchmail/fetchmailrc"
    fetchmail_rc_master="@l_prefix@/etc/fetchmail/fetchmail.master"
    fetchmail_rc_numfiles="100"
    fetchmail_users="@l_prefix@/etc/fetchmail/fetchmail.users"
    fetchmail_pidfile="@l_prefix@/var/fetchmail/fetchmail.pid"

%common
    fetchmail_signal () {
        [ -f $fetchmail_pidfile ] && kill -$1 `cat $fetchmail_pidfile`
    }

%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
    rc fetchmail quarterly
    @l_prefix@/bin/fetchmail -f ${fetchmail_rc} -L ${fetchmail_log_file}

%stop -p 800 -u @l_susr@
    rcService fetchmail enable yes || exit 0
    rcService fetchmail active no  && exit 0
    rcVarIsYes fetchmail_daemon || exit 0
    @l_prefix@/bin/fetchmail --quit
    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

%reload -p 200 -u @l_susr@
    rcService fetchmail enable yes || exit 0
    rcService fetchmail active no  && exit 0
    rcVarIsYes fetchmail_daemon || exit 0
    @l_prefix@/bin/fetchmail

%quarterly -p 800 -u @l_susr@
    rcService fetchmail enable yes || exit 0
    rcVarIsYes fetchmail_daemon || exit 0
    if [ -s ${fetchmail_users} ] ; then
        cat ${fetchmail_users} | shtool subst \
            -e '/^[[:space:]]*#.*/d' | while read user comment
        do
                urc=`eval echo ~$user`/.fetchmailrc
                if [ -s $urc ] ; then
                    cat $urc | shtool subst \
                        -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;;' \
                         >/tmp/$user.$$
                    chmod 0600 /tmp/$user.$$
                    if @l_prefix@/bin/fetchmail --configdump -f /tmp/$user.$$ >/dev/null 2>&1; then
                        cat /tmp/$user.$$
                    fi
                    rm /tmp/$user.$$
                fi
        done >${fetchmail_rc}.tmp
        cat ${fetchmail_rc_master} ${fetchmail_rc}.tmp >${fetchmail_rc}.new
        rm ${fetchmail_rc}.tmp
        chmod 0600 ${fetchmail_rc}.new
        if ! cmp -s ${fetchmail_rc} ${fetchmail_rc}.new ; then
            shtool rotate -f \
                -n ${fetchmail_rc_numfiles} -m 644 -o @l_susr@ -g @l_mgrp@ \
                ${fetchmail_rc}
            mv ${fetchmail_rc}.new ${fetchmail_rc}
        fi
        rm -f ${fetchmail_rc}.new
     else
         if [ -s ${fetchmail_rc_master} ] ; then
             cp ${fetchmail_rc_master} ${fetchmail_rc}
             chmod 0600 ${fetchmail_rc}
         fi
     fi
    exit 0

%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_log_file}
    exit 0

