#!/bin/bash
FULL="n"  # set to "y" to run compiler and upload results. Otherwise do fastest test.

# This set of tests is not meant to be used to verify the C code output on a file-by-file basis,
# it's purpose is to detect whether a change to the translator has accidentally affected areas
# other than the intended change.  A diff of the output from the previous run is shown at the
# end of the compilations.  The previous outputs will be deleted after a run so you should
# preserve the test.diff file if the specific differences need to be examined after a run.

# Define the log file location
LOG_FILE="TEST.log"

# Check if the script is already running inside the 'script' utility wrapper
if [ -z "$UNDER_SCRIPT" ]; then
    # Set the flag so the next execution skips this block, then launch 'script'
    export UNDER_SCRIPT=1
    
    # -q: quiet mode (suppresses the start/end messages of the script command)
    # -c: runs the specified command (re-executes this same script file with any original arguments)
    script -q -c "$0 $*" "$LOG_FILE"

    /home/gtoal/src/i2c/icode2ast/script2html < TEST.log > TEST.log.html
    ecce TEST.log.html TEST.log.html2 < /home/gtoal/src/i2c/icode2ast/TEST.ecc > /dev/null 2> /dev/null
    uniq < TEST.log.html2 > TEST.log.html3
    rsync -av TEST.log.html3 gtoal@gtoal.com:history.dcs.ed.ac.uk/archive/languages/imp77-tmp/gtoal/icode2ast/TEST.log.html
    if [ "$FULL" == "y" ] ; then
      rsync -av TEST.log test.tar gtoal@gtoal.com:history.dcs.ed.ac.uk/archive/languages/imp77-tmp/gtoal/icode2ast/
      rsync -av test/*.imp test/*.c.html test/*.imp.html test/*.i386 test/*.i686 test/*.arm7hf test/*.aarch64 test/*.x86_64 test/*.riscv64 test/*.mips test/*.powerpc test/*.sparc64 test/*.s390x test/*.m68k gtoal@gtoal.com:history.dcs.ed.ac.uk/archive/languages/imp77-tmp/gtoal/icode2ast/test/ > /dev/null 2> /dev/null
    else
      rsync -av test/*.imp test/*.c.html test/*.c test/*.imp.html gtoal@gtoal.com:history.dcs.ed.ac.uk/archive/languages/imp77-tmp/gtoal/icode2ast/test/
    fi
    exit 0
fi

DO() {
    banner $1
    cp test/$1.imp ./test-new/$1.imp
    rm -f test-new/$1.out.c
    ./icode2ast test-new/$1.imp > zzz.c
    if [ $? == 0 ]; then
        ./hashline --no-clang zzz.c | clang-format | ./hashline - > test-new/$1.out.c
        ecce test-new/$1.out.c -command "(f./* clang-format off */.s.. p  ( (v/ /e)0 (r\j,l)? )2?  p  ( (v-/ /e-)0 (l\j-r0,r)? )2? p  )0  m-0 (f./* clang-format on */.s.. p  ( (v/ /e)0 (r\j,l)? )2?  p  ( (v-/ /e-)0 (l\j-r0,r)? )2?  p  )0;%c" > /dev/null 2> /dev/null
        ecce test-new/$1.out.c -command "(f1/#line /2e0m,m)0;%c" > /dev/null 2> /dev/null
        grep -v "^#line" test-new/$1.out.c > test-new/$1.c ; ctohtml test-new/$1.c > test-new/$1.c.html
        /home/gtoal/src/imp77/hacks/BIN/imp2html $1.imp < test/$1.imp > ./test-new/$1.imp.html
        cp test-new/$1.c.html test/$1.c.html
        rm -f zzz.c
        imp77 -c --code test-new/$1.imp    # preserve .cod file
        mv  test-new/$1.cod test-new/$1.i386
        cc -fno-show-column -fno-diagnostics-color -fno-diagnostics-show-caret --static -c -o test-new/$1.o -I /home/gtoal/src/i2c/perms -I /home/gtoal/src/i2c/icode2ast/test-inc /home/gtoal/src/i2c/icode2ast/test-new/$1.out.c -L /home/gtoal/src/i2c/perms -lperms -lbacktrace |& head -21 # -Wa,-adhln  > /home/gtoal/src/i2c/icode2ast/test-new/$1.lst
        if [ "$FULL" == "y" ] ; then
          if [ $? == 0 ] ; then
              ( /home/gtoal/src/i2c/icode2ast/decode-arch.sh --i686 -O0 test-new/$1 > test-new/$1.i686 ) 2> /dev/null
              ( /home/gtoal/src/i2c/icode2ast/decode-arch.sh --arm7hf -O0 test-new/$1 > test-new/$1.arm7hf ) 2> /dev/null
              ( /home/gtoal/src/i2c/icode2ast/decode-arch.sh --aarch64 -O0 test-new/$1 > test-new/$1.aarch64 ) 2> /dev/null
              ( /home/gtoal/src/i2c/icode2ast/decode-arch.sh --x86_64 -O0 test-new/$1 > test-new/$1.x86_64 ) 2> /dev/null
              ( /home/gtoal/src/i2c/icode2ast/decode-arch.sh --riscv64 -O0 test-new/$1 > test-new/$1.riscv64 ) 2> /dev/null
              ( /home/gtoal/src/i2c/icode2ast/decode-arch.sh --mips -O0 test-new/$1 > test-new/$1.mips ) 2> /dev/null
              ( /home/gtoal/src/i2c/icode2ast/decode-arch.sh --powerpc -O0 test-new/$1 > test-new/$1.powerpc ) 2> /dev/null
              ( /home/gtoal/src/i2c/icode2ast/decode-arch.sh --sparc64 -O0 test-new/$1 > test-new/$1.sparc64 ) 2> /dev/null
              ( /home/gtoal/src/i2c/icode2ast/decode-arch.sh --s390x -O0 test-new/$1 > test-new/$1.s390x ) 2> /dev/null
              ( /home/gtoal/src/i2c/icode2ast/decode-arch.sh --m68k -O0 test-new/$1 > test-new/$1.m68k ) 2> /dev/null
              # possible additions: pdp11 vax msp430 avr or1k
          else
              echo "C: <a href=\"test/$1.c.html\">$1.c.html</a>"
          fi
        else
          ( /home/gtoal/src/i2c/icode2ast/decode-arch.sh --i686 -O0 test-new/$1 > test-new/$1.i686 ) 2> /dev/null
        fi
        echo "IMP: <a href=\"test/$1.imp.html\">$1.imp.html</a>C: <a href=\"test/$1.c.html\">$1.c.html</a>i686: <a href=\"test/$1.i686\">$1.i686</a>i386: <a href=\"test/$1.i386\">$1.i386</a>arm7hf: <a href=\"test/$1.arm7hf\">$1.arm7hf</a>aarch64: <a href=\"test/$1.aarch64\">$1.aarch64</a>x86_64: <a href=\"test/$1.x86_64\">$1.x86_64</a>riscv64: <a href=\"test/$1.riscv64\">$1.riscv64</a>mips: <a href=\"test/$1.mips\">$1.mips</a>powerpc: <a href=\"test/$1.powerpc\">$1.powerpc</a>sparc64: <a href=\"test/$1.sparc64\">$1.sparc64</a>s390x: <a href=\"test/$1.s390x\">$1.s390x</a>m68k: <a href=\"test/$1.m68k\">$1.m68k</a>"
    else
      echo "$1.imp" >> test-new/failed
    fi
}

mkdir -p test-new
echo > test-new/failed
cp test-inc/*.inc test-new/

# test/
DO test3
DO test4
DO test5
DO test8
DO test10
DO test11
DO test12
DO test13
DO test14
DO test15
DO test16
DO test17
DO test18
DO test19
DO test22
DO test23
DO test24
DO test26
DO test28
DO test29
DO test32
DO test33
DO test34
DO test35
DO test36
DO test37
DO test38
DO test39
DO test40
DO test41
DO test42
DO test43
DO test44
DO test45
DO test46
DO test47
DO test48
DO test49
DO test50
DO test51
DO test52
DO test53
DO test54
DO test55
DO test56
DO test57
DO test58
DO test60
DO test61
DO test62
DO test63
DO test64
DO test65
DO test66
DO test67
DO test68
DO test72
DO test73
DO test75
DO test79
DO test80
DO test81
DO test83
DO test85
DO test89a
DO test91
DO test92
DO test93
DO test94
DO test95
DO test96
DO test97
DO test98
DO test99
DO test101
DO test102
DO test107
DO test110
DO test112
DO test113b
DO test116
DO test117
DO test119
DO test121
DO test125
DO test126
DO test129
DO test130
DO test131
DO test132
DO test133
DO test134
DO test136
DO test138
DO test139
DO test140
DO test141
DO test143
DO test145
DO test146
DO test147
DO test149
DO test153
DO test155
DO test156
DO test157
DO test159
DO test160
DO test163
DO test164a
DO test164
DO test165
DO test166
DO test167
DO test169
DO test172
DO test175
DO test177
DO test178
DO test179
DO test180
DO test182
DO test185
DO test186
DO test187
DO test188
DO test189
DO test190
DO test191
DO test192
DO test194
DO test195
DO test196
DO test197
DO test198
DO test199
DO test200
DO test201
DO test202
DO test203
DO test204
DO test205
DO test208
DO test209
DO test210
DO test211
DO test212
DO test213
DO test214
DO test215
DO test217
DO test218
DO test219
DO test220
DO test221
DO test222
DO test223
DO test224
DO test225
DO test226
DO test227
DO test228
DO 1
DO 2
DO 3
DO 4
DO 5
DO 7
DO 8
DO 9
DO 10
DO 11
DO 12
DO 13
DO 14
DO 15
DO 16
DO 17
DO 18
DO 19
DO 20
DO 21
DO 22
DO 24
DO 26
DO 27
DO 28
DO 29
DO 30
DO 31
DO 32
DO 33
DO 34
DO 35
DO 36
DO 37
DO 38
DO 39
DO 40
DO 42
DO 43
DO 44
DO 45
DO 46
DO 47
DO 48
DO 49
DO 50
DO 51
DO 52
DO 53
DO 54
DO 55
DO 58
DO 59
DO 60
DO 61
DO 62
DO 63
DO 64
DO 65
DO 67
DO 68
DO 69
DO 198
DO ct0
DO ct6
DO ct11
DO ct15
DO vs0
DO vs1
DO vs2
DO vs3
DO vs4
DO vs5
DO vs7
DO vs9
DO vs10
DO vs11
DO it0
DO it1a
DO it1
DO it2
DO it34
DO it3
DO it4
DO it5
DO it7
DO it9
DO rt0
DO rt1
DO rt2
DO rt3
DO rt4
DO rt5
DO rt7
DO rt9
DO rt10
DO rt11
DO rt12a
DO rt12
DO rt14a
DO rt14
DO rt15
DO rt21
DO rt24
DO rt28
DO rt31
DO rt33
DO rt34
DO rt35
DO rt36
DO rt38
DO rt40
DO rt45
DO rt47
DO rt49
DO rt4a
DO rt50
DO rt52
DO rt53
DO rt54
DO rt55
DO rt56
DO rt57
DO rt58a
DO rt58
DO rt61
DO rt63
DO rt65
DO rt66
DO rt67
DO rt69
DO gtoal001
DO gtoal002
DO gtoal003
DO gtoal004
DO abd
DO ACKER
DO adecl
DO alias
DO allimpc1-77-edited
DO allimpc1-77
DO array1dspec
DO array2daccess
DO arrayaccess
DO arraydims
DO array
DO arrayparam
DO assint
DO basicrec
DO beginreturn
DO bernstein
DO bignum
DO bigtest2
DO bigtest
DO brackets
DO callproc
DO cat
DO c-features
DO chess9
DO chimps
DO concat1
DO concat2
DO cond2s
DO constarrayinit
DO continuetest
DO cordic
DO cycles
DO cyctags
DO debug
DO decode-77
DO defsw
DO divides
DO d
DO draft4
DO dscond
DO dsided
DO dynamic
DO equals_nl
DO exitcont
DO exittest
DO expr2
DO ext2darray
DO extbegin
DO extrt
DO extvar
DO fbench
DO fndef
DO forever
DO hanoi
DO iftags
DO imp77test
DO initown
DO initrec
DO initvars
DO inner
DO intmath
DO intname
DO intnamera
DO i-test
DO label
DO labtest
DO lc
DO leibnitz
DO length
DO lostdecl
DO lvaluemap
DO map
DO mapresult
DO minimal
DO minmap
DO monitor
DO monop
DO namename
DO namename2
DO nameparam2
DO nameparam
DO nametest
DO negbug
DO negshift
DO nestcall
DO nodef
DO noswitch
DO nrimp10s-77
DO openfail
DO ownarrayinit
DO ownarray
DO own
DO paper
DO pass1
DO pass1-pe
DO pass1v84-noprims
DO pass2-pe
DO pass3-pe
DO permtest
DO prec
DO recbug
DO recfmdecl
DO recmap
DO recordparam
DO rectest
DO repinit
DO res
DO restests
DO routest
DO rttest
DO scalarparams
DO scopes
DO shiftadd
DO shortestpath
DO shortidx
DO signal
DO simplecall
DO simpleproc
DO soaps-77
DO static
DO strassign
DO strcmp
DO strlen
DO strnameparam
DO strparam
DO swtest2
DO test3add
DO testcall
DO testconc
DO testfor
DO testgoto
DO testmin
DO testmod
DO test
DO testparams
DO testperm77
DO testprocparam
DO testresret
DO testuntil
DO threearrays
DO tictactoe
DO TPK
DO unass
DO varrec
DO vecce

# regression-compile/
DO addsuba
DO array2d
DO asems412
DO asm6809
DO badfor
DO badsignal
DO bcpls
DO blocklab
DO casterr
DO check
DO chimp
DO comp412
DO compare
DO EVENTS
DO gammon
DO gens400
DO gpm
DO hal70
DO hal7502
DO haunt
DO imp22g-77
DO imp918s
DO labscope
DO nameparmass
DO nminimo
DO old-bridge77
DO precedence-rainer
DO random
DO recordmap
DO shiftprecbug
DO skimp77
DO skimpais
DO skimpd
DO syscall
DO unwinding
DO vax-pass1
DO vax-pass2
DO vax-pass3
DO wumpus

# tests/
DO arraysnd
DO extmain
DO formatoutput2
DO formatoutput3
DO formatoutput
DO nameass
DO owninit
DO strlentest

# new-tests/
DO add
DO arrsinrec
DO bigrecspecs
DO calls
DO design-flaw
DO extdata
DO multiorfm
DO nestedfns
DO nestparams
DO precdiv
DO recordformatspecevil
DO recordformatspec
DO recprob2
DO recprob3
DO recprob
DO recscope
DO recscope-rebuilt
DO recselfref
DO recspecs
DO strnam
DO testprocparamspecbug
DO unbalanced
DO write-test
DO zeroinit

# active-tests/
DO 1Darray
DO 3Darray
DO access
DO aliasbody
DO aliascall
DO aliastest
DO arrayfield
DO blocktest
DO emptyparamtest
##DO expr0
DO fail2
DO fail3
DO fail5
DO fail6
DO failtest
DO fncall
DO fptest
DO iftest
DO initreptest
DO inittest
DO jump
DO labstest
DO mapcalltest
DO multiplebounds
DO onedaccess
DO owninittest
DO ownrectest
DO paramval
DO ptrcomp
DO recarraytest
DO recfield
DO recfnfield
DO recmapfield
DO recparam
DO recvartest
DO repro
DO san
DO sig1test
DO sig2test
DO sig3test
DO subrec2
DO subrecs
DO switchtest
DO testarr0
DO testarr1
DO testarr2d
DO testarr3d
DO testarr3
DO testdscond
DO threedaccess

# .
DO constlenbug
DO imprtl-io
DO rec2
DO rttest2
DO twonamepars
DO arr2
DO swneg2
DO swneg
DO swneg0
DO neglb
DO testnegfor
DO dsided2
DO ptrbug1
DO ptrbug2
DO ptrbug3
DO ptrbug4
DO ptrbug5
DO lostfields
DO namepar
DO brokenarray
DO 1Darray2
DO wreck01
DO dimtest
DO recnameparam
DO recarrfield
DO rtprocfnpredparam
DO rtprocfnpredparam2
DO mixedsize
DO longintdec
DO recarrf
DO forinc
DO stackfm
DO recarraddr
DO recassconst
DO fc0001
DO fc0002
DO fc0003
DO fc0004
DO fc0005
DO fc0006
DO array2ass
DO strconcat
DO strlentrap
DO strlentest
DO dstypes
DO jams
DO test37b
DO sorttest
DO qsort
DO cnstlreal
DO nullrecp
DO strstuff
DO m6502
DO ercc-imp-lib
DO major
DO arrayname
DO arrayparamerr
DO array_size_wrong
DO ass11
DO bug30c
DO bug30d
DO bug30e
DO conformant
DO declerr
DO iname
DO LIB
DO imp11
DO mactest
DO missingrecord
DO nilrecord
DO semi2
DO SIM
DO sqrt2
DO word
DO collatz
DO collatz-flat
DO t1
DO t2
DO t3
DO t4
DO t5
DO t6
DO t7
DO t8
DO t9
DO t10
DO flatten
DO dhry_1
DO dhry_2
DO ack
DO crc32
DO hello
DO layout
DO precedence
DO sktest
DO vaxcode-77
DO uiocp17
DO vaxsim2
DO forall
DO option
DO mutrec
DO arraynamebug
DO litconst

#
#  Currently these are the tests that cause an icode2ast failure:
#    abd.imp chess9.imp nrimp10s-77.imp pass1.imp pass1-pe.imp pass1v84-noprims.imp
#    pass2-pe.imp pass3-pe.imp vecce.imp asems412.imp imp22g-77.imp imp918s.imp
#    nminimo.imp vax-pass1.imp vax-pass2.imp vax-pass3.imp imprtl-io.imp
#
#  They are all large programs and I haven't been able to replicate the failures
#  with short extracts.  I suspect some large array is being written past, however
#  I am running with checks enabled that ought to catch simple bound overshoots.
#

diff -r test-new test > test.diff
rm -rf test
mv test-new test
cat test.diff
echo Failed:
echo `cat test/failed`
