include("../WBInclusions")
using HTTP
include("DICT")

#pushD("Oh My God", ['G', "G2"])

APFS = "032" # WBcompare:APFS
version = APFS

global CLEANUPLOCALONLY = length(ARGS) == 2 || ARGS[2] == "CLEANUP"
global ANALYZEALL = length(ARGS) == 2 || ARGS[2] == "MICE"
if !CLEANUPLOCALONLY && !ANALYZEALL
    println("No operation specified. No work done. Exiting...."
    exit(16)
end

global big = 150000
global BIGS = []
global t = time();
global PrevTime = 0.0

global URLroot = "https://www.bmccedd.org/w/images/"

println("\n  Remote: " * URLroot * "\n      (Bad: remote -> local len)\n")

global tbl = Array{String}(undef, 2100)
global sz = 0
global totlen = 0

function myPrintln(txt...)
    for t in txt
        write(LOG, string(t))
    end
    write(LOG, "\n")
    println(txt...)
end

function getImage(img)
    global errId = 0
    try
        global errId = 1
        a = HTTP.get(URLroot * img)
        global errId = 2
        ab = a.body
        global errId = 3
        global errId = 4
        fb = RRead("./images/" * img, "raw")
        global errId = 6
        return([ab, fb])
    catch ex
        s = string(ex)  
        if length(s) > 200
            s = s[1:200] * "..."
        end
        myPrintln("\n      Failure[" * string(errId) * "]: ", typeof(ex), ".\n", s);
        #exit(16)
        return nothing
    end
end

global LOG = open("WBC.log", "w")

global others = []

for (root, dirs, files) in walkdir(".")
    for file in files
        x = joinpath(root, file) # path to files
        if !contains(x, "images")
            push!(others,x)
            continue
        end
        global sz += 1
        tbl[sz] = replace(x[10:length(x)], "\\" => "/")
        q = getD(tbl[sz][6:length(tbl[sz])])
# println("----             \"", q, "\"  ", typeof(q))  # God damn strong typing! [string, string, int?] 
        if q == MISSING
            myPrintln("============> Missing from DICT: \"" * tbl[sz] * "\"")
            if CLEANUPLOCALONLY
                rm("images/" * tbl[sz])
            end
            push!(q, 1)
            continue
        end
        push!(q, 0)
    end
end
for itm in keys(D)
    if length(D[itm]) < 3
        myPrintln("============> Missing from local: \"" * itm * "\" \"" * string(D[itm]) * "\"")
    end
end

# Kludge to get rid of dead files in a remote setup

if !ANALYZEALL
    exit(0)
end       

for i = 1 : sz
    tbli = tbl[i]
    sleep(1)
    if i % 50 == 0
        ii = "    " * string(i)
        lenii = length(ii)
        ii = ii[lenii - 4 : lenii]
        NewTime = round(time() - t, digits=4)
        #dt = string(Dates.now())
        #dtx = findfirst('T', dt)
        #noww = dt[1: dtx - 1] * " " * dt[dtx + 1: 19]
        tdiff = replace(string(NewTime - PrevTime), r"\.\d+" => "")
        myPrintln("---- Processed " * ii * " files. " * noww() * " ---- ",
                        NewTime, " (", tdiff, ")")
        global PrevTime = NewTime
    end
    x = getImage(tbli)
    if x == nothing
        continue
    end
    lenx1 = length(x[1])
    lenx2 = length(x[2])
    global totlen += lenx1
    if lenx1 != lenx2 || x[1] != x[2]
        myPrintln("    Bad: " * 
            string(i) * ". " * tbli[1:5] * " " * tbli[6:length(tbli)] * " " * 
                    string(lenx1) * " " * string(lenx2) )
    end
    bigger = max(lenx1, lenx2)
    if bigger >= big
        push!(BIGS, string(bigger) * ":" * tbli)
    end
end

a = join(sort(BIGS, rev=true),"\n")
b = replace(a, ":" => "  ")
b = replace(b, r"( [0-9a-f]/[0-9a-f]{2})/" => s"  \1    ")
z = open("WBIG", "w")
write(z, b * "\n\n" * join(sort(others),"\n"))
close(z)

stotlen = formatNumber(totlen) 

tmt = time() - t
myPrintln("\n" * string(sz) * " files in directories. " * 
            stotlen * " total length. v" * version * "  ",
            string(round(tmt, digits=4)) * " (" * string(round(tmt / 60, digits=3)) * ")\n")

write(LOG, "\n")
close(LOG)