#!/bin/sh

dirset=0
dir=""
pvs=pvs
#pvs=/home/tews/VFiasco/Pvs/tools/mypvs
timeout=240
test=0
tar=0
verbose=0

if [ -f Proofgen/proofgen -a -f everything.pvs ] ; then
    true
else
    echo start this script from the model directory as Proofgen/proofgen
    exit 1
fi

usage(){
    echo usage: Validate/validate [-h] [-timeout secs] [-pvs pvsprog] [-c] [dir]
    echo "  -h            displays this message"
    echo "  -timeout secs sets the timeout in seconds [default 240]"
    echo "  -pvs          selects the pvs to run"
    echo "  -test         runs test on selected theory"
    echo "  -tar          do not run pvs, untar proofs.tgz instead (for testing)"
    echo "  generate output in dir when given, otherwise in Proofgen"
    exit 1
}

while [ $# -gt 0 ] ; do
    case $1 in
	-h|--help)
	    usage;;
	-timeout)
	    if [ $# -lt 2 ] ; then
		usage
	    fi
	    timeout=$2; shift; shift;;
	-pvs)
	    if [ $# -lt 2 ] ; then
		usage
	    fi
	    pvs=$2; shift; shift;;
	-test)
	    test=1; shift;;
	-tar)
	    tar=1; shift;;
	-*)
	    usage;;
	*)
	    dirset=1; dir=$1; shift;;
    esac
done


date +'%nstarting on %a %d %b %Y %H:%M:%S %Z (%z)%n' > Proofgen/log

if [ $test = 1 ] ; then
    testopt="-f latex-test"
    proof_files='test-proof*'
    master=xx
else
    testopt=""
    proof_files='proof-*'
    master=all-proofs
fi

if [ $tar = 0 ] ; then

    $pvs -batch -v 2 -load Proofgen/latex-all-proofs.el -timeout $timeout \
	$testopt -f latex-all-proofs \
	| tee -a Proofgen/log 

    tar -czf proofs.tgz proof-*.tex all-proofs-include.tex

else
    tar -xzf proofs.tgz
fi


for f in $proof_files ; do
    if grep -q 'Q\.E\.D\.' $f ; then
	true
    else
	echo "latex proof problem in $f"
    fi
done


if false; then
    for f in $proof_files ; do
	cat - >> $f <<EOF

%%% Local Variables: %%%
%%% TeX-master: "$master.tex" %%%
%%% End: %%%
EOF
    done
fi


if true ; then

    broken_proofs="\
	linear\\\\_resolve\\\\_same\\\\_page\\\\_address \
       "

    if [ $verbose = 1 ] ; then

	broken_proofs="$broken_proofs \
		device\\\\_plain\\\\_unchanged\\\\_memory\\\\_invariant \
		device\\\\_plain\\\\_read\\\\_side\\\\_effect\\\\_unchanged \
		device\\\\_plain\\\\_write\\\\_side\\\\_effect\\\\_unchanged \
		device\\\\_plain\\\\_transformers\\\\_ok2 \
		linear\\\\_plain\\\\_transformers\\\\_ok \
		linear\\\\_plain\\\\_unchanged\\\\_memory\\\\_invariant \
		linear\\\\_unchanged\\\\_invariant \
	plain\\\\_memory\\\\_transformer\\\\_invariant\\\\_write\\\\_data \
		plain\\\\_mem\\\\_write\\\\_list\\\\_states \
    "
    fi

    for p in $broken_proofs ; do
	files=$(egrep -l "(Verbose|Terse) proof for .* $p}" proof-*tex)
	for f in $files ; do
	    echo erasing $f for $p
	    cat > $f <<EOF

The \LaTeX{} code for this proof is broken.

EOF
	done
    done
    
     # empty files because of parsing errors
	# 0741 max_linear_offset_val 
	# 1169 pde_valid_to_byte 
	# 1197 pte_valid_to_byte
	# 1256 plain_memory_inv_pred_write_data
	# 1691 while_terminates
	# 1692 hoare_while

    # untried because of the adt TCC problem
	# Pde_4m_TCC1
	# Pte_TCC1

    # unfinished spec10 proof-0356.tex


    others="\
	proof-0741.tex \
	proof-1169.tex \
	proof-1197.tex \
	proof-1256.tex \
	proof-1691.tex \
	proof-1692.tex \
    "

    if [ $verbose = 1 ] ; then

	# break exception during verbose proofs in
	    # 1484 search_not_found
	    # 1485 search_found

	others="$others \
		proof-1484.tex \
		proof-1485.tex \
        "
    fi

    for p in $others ; do
	echo erasing $p
	cat > $p <<EOF

The \LaTeX{} code for this proof is broken.

EOF
    done

fi

date +'%nfinished on %a %d %b %Y %H:%M:%S %Z (%z)%n' >> Proofgen/log

