#!/bin/sh

# User configuration.

SPLUS=/usr/bin/7plus

XFBB_BASEDIR=$DESTDIR/var/ax25/fbb	# Path of the XFBB dir on your system.
XFBB_FILESDIR=$XFBB_BASEDIR/fbbdos	# Path to the downloadable files.
MAILIN=$XFBB_BASEDIR/mail/mail.in

# End of user config stuff.

# Check if PG or server

get_info()
{
	read a a a CALL
	read -r P
}

banner()
{
	echo
	echo "The 7plus server for Linux and XFBB."
	echo "Version $VERSION made by Twan van der Meer the PE1NHL."
	echo "Adapted by F6FBB - 11/2000"
	echo
}

info()
{
	if [ $PG = 0 ] ; then
		echo $1 >> $MAILIN
	else
		echo $1
	fi
}

footer()
{
	if [ $PG = 0 ] ; then
		echo "/EX" >> $MAILIN
	fi
}

if [ $# = 1 ] ; then
	# Server ... Read the mail file
	PG=0
	get_info < $1
	echo "SP $CALL < 7PLSRV" >> $MAILIN
	echo "7plserv report" >> $MAILIN
	banner >> $MAILIN
else
	PG=1
	CALL=`echo $1 | cut -d- -f1 `
	P=$5
	banner
fi

PROGNAME=`echo $P | tr \\\\\\\\  /`

VERSION='V0.99'
BASENAME=`basename "$PROGNAME"`

# Some autor's indentification. 

TMP=/tmp/7p.$$

# Check if PROGNAME is not empty, otherwise print help message.
if [ "$PROGNAME" = '' ]
then
	info "To use the 7plserv, type:  7plserv <filename>"
	info "<filename> is the file to download, including the path."
elif [ -d "$XFBB_FILESDIR/$PROGNAME" ]               # Check if file is not a directory
then
	info 'You typed a directory name!'
elif [ -f "$XFBB_FILESDIR/$PROGNAME" ]               # Check if file exists
then
	# Welcome and activation

	info "The 7plus server is now processing $PROGNAME requested by $CALL"
	info

	# Copy file to /tmp dir to work on it.
 
 	mkdir -p $TMP
	cp $XFBB_FILESDIR/$PROGNAME $TMP
	cd $TMP

	# 7plus the programm.

	$SPLUS $BASENAME | grep successful && info 'Processing succesfull!'

	# Throw used program in the bin.

	rm $BASENAME

	# Check if there is only one *.7pl file, or multiple *.p?? files.
	# After checking copy the files in the mail.in and append it
	# to the FBB mail.in.

	echo "SP $CALL < 7PLSRV" > mail.in
	echo "7plus file: $PROGNAME" >> mail.in
	echo >> mail.in 
	echo "The 7plus server for Linux and XFBB." >> mail.in 
	echo "Version $VERSION made by Twan van der Meer the PE1NHL." >> mail.in 
	echo >> mail.in 

	if [ -f *.7pl ]
	then
		cat *.7pl >> mail.in
		rm *.7pl
		if [ "$?" = 0 ]
		then
			info "The 7plus mail will be ready for download in a few seconds."
		fi
	else
		cat *.p?? >> mail.in
		rm *.p??
		if [ "$?" = 0 ]
		then
			info "The 7plus mail will be ready for download in a few seconds."
		fi
	fi 
	echo '/EX' >> mail.in
	
	footer
	
	cat mail.in >> $MAILIN
	rm mail.in
	rm -r $TMP
else
	echo 'That file does not exist!'
fi

# Tell user 7plserv is finished.

exit 0

