#!/bin/sh

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

make

echo
echo "Sysop callsign(s) separated by a space."
echo "Ex : F6FBB F5NUF@F6FBB.FMLR.FRA.EU :"
echo "enter the list then <return> :"
read SYSOP

echo
echo "- 7plus sysops are :"
for CALL in $SYSOP ; do
	echo "  $CALL"
done
echo

#
# 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
#
##############################################


#
# THIS VARIABLE MUST BE INITALIZED !
# ==================================
#
# 7PLUS administrator(s) for report mails
# Replace SYSOP by the list of callsign separated by a space
#
SP_ADM="$SYSOP"

#
# THESE VARIABLES SHOULD BE OK
#

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

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

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

#
# Directory of the 7plus files (parts)
#
SP_DIR=$DATADIR/7plus/7pfbb_dir

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

#
# 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.00"

report()
{
	for ADM in \$SP_ADM ; do
		echo "#"
		echo "SP \$ADM"
		echo "7pFBB report"
	    echo
	    echo "7pfbb v\$FBBVERSION (f6fbb - 2000)"
	    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 7PLSRV server"
cp 7plserv $LIBDIR/server/7plserv
chmod +x $LIBDIR/server/7plserv
echo

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

exit 0
