#!/bin/sh

##############################################
#
# 7PLUS manager for LINUX Version 1.1 (06/99)
#
# F6FBB - http://www.f6fbb.org
#
##############################################


##############################################
#
# THESE 3 VARIABLES MUST BE INITALIZED !
#

#
# Directory in which fbb software is installed
#
BASE_FBB=/usr/local/fbb

#
# 7PLUS administrator for report mails
#
# Should be like (no space character !) :
#   SP_ADM=F5NUF@F6FBB.FMLR.FRA.EU
#   SP_ADM=F6FBB
#
SP_ADM=N0CALL

#
# Number of days to delete old 7plus parts
#
OLD_FILES=30

#
##############################################


#
# The following variables should be correct
# they should not be changed
#

#
# MAIL.IN file
#
MAIL_IN=$BASE_FBB/mail.in

#
# Name (and path) of the 7plus program
#
SPLUS=$BASE_FBB/bin/7plus

#
# Name of the log file
#
SP_LOG=$BASE_FBB/m_filter.fwd

#
# Directory of the 7plus files (parts)
#
SP_DIR=$BASE_FBB/7pfbb

#
# Directory for the decoded files
#
SP_RES=$SP_DIR/ok


#############################################################################


if [ ! -f $SP_LOG ]
then

  echo "No log file. exiting..."
  exit 0

fi

if [ ! -d $SP_DIR ]
then
  echo "Creating $SP_DIR"
  mkdir $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 $SP_RES
  if [ $? != 0 ]
  then
    echo "Could not create $SP_RES. Aborting..."
    exit 2
  fi
fi

#
# Move the log file to the 7P directory
#
mv $SP_LOG $SP_DIR/7pl_log

#
# Go to the 7P directory
#
cd $SP_DIR

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

$SPLUS 7pl_log -y -x > /dev/null

#
# 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
    grep success 7pfbb_tmp >> 7pfbb_mail
    rm $SP_DIR/$name.p*
    echo "Ok"
  else
    echo "No"
  fi
  rm 7pfbb_tmp
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
    grep success 7pfbb_tmp >> 7pfbb_mail
    rm $SP_DIR/$name.7pl
    echo "Ok"
  else
    echo "No"
  fi
  rm 7pfbb_tmp
done

#
# Delete old parts only in the SP_DIR directory
#
find $SP_DIR -daystart -atime +$OLD_FILES -name '*' -maxdepth 1 -print -exec rm {} \; > 7pfbb_tmp

#
# Add the deleted files to the report file
#
if [ -s 7pfbb_tmp ]
then
  echo >> 7pfbb_mail
  echo "Deleting old 7plus parts:" >> 7pfbb_mail
  while read file
  do
    echo "   `basename $file`" >> 7pfbb_mail
  done < 7pfbb_tmp
fi
rm 7pfbb_tmp

#
# Create report message if needed
#

if [ -f 7pfbb_mail ]
do
  (echo "SP $SP_ADM" ; echo "7pFBB report" ; cat 7pfbb_mail ; echo "/EX" ) >> $MAIL_IN
  rm 7pfbb_mail
done

#
# All correct... Bye !
#

exit 0
