#!/bin/awk
# Checks that *ref in .svx files link to valid surveyscans folders
# Assumes that svx-refs has been generated.
# usage:
# cd /loser/
# ./check-refs.sh 
#
# 2020-04-05 Philip Sargent
#
# Does not check whether move than one svx file points at the same wallet (might be real)
# Does not check whether more than one wallet json points to the same svx file (an error)
function ltrim(s) { sub(/^[ \t\r\n]+/, "", s); return s } # remove leading whitespace
function rtrim(s) { sub(/[ \t\r\n]+$/, "", s); return s } # remove trailing whitespace
function trim(s)  { return rtrim(ltrim(s)); }
BEGIN {
    walltag = "[1-2][0-9][0-9][0-9]#X?[0-9][0-9]"
    ss ="/home/expo/expofiles/surveyscans/"
    ssurl ="expofiles/surveyscans/"
    svxurl ="survexfile/"
    }
$2 ~ walltag {
    svxfile = $1
    urlsvxfile = svxurl svxfile
    year = substr($2,0,4)
    wallet = $2
    walldir = year "/" wallet
    path = ss year "/" wallet
    json = path "/contents.json"
    urljson = ssurl year "/"  year "%23" substr(wallet,6,length(wallet))  "/contents.json"
    #printf("%s :: ", wallet)
    included =""
    if ($NF ~ "INCLUDE") {
        included = "(*includes " $(NF-1) " )"
    }
           
    if (system("test -d " path)!=0) {
        printf("Missing WALLET %s <a href=\"%s\">%s</a>\n", walldir, urlsvxfile, svxfile) > "svx-refs.tmp"
        print "mkdir ", path > "svx-refs.tmp"
    } else if (system("test -f " json)!=0) {
        print "Missing JSON  ", walldir, $1 > "svx-refs.tmp"
        print "cp -p " ss "template.json ", json > "svx-refs.tmp"
    } else if (system("test -s " json)!=0) {
        print "Zerolength JSON  ", walldir, $1 > "svx-refs.tmp"
        # copy an empty template  into it
        print "cp -p " ss "template.json ", json > "svx-refs.tmp"
    } else if (system("grep \"survex file\" " json ">> grep.txt")!=0 ) { 
            print "CORRUPT json  - missing survex file line",walldir, $1 > "svx-refs.tmp"
    } else {
        # Now scan the content.json file and look for the reference to the svx file
        i=0
        jline[0]=""
        for (k in jline ) delete jline[k]
        while (getline line <json > 0) {
            n=gsub(/\"/,"",line) # " syntax colouring bad in VS code for awk
            i += 1
            jline[i]=line
            result="working"
            if (line ~ /survex file/) { 
                result="survexline"
                n = split(line,arr,":")
                jsonsvx = trim(arr[n])
                n=gsub(/,$/,"",jsonsvx) # if it is the last field then no comma
                urljsonsvx = svxurl jsonsvx
                #printf("[%s] {%s} ",jsonsvx, urljsonsvx)
                if (jsonsvx == "") {
                    print "UNSET json  ", walldir, $1 > "svx-refs.tmp"
                    printf("sed -i '/survex file/ s;\"\";\"%s\";' %s\n",svxfile, json) > "svx-refs.tmp"
                    printf("%s :: ", wallet)
                    printf("UNSET - json field blank\nsvx :%s:\n",svxfile)
                    result="unset"
                    #break # behaves like next not break
                } else if (jsonsvx  == svxfile ) {
                    printf("<a href=\"%s\">%s</a> :: ", urljson,wallet) > "ok.tmp"
                    printf("OK - svx filenames match - <a href='%s'>%s</a>\n",urljsonsvx,jsonsvx) > "ok.tmp"
                    result="ok"
                    #break # behaves like next not break
                } else  { 
                    printf("%s :: ", wallet)
                    printf("MISMATCH <a href='%s'>%s</a> <-svx  <a href=\"%s\">%s</a> %s\nMISMATCH <a href=\"%s\">%s</a> json-> <a href='%s'>%s</a>\n",urljson,wallet,urlsvxfile,svxfile,included,urljson,wallet,urljsonsvx,jsonsvx) > "svx-refs.tmp"
                    result="mismatch"
                    #break # behaves like next not break
                }
                if (result =="survexline") {
                    printf("%s + FAILTOSET ", wallet)
                } else {
                    # printf("%s + SET =%s= ", wallet, result)
                }
            } else {
                # This wasn't the right line, read the next one..
            }
        } # end of look going through a json file, result should be ok, mismatch or unset
        if (result=="working") {
            # as expected if there are lines after the survex line in the json file
        } else if (result=="survexline") {
            # the last line in the json was the survex line but failed to set to ok, mismatch or unset
            printf("%s :: FAILTOSET %s\n", wallet, jsonsvx)
            #system("cat " json)
            #for (i in jline ) printf("%s %s\n",i,jline[i])
        } else {
            printf("%s :: BIZARRE\n", wallet)
            #printf("%s :: BIZZARE should not get here\n", wallet) > "svx-refs.tmp"              
            #system("cat " json)
            #for (i in jline ) printf("%s %s\n",i,jline[i])
        } 
    } # end of test for missing or empty path or json file
} # end of test for matching a wallet tag
END {

}