#!/bin/sh

#########################################################
#
# 7PLUS manager for LINUX F6FBB BBS - F6BVP - February 2015
# 7PLUS manager for LINUX Version 1.2 (10/99)
#
# F6BVP - http://f6bvp.org
#
##########################################################
# For this script to be executed during fbb maintenance
# it must reside in /usr/local/lib/fbb/scripts/maintenance 
##########################################################
#
# THESE 3 VARIABLES MUST BE INITIALIZED !
#
#
# Directory in which fbb software is installed
#
DESTDIR=/usr/local
BASE_FBB=$DESTDIR/var/ax25/fbb
#
# 7PLUS administrator for report mails
#
# Should be like (no space character !) :
#   SP_ADM=F5NUF@F6FBB.FMLR.FRA.EU
#   SP_ADM=F6FBB
#   SP_ADM="F5NUF@F6FBB.FMLR.FRA.EU F6FBB"
#
SP_ADM=N0CALL

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

#
############################################################
# The following variables should be correct
# and do not need to be changed.
# However check carefully that the directories match your system
#############################################################
#
# MAIL.IN file
#
MAIL_IN=$BASE_FBB/mail/mail.in
#
# Name (and path) of the 7plus program
#
SPLUS=$DESTDIR/bin/7plus
#
# Name of the file created by m_filter
#
SP_LOG=$DESTDIR/lib/fbb/filter/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 $SP_LOG file to process..."
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

# If a 7+ log file exists
# Move it into the 7P directory
#
if [ -f $SP_LOG ]
then
	mv $SP_LOG $SP_DIR/7pl_log
fi
#
# 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 -maxdepth 1 -daystart -atime +$OLD_FILES -name '*' -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 ]
then
  for ADM in $SP_ADM
  do
    (echo "SP $ADM" ; echo "7pFBB report" ; cat 7pfbb_mail ; echo "/EX" ) >> $MAIL_IN
  done
  rm 7pfbb_mail
fi

#
# All correct... Bye !
#

exit 0
