#!/bin/sh
##############################################
#
# 7PLUS auto-extracter and manager for LINUX
#
# F6FBB - 1996
#
##############################################


#
# ALL THESE VARIABLES MUST BE INITALIZED !
#

#
# 7PLUS administrator for report mails
#
SP_ADM=HI8GN

#
# MAIL.IN file
#
MAIL_IN=/var/ax25/fbb/mail/mail.in

#
# Name (and path) of the 7plus program
#
SPLUS=/usr/lib/fbb/tool/7plus

#
# Name of the log file
#
SP_LOG=/usr/lib/fbb/filter/m_filter.fwd

#
# Directory of the 7plus files (parts)
#
SP_DIR=/var/ax25/fbb/7pl/7pfbb

#
# Directory for the decoded files
#
SP_RES=/var/ax25/fbb/fbbdos/yapp/7pfbb

if [ ! -d $SP_DIR ]
then
  echo "Creating $SP_DIR"
  mkdir -p $SP_DIR
  if [ $? != 0 ]
  then
    echo "Could not create $SP_DIR. Aborting..."
    exit 1
  fi
fi

if [ ! -d $SP_RES ]
then
  echo "Creating $SP_RES"
  mkdir -p $SP_RES
  if [ $? != 0 ]
  then
    echo "Could not create $SP_RES. Aborting..."
    exit 2
  fi
fi

# Move the log file to the 7P directory
#
if [ ! -f $SP_LOG ];then
  echo "No log file. exiting..."
  exit 0
else
    echo "Log file. exiting..."
    mv $SP_LOG $SP_DIR/m_filter.fwd
fi

# Go to the 7P directory
#
cd $SP_DIR

#
# Extract 7plus files from the log file and put them in the result directory
#

$SPLUS m_filter.fwd -y -x > /dev/null
rm -rf m_filter.fwd

#
# Go to the result directory to decode files
#
cd $SP_RES

for file in $SP_DIR/*.p01
do
  name=`basename $file .p01`
  echo -n "Trying to decode $SP_DIR/$name ... "
  $SPLUS $SP_DIR/$name > 7pfbb_tmp
  if [ $? = 0 ]
  then
    echo "SP $SP_ADM"      >> $MAIL_IN
    echo "7pFBB report"    >> $MAIL_IN
    grep success 7pfbb_tmp >> $MAIL_IN
    echo "/EX"             >> $MAIL_IN
    rm -rf $SP_DIR/$name.p*
    echo "Ok"
  else
    echo "No"
  fi
  rm -rf 7pfbb_tmp
  rm -rf 7plus7pfbb.p*
done

for file in $SP_DIR/*.7pl
do
  name=`basename $file .7pl`
  echo -n "Trying to decode $SP_DIR/$name ... "
  $SPLUS $SP_DIR/$name > 7pfbb_tmp
  if [ $? = 0 ]
  then
    echo "SP $SP_ADM"      >> $MAIL_IN
    echo "7pFBB report"    >> $MAIL_IN
    grep success 7pfbb_tmp >> $MAIL_IN
    echo "/EX"             >> $MAIL_IN
    rm -rf $SP_DIR/$name.7pl
    echo "Ok"
  else
    echo "No"
  fi
  rm -rf 7pfbb_tmp
  rm -rf 7plus7pfbb.p*
done

exit 0
