#!/bin/sh

PATH=/usr/local/isdn/bin:/usr/local/bin:/usr/bin:/bin
PATH=/usr/local/isdn/bin-$(uname -r):$PATH

set -e

cd /var/spool/fone
pid=$$

if test -n "$1" ; then ansage="$1" ; else ansage=ansage ; fi
if test -n "$2" ; then in="$2" ; else in=in ; fi
if test -n "$3" ; then work="$3" ; else work=NONE ; fi

if test -d $ansage ; then
   (
      	cd $ansage
		if test -s "LIST" ; then
			files=$(cat LIST)
		else
	  		file=1
			files=""
	  		while test -f $file ; do
				files="$files $file"
				file=$(expr $file + 1)
			done
		fi
		cat $files
   ) &
fi
if test -d $in ; then
   	cd $in
	file=1
	while test -f $pid.$file ; do
		file=$(expr $file + 1)
	done
	thefile=$in/$pid.$file
	cd ..
	cat > $thefile
	#tee $thefile | ( set +e; tosnd 3 mac.smurf.noris.de ; cat >/dev/null )
	if test -s $thefile ; then
		echo -e "Subject: Phone call from $PHONE\n\nPhone call: $thefile" | mail $(ls -l $thefile|awk '{print $3;}' )
	else
		rm -f $thefile
		exit 1
	fi
else
	exit 1
fi
( if test -x $work ; then nohup ./$work $in/$thefile >/dev/null 2>&1 ; fi ) &
exit 0
