include("WBInclusions")
using HTTP
    
global HICOLOR = HI_AQUA
global version = "052"

global downLoadCount = 0
global downLoadLength = 0

if length(ARGS) == 0 || beginswith(ARGS[1], r"[wWpPtT]")

    # Read file "V1" and execute it. This sets variable "V", from which we set V_2
    
    EMP(replace(String(HTTP.get("https://www.bmccedd.org/V1").body), "\n *" => ""))
    global V_2 = V[2]
    
    global doStart = true
    global didSomething = false
    global progressCTR = 0
    
    global findTS = r">\+\d{4}\.\d{2}\.\d{2} v\d{3}"
    
    function doOne(fname, txt, oldRVA)
        cc = "https://www.bmccedd.org/" * fname
        f = open(fname, "w")
        newBody0 = HTTP.get(cc).body
        write(f, newBody0)
        close(f)
        newBody = String(newBody0)
        global downLoadLength += length(newBody)
        global downLoadCount += 1
        allVStamps = findall(findTS, newBody)
        lenAV = length(allVStamps)
        if lenAV != 1 && !(fname == "Experiment.html" && lenAV == 3)        # KLUDGE
            println(hilite(HICOLOR, "Versioning doOne wrong in file:  " * fname))
            exit(16)
        end
        rv = parse(Int32, newBody[allVStamps[lenAV]][15:17])
        newRVA = "0000" * string(rv)
        newRVA = newRVA[length(newRVA) - 3: length(newRVA)]
        v = get(V_2, fname, "----")
        if v == "----"
            println(hilite(HICOLOR, "  doOne In target but not in source: " * fname))
        elseif v != newRVA
            println(hilite(HICOLOR, "  ====> doOne remote not match V_2 for " * fname * ": (" * oldRVA * ") " * newRVA * " ? " * v)) 
        end
        println(txt)

        global didSomething = true
    end
    
    #2022-10-13 16:12    ChangeLog.html
    
    println("\nWBW  v" * version * ". lenV: " * string(length(V_2)) * ".    T/stmp: " * V[1] * "\n")
    
    try
        aLocal = readdir()
        for itm in aLocal
            if findfirst(".html", itm) == nothing || 
                    startswith(itm, r"XXX|JUNK")
                continue
            end
            FileContents = RRead(itm, "asis")
            allVStamps = findall(findTS, FileContents)
            lenAV = length(allVStamps)
            if lenAV != 1 && !(itm == "Experiment.html" && lenAV == 3)        # KLUDGE
                println(hilite(HICOLOR, "Versioning local wrong in file:  " * string(lenAV) * "  " * itm))
                exit(16)
            end
            rv = parse(Int32, FileContents[allVStamps[lenAV]][15:17])
            currentRVA = "0000" * string(rv)
            lenCRVA = length(currentRVA)
            currentRVA = currentRVA[lenCRVA - 3: lenCRVA]
            v = get(V_2, itm, "----")
            if v == "----"
                println(hilite(HICOLOR, "    In target but not in source: " * itm))
            elseif v != currentRVA
                if v > currentRVA
                    doOne(itm, "  Updated  " * currentRVA * " > " * v * "   " * itm, currentRVA)
                else 
                    println(hilite(HICOLOR, "        <<<< \"" * itm * "\" " * v * " < " * currentRVA * " <<<<"))
                end
            end
            delete!(V_2, itm)
        end
        for k in keys(V_2)
            doOne(k, "  Added                  " * k, "n/a")
        end         
    catch ex
        s = string(ex)  
        if length(s) > 200
            s = s[1:200] * "..."
        end
        println(hilite(HICOLOR, "\n      Failure: " * string(typeof(ex)) * ".\n" * s))
        return 16
    end
    if didSomething
        println(" \n     Downloaded: " * formatFixed(downLoadLength) * "  " *
                formatFixed(downLoadCount) * "\n")
    end
end

if length(ARGS) > 0 && !beginswith(ARGS[1], r"[iI]")
    println(hilite(HICOLOR, "Invalid ARGS[1]: \"" * ARGS[1] * "\""))
    exit(16)
end

global doEnd = false
global doStart = true

#   2022-10-02 15:12     w/images/c/cc   SmilingLady.jpg 123

b = split(String(HTTP.get("https://www.bmccedd.org/V2").body), "\n")

println("WBYY  v" * version)
for itm in b
    if length(itm) < 42 || itm[24:32] != "w/images/"
        continue
    end
    try
        d = findlast(" ", itm).start
        c = itm[24:36] * "/" * itm[40:d - 1]
        len = itm[d + 1: length(itm)]
        doit = true
        what = "new " * len
        if isfile(c)
            sz = string(stat(c).size)
            what = (doit = (sz = string(stat(c).size)) != len) == true ? sz * ":" * len : len
        end
        if doit
            c113 = c[1:13]
            if !isdir(c113) && isdir(c[1:10])
                mkdir(c113)
                print(" [ New directory: " * c113 * " ] ")
            end
            cc = "https://www.bmccedd.org/" * c
            if doStart
                println(" ")
                global doStart = false
            end
            print("    " * cc)
            #res = HTTP.get(cc)
            f = open(c, "w")
            write(f, HTTP.get(cc).body)
            close(f)
            println(" Done.  " * what)
            global doEnd = true
        end
    catch ex
        s = string(ex)  
        if length(s) > 200
            s = s[1:200] * "..."
        end
        println(hilite(HICOLOR, "\n      Failure: " * string(typeof(ex)) * ".\n" * s))
        #return 0
    end
end
if doEnd
    println(" ")
end