#! /bin/sh
# auto.sh:  Tests for "admin"'s detection of binary files.

# Import common functions & definitions.
. ../common/test-common
. ../common/real-thing
. ../common/config-data

if $binary_support
then
    true
else
    echo "Skipping these tests -- no binary file support."
    exit 0
fi 



good() {
    if $expect_fail ; then
	echo UNEXPECTED PASS
    else
	echo passed
    fi
}

bad() {
    if $expect_fail ; then
	echo failed -- but we expected that.
    else
	fail "$@"
    fi
}


# test_bin: 
# Usage:   test_bin LABEL <contents>
#
# create a flie containing the specified argument and check
# that it is encoded as a binary file.  
test_bin() {
label=$1
echo_nonl ${label}...
shift

infile=$1
shift


if ${use_stdin}
then
    cmd="cat ${infile} | ${vg_admin} -i ${adminflags} $s"
else
    cmd="${vg_admin} -i${infile} ${adminflags} $s"
fi

rm -f $s
remove errmsg
if ( eval "${cmd}" ) >/dev/null 2>errmsg
then
    if ( ${prs} -d:FL: $s 2>/dev/null; echo foo ) | grep encoded >/dev/null 2>&1
    then
	remove $g errmsg
	if ${get} -p ${s} 2>errmsg >${g}
	then
	    remove errmsg
	    if diff ${infile} ${g} >diffs
	    then
		remove diffs
		good
	    else
		# We failed to get the data back correctly from the 
		# SCCS file.
		bad "$label data lost (see file 'diffs') in ${cmd}".
	    fi
	else
	    bad $label "${get} -p $s failed: `cat errmsg`"
	fi
    else
	bad $label input did not produce an encoded s-file.
    fi
else
    cat errmsg ; remove errmsg
    bad $label ${admin} returned exit status $?.
fi
rm -f $s
}

# test_ascii: 
#
# As for test_bin, but the resulting SCCS file must NOT be encoded.
# 
test_ascii() {
label=$1
echo_nonl ${label}...
shift

rm -f infile $s
echo_nonl "$@" > infile
if ${vg_admin} -iinfile ${adminflags} $s >/dev/null 2>&1
then
    if ( ${prt} -f $s 2>/dev/null ; echo foo ) | grep encoded >/dev/null 2>&1
    then
        bad $label input produced an encoded s-file and should not have.
    else
        good
    fi
else
    bad $label ${vg_admin} returned exit status $?.
fi
rm -f infile $s
}


g=testfile
s=s.$g
z=z.$g
x=x.$g
p=p.$g
files="$s $z $x $p"

remove $files long-text-file infile ctrl-A-file no-newline ctrl-A-end
remove command.log log  base [sxzp].$g errmsg $g

expect_fail=false
adminflags=""

remove long-text-file
../../testutils/yammer 1000 "this is a text file" > long-text-file
#../../testutils/yes "this is a text file" | nl | head -1000 >long-text-file
if test -s long-text-file
then
    true 
else
    miscarry could not create long-text-file.
fi

remove no-newline 
echo_nonl "no newline here" > no-newline
remove ctrl-A-file
echo_nonl \
    "the next line of this file starts with " \
    "a ctrl-A,\n\001Which SCCS does not like" > ctrl-A-file
remove ctrl-A-end
echo_nonl \
    "This file ends with a ctrl-A.\n\001" > ctrl-A-end


# Make sure that we correctly decide to encode the files, 
# and that we don't lose data.

# First make sure that forcing binary mode works.
use_stdin=false
adminflags=-b
test_bin s1 long-text-file
adminflags=


# Now try various nearly-text files.
if $TESTING_SCCS_V6
then
	test_ascii s2 ctrl-A-file
else
	test_bin s2 ctrl-A-file
fi

# Create a file which we only discover needs encoding after
# we have read loads of it.
remove infile ; cat long-text-file ctrl-A-file > infile
if $TESTING_SCCS_V6
then
	test_ascii s3 infile
else
	test_bin s3 infile
fi

# Another long file but binary because it lacks a newline at the end.
if $TESTING_SCCS_V6
then
	test_ascii s4 ctrl-A-end
else
	test_bin s4 ctrl-A-end
fi

use_stdin=true


## Same tests as before, but with the "-i" file on a pipe.



if $TESTING_CSSC
then
    # Do the tests that SCCS does not pass.
    use_stdin=false
    
	if $TESTING_SCCS_V6
	then
	    test_ascii s5 no-newline	# Real SCCS fails this one.
	else
	    test_bin s5 no-newline	# Real SCCS fails this one.
	fi
    
    remove infile ; cat long-text-file no-newline > infile
	if $TESTING_SCCS_V6
	then
	    test_ascii s6 infile
	else
	    test_bin s6 infile
	fi

    use_stdin=true
	if $TESTING_SCCS_V6
	then
	    test_ascii i1 ctrl-A-file
	    test_ascii i2 infile
	else
	    test_bin i1 ctrl-A-file
	    test_bin i2 infile
	fi
    remove infile ; cat long-text-file ctrl-A-file > infile
	if $TESTING_SCCS_V6
	then
	    test_ascii i3 ctrl-A-end
	    test_ascii i4 no-newline
	else
	    test_bin i3 ctrl-A-end
	    test_bin i4 no-newline
	fi

    remove infile ; cat long-text-file no-newline > infile
	if $TESTING_SCCS_V6
	then
	    test_ascii i5 infile
	else
	    test_bin i5 infile
	fi
else
    echo "Not running tests on CSSC; Some tests have been been omitted"
fi

remove $files long-text-file infile ctrl-A-file no-newline ctrl-A-end
remove command.log log  base errmsg $g
success
