#!/bin/sh
#
#  $MBSE_ROOT/etc/run_inout
#
#  Script to check for mail in the inbound and mail to export from the BBS.
#  This will do all handling of fidonet mail and files. This script must be
#  installed in cron. Example cron command:
#
#  * * * * * /opt/mbse/etc/run_inout
#
#  You NEED TO CUSTOMIZE the News/e-mail Gateway in this script!
#
#  The lines with "mbfido" have two versions, select the one you want.
#
#  13-Jul-2000 MB.

if [ "$MBSE_ROOT" = "" ]
then
    export MBSE_ROOT=`cat /etc/passwd | grep mbse: | awk -F ':' '{ print $6}'`
fi

#  Prevent that 2 copies run at the same time, it won't do any harm,
#  but why should we. Also, don't run if the bbs is closed and if the
#  system is running on UPS battery power.
#
if [ -f $MBSE_ROOT/sema/bbsclosed ]; then
    exit 1
fi
if [ -f $MBSE_ROOT/sema/run_inout.run ]; then
    exit 1
fi
if [ -f $MBSE_ROOT/sema/upsalarm ]; then
    exit 1
fi
/bin/touch $MBSE_ROOT/sema/run_inout.run
/bin/touch $MBSE_ROOT/sema/run_inout.last


#  Now set some handy variables.
#
PINBOUND=/SYS/usr/mail/inbound
GATE=$MBSE_ROOT/gate
export PATH=/usr/bin:/bin:$MBSE_ROOT/bin:


#  If someone wrote a mail at the bbs, scan for mail and process it.
#
if [ -f $MBSE_ROOT/sema/mailout ]; then
    $MBSE_ROOT/bin/mbfido scan -quiet
    touch $MBSE_ROOT/sema/newsout
fi


#  If there is new mail coming from the newsserver, process it.
#  Note that this is for node 100/99 Your *.opk filename is not
#  this name!
#
if [ -f $GATE/outbound/00640063.opk/current.tmp ]; then
    mv -f $GATE/outbound/00640063.opk/current.tmp $PINBOUND/`mbseq`.pkt
    /bin/touch $MBSE_ROOT/sema/mailin
fi


#  Process any mail or files
#
if [ -f $MBSE_ROOT/sema/mailin ]; then
    $MBSE_ROOT/bin/mbfido tic toss -quiet
#   $MBSE_ROOT/bin/mbfido tic toss web -quiet
    touch $MBSE_ROOT/sema/newsout
    #
    #  If there was bad mail, move it to a seperate directory.
    #
    for file in `/bin/ls $PINBOUND/*.bad 2>/dev/null`; do
         mv -f $file $PINBOUND/bad
    done;
    rm -f $MBSE_ROOT/sema/mailin
fi


#  Now gate the messages to the newsserver. I use the ifmail
#  package for this. Also note that my gateway is 2:2801/899.888
#  As your addresses are different then mine, you have to figure out
#  the right directory and filenames for your system.
#
if [ -f $MBSE_ROOT/sema/newsout ]; then
    if [ -f /SYS/usr/mail/out/0af10383.pnt/00000378.flo ]; then
        cp -f /SYS/usr/mail/out/0af10383.pnt/00000378.* $GATE/inbound
        rm -f /SYS/usr/mail/out/0af10383.pnt/00000378.*
        rm -f $MBSE_ROOT/gate/inbound/00000378.flo
        $MBSE_ROOT/bin/ifunpack
    fi
    #
    #  Now for the internet gateway, 92:100/88
    #
    if [ -f /SYS/usr/mail/bibnet/00640058.flo ]; then
        cp -f /SYS/usr/mail/bibnet/0000000b.* $GATE/inbound
        rm -f /SYS/usr/mail/bibnet/0000000b.*
        rm -f /SYS/usr/mail/bibnet/00640058.flo
        $MBSE_ROOT/bin/ifunpack
    fi
    #
    rm -f $MBSE_ROOT/sema/newsout
fi


#  Check for compile nodelist semafore
#
if [ -f $MBSE_ROOT/sema/mbindex ]; then
    $MBSE_ROOT/bin/mbindex -quiet
fi


#  Link the message base if told to do so.
#
if [ -f $MBSE_ROOT/sema/msglink ]; then
    $MBSE_ROOT/bin/mbmsg link -quiet
fi

#  Create new filerequest index of told to do so.
#  If you want to update your web pages, use the
#  second line.
#
if [ -f $MBSE_ROOT/sema/reqindex ]; then
    $MBSE_ROOT/bin/mbfile index -quiet
#   $MBSE_ROOT/bin/mbfile index web -quiet
fi


#  Finally, remove the lock semafore.
#
rm -f $MBSE_ROOT/sema/run_inout.run

