#!/bin/sh

# 7pserv version 2.1 (pa3aes)
# 7pserv version 2.1.1 (pe1icq)
# This script works as PG or SERVER in Xfbb
#
# This 7plus server script is based on the version of PE1NHL adapted by F6FBB
# which was distributed with the 7pfbb2_00 package.
# See the "README.7pserv" file for changes and information ...
#
# Adapted by PA3AES@PI8WFL.#NH1.NLD.EU (or pa3aes@amsat.org) august 2001
# fbbgetconf  adaptation by PE1ICQ@PI8ZAA.#NBO.NLD.EU (or pe1icq@amsat.org) august 2001

# User configuration:
SPLUS=/usr/bin/7plus			# 7plus program location and name
SIZE=36000				# size of 7plus parts (max 36000 bytes)
MAXSIZE=1000000				# max size of file to be 7plus-served
# End of user configuration part.


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

BBS=`$SBIN/fbbgetconf callsign | cut -d. -f1`		# The callsign of your bbs (sender of 7plus parts)
XFBB_FILESDIR=`$SBIN/fbbgetconf fbbdos | tr -d *,`	# Path to the downloadable files.
MAILIN=`$SBIN/fbbgetconf import`	# Path to mail.in file of FBB

VERSION='V2.1.1'
TMP=/tmp/7p.$$

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

banner()
{
	echo
	echo "XFBB 7plus server Version $VERSION "
	echo "Adapted by F6FBB & PA3AES - 2000/2001"
	echo
}

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

header()
{
	if [ $PG = 0 ] ; then
		echo "SP $CALL < $BBS" >> $MAILIN
		echo "$BBS - 7PSERV information message"
	fi
}

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

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


# start of main program

if [ $# = 1 ] ; then
	PG=0					# It's called as Server ... Read the mail file
	SERVER_FILE=$1				# save filename for deleting when exiting
	get_info < $1				# read input of file
 else
	PG=1					# It's Called as PG
	CALL=`echo $1 | cut -d- -f1 `
	P=$5
	banner
fi

 PROGNAME=`echo $P | tr \\\\\\\\  / | tr -s .`  # translate backslash to slash and 
                                                # translate multiple ".." to one (security !!)
 BASENAME=`basename "$PROGNAME"`

                                                # Check if PROGNAME is not empty, otherwise print help message.
if [ "$PROGNAME" = '' ]
 then
	info "To use the 7pserv, type:  7pserv <filename>"
	info "<filename> is the file to download, including the path."

elif [ -d "$XFBB_FILESDIR/$PROGNAME" ]          # Check if file is not a directory
 then						# data_out for server, info for pg output
	header
	data_out "7PSERV error !!! "
	data_out
	data_out "The file you requested ($PROGNAME)"
	data_out "is a directory-name instead of a file name !"
	info "You typed a directory name ($PROGNAME) instead of a file name !"
	footer
	
elif [ -f "$XFBB_FILESDIR/$PROGNAME" ]          # Check if file exists
 then						# File exists so process it !

     if [ `ls -l $XFBB_FILESDIR/$PROGNAME | awk '{print$5}'` -ge $MAXSIZE ] # Check if file > maxlength
          then  							    # file is too big, announce this and quit...
		header
		data_out "7PSERV error !!! "
		data_out
		data_out "The file you requested ($PROGNAME)"
		data_out "is too large for 7plus download (max-size= $MAXSIZE bytes ) !"
		data_out "Use Bget of Yget to download directly on $BBS."
		info "The file is larger than $MAXSIZE bytes, 7plus not allowed, try Bget or Yget !"
		footer
	        
     else  					# file is not too big, continue

	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 and use maximum part-size ($SIZE)
	$SPLUS "$BASENAME" -sb $SIZE | grep successful && info 'Processing succesfull!'


						# send 7plus parts to mail.in file
	for FILE in *.7pl *.p??
	    do
		if [ -f $FILE ]
		    then
    		    echo "SP $CALL < $BBS" >> mail.in
    		    echo "7plus file: $FILE " >> mail.in
    		    echo "From : 7PSERV @ $BBS " >> mail.in
		    echo "File : $PROGNAME - Part: $FILE" >> mail.in
		    echo "7PSERV for XFBB (C) 2000-2001 F6FBB, version $VERSION by PA3AES" >> mail.in
		    echo >> mail.in
		    echo >> mail.in
		    cat $FILE >> mail.in
    		    echo >> mail.in
    		    echo "/EX" >> mail.in
    		    if [ "$?" = 0 ]
			then
    			    info "File $FILE is ready for download in a few seconds."
		    fi
		fi
	    done
	
     cat mail.in >> $MAILIN
     fi

 else
    if [ $PG = 0 ] ; then
	header
	data_out "7PSERV Error !!! "
	data_out
	data_out "File $PROGNAME does not exist !"
	data_out "Did you enter filename correct ? (small/CAPITAL letters)"
	footer
    else
	echo "$PROGNAME does not exist (check small/CAPITAL letters !)"
    fi

fi



# remove the input file when called as server
if [ $PG = 0 ] ; then
    rm $SERVER_FILE
fi

# remove temp directory :
rm -f -r $TMP

exit 0

