#!/bin/sh

# Test FL routines
#
# $Id: tFLroutines 1.15 1998/07/06 libtsp-v3r0 $

if [ "$USER" = "" ]; then
  USER=$USERNAME
fi

echo ""
echo "========== tFLbackup"
fname=temporaryfile
touch $fname
tFLbackup $fname	2>&1 
ls ${fname}*
rm -f $fname ${fname}~

fname=temporaryfile.xxxx
fnameb=${fname}~
touch $fname
touch $fnameb
chmod u-w $fnameb
tFLbackup $fname	2>&1	# move on top of existing readonly file
ls ${fname}*
rm -f $fname $fnameb

fname=tempf
fnameb=${fname}~
touch $fname
ln -s $fname $fnameb
tFLbackup $fname	2>&1	# backup link (should kill link)
ls ${fname}*
rm -f $fname $fnameb

tFLbackup ./		2>&1	# directory
if [ -f /bin/sh ]; then
  tFLbackup /bin/sh	2>&1	# no permission
fi
tFLbackup /dev/tty		# device
tFLbackup XxXxYx		# file does not exist

echo ""
echo "========== tFLdefName"
tFLdefName abc/def xxx/yyy
tFLdefName def xxx/yyy
tFLdefName abc/ xxx/yyy
tFLdefName abc /

echo ""
echo "========== tFLexist"
tFLexist XxXxXx		# file does not exist
tFLexist /dev		# directory
tFLexist /dev/tty	# device
tFLexist Makefile
fname=tempf
fnamel=${fname}l
fnamell=${fnamel}l
touch $fname
ln -s $fname $fnamel
ln -s $fnamel $fnamell
tFLexist $fname
tFLexist $fnamel		# link to regular file
tFLexist $fnamell
rm -f $fname $fnamel $fnamell

echo ""
echo "========== tFLexpHome"
tFLexpHome ""
tFLexpHome abc/def
tFLexpHome ~junk
tFLexpHome ~junk/abc/def
tFLexpHome ~root
tFLexpHome ~root/abc/def

echo ""
echo "========== tFLfReadData"
cat << EoF > test.dat
! data values
0.5
1.0 1.5 ! xxxx

2.0
EoF
tFLfReadData test.dat		# test ignore comments and blank lines
rm -rf test.dat

cat << EoF > test.dat
! data values
0.5
1.0 1.5 xxxx

2.0
EoF
tFLfReadData test.dat 2>&1	# test bad data value
rm -rf test.dat

cat << EoF > test.dat
0.5 1.0 1.5 2.0 2.5 3.0
EoF
tFLfReadData test.dat 2>&1	# test too many data items
rm -rf test.dat

cat << EoF > test.dat
0.5 1.0 1.5 2.0 2.5
xx
EoF
tFLfReadData test.dat 2>&1	# test too many lines
rm -rf test.dat

echo ""
echo "========== tFLfWriteData"
tFLfWriteData test.dat 1.1 2.2 3.3 4.4 5.5
cat test.dat
rm -rf test.dat

echo ""
echo "========== tFLgetRec"

cat > test.dat << EoF
abc !xxxx
abd\\
def\\
abc #xxxx
EoF

tFLgetRec test.dat "!#" "\\" 2
echo ""
tFLgetRec test.dat "!#" "\\" 1
echo ""
tFLgetRec test.dat "!#" "\\" 0
rm -f test.dat

# Create a file with no newline for the last data item
echo "563" | dd ibs=1 count=3 2>/dev/null 1>fstdout
(cat <<EoF; cat fstdout) > test.dat
356
 571.3 
 	-444.2 
EoF
rm -f fstdout

tFLgetRec test.dat "" "" 0
rm -f test.dat

echo ""
echo "========== tFLfullName"
tFLfullName ""
tFLfullName /xx
tFLfullName /xx/yy
tFLfullName ~kabal/edit/../xx

echo ""
echo "========== tFLhomeDir"
tFLhomeDir junk
tFLhomeDir root

echo ""
echo "========== tFLpathList"
PLIST=".:$PATH"
export PLIST
tFLpathList xxx/yyy ""
tFLpathList xxx/yyy "$PLIST"
tFLpathList ls ""
tFLpathList ls "xxx:zzz"
tFLpathList ls '$PLIST'

echo ""
echo "========== tFLreadLine"
tFLreadLine "Prompt > " << EoF
line 1
line 2
EoF

echo ""
echo "========== tFLseekterm"
echo "xxx"  | tFLseekterm "stdin"	# pipe
(tFLseekterm "stdout" | cat > /dev/null)	2>&1
tFLseekterm "stdin"			# terminal
tFLseekterm "/dev/tty"
tFLseekterm "stdin" < tFLseekterm.c	# file
(tFLseekterm "stdout" > xxx.tmp)		2>&1
rm -f xxx.tmp
tFLseekterm "stdin" < /dev/null
(tFLseekterm "stdout" > /dev/null)		2>&1

echo ""
echo "========== tFLxxxDate (expect time zone differences)"
# The test files were created on a Unix system
# with times of 12:00 UTC in Feb and 12:00 UTC in Aug
# They were then transferred (using tar/WinZip) to a Windows system (EDT)
#   - CygWin32 gives the incorrect time (during local summer time)
tFLxxxDate DS-1990-02-02-1200UTC
tFLxxxDate DS-1990-08-08-1200UTC

echo ""
echo "========== tFLxxxName"
tFLxxxName ""
tFLxxxName xx
tFLxxxName ./xx
tFLxxxName xx/yy/
tFLxxxName /xx/yy/
tFLxxxName xx/yy.ex
tFLxxxName xx/yy/zz
tFLxxxName /
tFLxxxName /xx
tFLxxxName /xx/yy.ex
tFLxxxName /xx/yy/zz

echo ""
echo "========== tFLjoinNames"
tFLjoinNames "" xxx/yyy
tFLjoinNames abc/def ""
tFLjoinNames abc/def xxx/yyy
tFLjoinNames abc/def/ xxx/yyy
tFLjoinNames / xxx/yyy
tFLjoinNames abc/def /xxx/yyy
tFLjoinNames abc/def/ /xxx/yyy

echo ""
echo "========== tFLexpHome (expect system dependencies)"
tFLexpHome ~
tFLexpHome ~${USER}
tFLexpHome ~${USER}/abc/def
tFLexpHome ~/abc/def

echo ""
tFLdefName abc/def xxx/yyy
echo "========== tFLfullName (expect system dependencies)"
tFLfullName xx
tFLfullName xx/yy
tFLfullName ~${USER}/edit/..//xx	# should leave ~ part intact

echo ""
echo "========== tFLhomeDir (expect system dependencies)"
tFLhomeDir ""
tFLhomeDir root
tFLhomeDir ${USER}

echo ""
echo "========== tFLpathList (expect system dependencies)"
tFLpathList ~/.emacs ""
tFLpathList .emacs '~'
tFLpathList ~${USER}/.emacs ""
tFLpathList .emacs ~${USER}
