#!/bin/sh

##############################################
#
# 7PLUS auto-extracter and manager for LINUX
#
# F6FBB - 1996
# adapted by PE1ICQ - 2001
#
##############################################

PREFIX=$DESTDIR/usr
SBIN=$PREFIX/sbin
FBBCONF=/etc/ax25/fbb.conf

#
# THIS VARIABLE MUST BE INITALIZED !
# ==================================
#
# 7PLUS administrator(s) for report mails
#
SYSOP=`$SBIN/fbbgetconf -f $FBBCONF sysop`
SYSMAIL=`$SBIN/fbbgetconf -f $FBBCONF sysmail`
[ -z "$SYSOP" ] || SP_ADM=$SYSOP
[ -z "$SYSMAIL" ] || SP_ADM=$SYSMAIL
#
# If you don't want to send reports to the calls taken 
# from /etc/ax25/fbb.conf then uncomment the line below 
# and replace SYSOP by the list of callsign(s) separated by a space.
#
#SP_ADM="SYSOP"

#
# THESE VARIABLES SHOULD BE OK
#

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

#
# Directory of the 7plus files (parts)
#
SP_DIR=`$SBIN/fbbgetconf -f $FBBCONF data`/7plus/7pfbb_dir

#
# Directory for the decoded files
#
#SP_RES=$SP_DIR/ok
SP_RES=`$SBIN/fbbgetconf -f $FBBCONF fbbdos | tr -d *,`/auto7p

#
# Delay for the part files to be deleted (2 months)
#
PART_DELAY=60

#
# Should the decoded files be deleted (YES/NO)
# Warning, 'YES' will destroy the decoded files !!
#
FILE_CLEAN=NO

#
# Delay for the decoded files to be deleted (1 month)
#
FILE_DELAY=30

#
# Don't change anymore line
#

MAIL_TMP=/tmp/7pltmp.$$
FBBVERSION="2.1"

#
# MAIL.IN file
#
MAIL_IN=`$SBIN/fbbgetconf -f $FBBCONF import`

#
# Name of the log file
#
SP_LOG=`$SBIN/fbbgetconf -f $FBBCONF fdir`/m_filter.fwd

report()
{
	for ADM in $SP_ADM ; do
		echo "#"
		echo "SP $ADM"
		echo "7pFBB report"
	    echo
	    echo "7pfbb v$FBBVERSION (f6fbb - 2000, pe1icq - 2001)"
	    echo
		cat $1
		echo "/EX"
	done
}

not_found()
{
	echo "Could not find 7plus program..."
	echo "7pfbb need it to work !"
	echo
	echo "Check that :"
	echo "  - the 7plus package is installed"
	echo "  - the SPLUS variable in 7pfbb script is correctly set"
	echo "  - the 7plus program is executable"
}

if [ ! -x $SPLUS ] ; then
	not_found > $MAIL_TMP
	report $MAIL_TMP >> $MAIL_IN
	exit 0
fi

if [ ! -f $SP_LOG ]
then
	echo "No log file. exiting..."
	exit 0
fi

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

echo >> $MAIL_TMP

nbdel=`find $SP_DIR -maxdepth 1 -type f -mtime +$PART_DELAY -exec rm -f {} \; -print | wc -l`
if [ $nbdel -gt 0 ] ; then
	echo "$nbdel 7plus parts deleted   (older than $PART_DELAY days)" >> $MAIL_TMP
fi                                                                                         

if [ $FILE_CLEAN = YES ] ; then
	nbdel=`find $SP_RES -maxdepth 1 -type f -mtime +$FILE_DELAY -exec rm -f {} \; -print | wc -l`
	if [ $nbdel -gt 0 ] ; then
		echo "$nbdel decoded files deleted (older than $FILE_DELAY days)" >> $MAIL_TMP
	fi                                                                                         
fi

if [ -f $MAIL_TMP ] ; then
	report $MAIL_TMP >> $MAIL_IN
	rm $MAIL_TMP
fi

exit 0
