#!/bin/sh

LIBDIR=$DESTDIR/usr/lib/fbb
FBBCONF=$DESTDIR/etc/ax25/fbb.conf

make


#
# Create maintenance/50_7pfbb script
#

SCRIPT=$LIBDIR/script/maintenance/50_7pfbb

echo "- Creating script $SCRIPT ..."

cat <<EOF > $SCRIPT
#!/bin/sh

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

PREFIX=\$DESTDIR/usr
SBIN=\$PREFIX/sbin
FBBCONF=$FBBCONF

#
# 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 $FBBCONF 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
EOF
echo

chmod +x $SCRIPT

echo "- Copying m_filter"
if [ -x $LIBDIR/filter/m_filter ] ; then
	echo "  ! m_filter already exists in $LIBDIR/filter directory"
	echo -n "  ! overwrite (y/n) ? "
	read rep
	if [ "$rep" = "y" -o "$rep" = "Y" ] ; then
		echo "  * m_filter saved as $LIBDIR/filter/m_filter.old"
		cp $LIBDIR/filter/m_filter $LIBDIR/filter/m_filter.old
		cp m_filter $LIBDIR/filter/m_filter
		chmod +x $LIBDIR/filter/m_filter
	fi
else
	cp m_filter $LIBDIR/filter/m_filter
	chmod +x $LIBDIR/filter/m_filter
fi
echo

echo "- Copying 7PSERV server"
cp 7pserv $LIBDIR/server/7pserv
chmod +x $LIBDIR/server/7pserv
echo

echo "- Checking fbb.conf :"
if ! grep 7pserv $FBBCONF ; then
	echo "  * Adding 7PSERV to fbb.conf"
	echo "# 7PSERV 7+ server" >> $FBBCONF
	echo "server = 7PSERV 7pserv 7plus server" >> $FBBCONF
fi
echo

echo "- Creating symlink for PG usage"
ln -sf $LIBDIR/server/7pserv $LIBDIR/pg/7pserv
echo

exit 0
