include("WBInclusions")

global SHOWUNUSEDS = false

APFS = "258" # WBTree:APFS
version = APFS

mutable struct R
    ct::Int32
    nm::String
end

global V1ID = getBackupNumber()

global SOUGHT = 0
global QUORAS = 0

global nowww = noww()
global DATEPROBLEMS = []
global Validated = -1
global SZ = 0
global EXTERNAL_LINKS = 0
global TOTAL_LINKS = 0
global IMAGE_LINKS = 0
global EMPTIES = 0
global totAUI = 0

global YAHOOS = 0
global ZAHOOS = 0

global szDict = Dict{String,Array{String}}()
global oflDict = Dict{String, Int32}()

global ILUsage = Dict{String, Int32}()
global ILUct = 0
global ILUIgnoreds = Dict{String, Array{String}}(
    "_LynxBlk" => [],
    "_Footnotes" => [],
    "_BRM5V" => []
)

global NUTHIN = ["NUTHIN"]
function ILU(itm)
    a = get!(ILUsage, itm, -1)
    if a == -1
        if itm == "IndexOfSubjects#X" && findfirst(" id=\"X\"", RRead("IndexOfSubjects.html", "clean")) != nothing
            return
        end    
        println(hilite(HI_YELLOW, "    Not found: \"" * itm * "\""))
        return
    end
    ILUsage[itm] = a + 1
    poundStart = findfirst("#", itm).start
    id = itm[poundStart + 1: length(itm)]
    f = itm[1:poundStart - 1]
    if haskey(ILUIgnoreds,id)
        aa = ILUIgnoreds[id]
        if !(f in aa)
             push!(aa, f)
        end
     end
end

global IDS = Dict{String,Array{String}}()
global LWIDS = Dict()
global MAPS = 0
global L2C1FirstPadding = true
global noTOCs = []
global FootnoteBlocks = 0

global HRlen = 24
global HighRevisions = []
for i = 1 : HRlen
    push!(HighRevisions, R(-1 * i, ""))
end

global SumRevisions = 0
global NYTComments = 1  # Add one for th comment I had to take direcly from the NYT website +2023.06.01
global Bingers = 0
global Googlers = 0
global Qsum = 0
global Q = ""
global AllFilesInCurrentDirectory = []

function qwrite(txt)
    global Qsum += length(txt)
    global Q *= txt
end

function ErrorQuit(txt)
    println(hilite(HI_RED, "\n--------\n    " * txt * "\n--------") * "\n")
    exit(16)
end

HSYMS = []

global fdict = Dict{String, String}()
global ildict = Dict{String, Int64}()
global linkCt = 0
global ilinkCt = 0
global pageCt = 0
global incoming = Dict{String, Array{String}}()
global externalLinks = Dict{String, Array{String}}()

global excludeds = ["IndexOfSubjects.html"]
global ignoreds = [
    "JUNK.html",
    "JUNK2.html",
    "JUNK6.html",
    "JUNK7.html",
    "JUNK10.html"
]

pre = "<!DOCTYPE html>\n<html lang=\"en\" dir=\"ltr\">\n<head>\n<link rel=\"stylesheet\" type=\"text/css\" href=\"w/css/WBmiracle.css\">\n<title>Link inventory</title>\n<meta charset=\"UTF-8\">\n<style>\n    tr td span a {text-decoration:none;}\n    .bY {\n        background-color:yellow;\n        padding:2px 5px;\n    }\n    .bY a {\n        font-family:sans-serif;\n    }\n    .bG {\n        font-weight:normal;\n        color:gray;\n    }\n    .bib {\n        font-weight:bold;\n        color:blue;\n    }\n    .px2 {\n        padding:2px;\n    }\n    .px2 a {\n        font-family:sans-serif;\n    }\n</style>\n</head>\n<body style=\"background-color:white;\"><div style=\"padding:2px 0px 0px 6px;\"><a href=\"welcome.html\" style=\"text-decoration:none;\" class=\"rWel\"><img src=\"w/images/1/12/Prev2a.gif\" alt=\"Previous\" style=\"padding-left:3px;padding-right:1px;width:10px; height:12px;\">Welcome</a></div>\n<div class=\"read600\">\n" *
            "<p style=\"font-family:sans-serif;white-space:nowrap;\">WBTree v" * version * ".&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"IndexOfSubjects.html\">Index</a>&nbsp;&nbsp;&nbsp;<a href=\"w/WBKountOut.html\">Images</a>&nbsp;&nbsp;&nbsp;<a href=\"Dashboard.html\">Dashboard</a><span style=\"vertical-align:1px;padding-left:2px;\"><a href=\"Dashboard.html\" ><img src=\"w/images/5/5b/Next2a.gif\" alt=\"Next2a.gif\" style=\"width:10px;height:12px;\"></a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" * APtS_ET * "</span><br>\n"
 
pres = Dict(
    "Tree" => "Go: <a href=\"XXXRTree.html\">RTree</a><span style=\"vertical-align:1px;padding-left:2px;\"><a href=\"XXXRTree.html\" ><img alt=\"Next2a.gif\" src=\"w/images/5/5b/Next2a.gif\" style=\"width:10px;height:12px;\"></a><a href=\"XXXXTree.html\" ><img alt=\"Next2a.gif\" src=\"w/images/5/5b/Next2a.gif\" style=\"width:10px;height:12px;\"></a></span>",
    "RTree" => "Go: <a href=\"XXXXTree.html\">XTree</a><span style=\"vertical-align:1px;padding-left:2px;\"><a href=\"XXXXTree.html\" ><img alt=\"Next2a.gif\" src=\"w/images/5/5b/Next2a.gif\" style=\"width:10px;height:12px;\"></a><a href=\"XXXTree.html\" ><img alt=\"Next2a.gif\" src=\"w/images/5/5b/Next2a.gif\" style=\"width:10px;height:12px;\"></a></span>",
    "XTree" => "Go: <a href=\"XXXTree.html\">Tree</a><span style=\"vertical-align:1px;padding-left:2px;\"><a href=\"XXXTree.html\" ><img alt=\"Next2a.gif\" src=\"w/images/5/5b/Next2a.gif\" style=\"width:10px;height:12px;\"></a><a href=\"XXXRTree.html\" ><img alt=\"Next2a.gif\" src=\"w/images/5/5b/Next2a.gif\" style=\"width:10px;height:12px;\"></a></span>"
)

pre2 = "</p>\n"

post = "</table>\n<p style=\"margin-top:32px;width:600px;text-align:right;\">[ <a href=\"https://validator.w3.org/\"><span style=\"font-size:larger;vertical-align:-1px;\">&Omega;</span>&nbsp;W3C&nbsp;HTML&nbsp;validator</a>&nbsp;&nbsp;~--S,Z-- ]</p></div>\n</body></html>"

println("\nWBTree v", version, " (./V1).  ", nowww, "    " * APtS_ET , 
        ".  V1ID: " * hilite(HI_YELLOW,  string(V1ID)) * "\n")

rm("XXXTree.html", force=true)
rm("XXXRTree.html", force=true)
rm("XXXXTree.html", force=true)

function saveTable(fn, fv)
    global Q = ""
    global Qsum = 0
    qwrite(pre)
    qwrite(pres[fn])
    qwrite(pre2)
    qwrite(replace(fv, r"TheActiFist</a></span><span class=\"bG\">&nbsp;(&lt;=|=&gt;)" =>
            s"TheActiFist</a></span><span class=\"bG\">&nbsp;\1&nbsp;&nbsp;<span style=\"color:darkred;\">Damn him (her, other)!</span>"))
    qwrite(post)
    # sQsum = string(Qsum)
    sQsum = formatFixed(Qsum)    # sQsum[1:3] * "," * sQsum[4:6]
    ilc = ""
    if fn == "Tree"
        ilc = "&nbsp;&nbsp;" * formatFixed(length(findall(" href=\"#",Q)))
    end
    global Q = replace(Q, "--S,Z--" => sQsum * ilc, count = 1) 
    global q = open("XXX" * fn * ".html", "w")
    write(q, Q)
    close(q)
end    

function makeTable(DICT, ptr)
    global L2C1FirstPadding = true
    if ptr == "=&gt;"
        istr = string(ilinkCt)
        if ilinkCt > 999
            listr = length(istr)
            istr = istr[1: listr - 3] * "," * istr[listr - 2: listr]
        end
        global out = "<table style=\"border-collapse:collapse;font-family:sans-serif;font-weight:bold;background-color:lightyellow;padding:10px;\"><tr><td colspan=2 style=\"padding:2px 0px 12px 2px;color:gray;font-weight:normal;\"><span style=\"border:2px solid darkred;padding:2px 3px 1px 2px;background-color:yellow;\"><a href=\"XXXRTree.html\" style=\"text-decoration:none;\">T</a></span> Pages: " * string(pageCt) * ". Interpage links &gt; <span style=\"font-weight:bold;\">" * lc * "</span>. Imaps: " * string(MAPS) * ". NYTs: <span style=\"font-weight:bold;\">" * string(NYTComments) * "</span>. B/G: <span style=\"font-weight:bold;\">" * string(Bingers) * "</span>/<span style=\"font-weight:bold;\">" * string(Googlers) * "</span><br><span style=\"white-space:nowrap;\"><span style=\"color:blue;padding-top:4px;\">" * nowww * "</span>.  Intrapage ('#') links > " * istr * ".  Img links: " * formatFixed(IMAGE_LINKS) * ".  Footnotes: " * string(FootnoteBlocks) * ".</span></td></tr>\n"
        global ILINKS = istr
        global RLINKS = lc
    elseif ptr == "&lt;=" 
        global out = "<table style=\"border-collapse:collapse;font-family:sans-serif;font-weight:bold;background-color:lightyellow;padding:10px;\"><tr><td colspan=2 style=\"padding:2px 0px 12px 2px;color:gray;font-weight:normal;\"><span style=\"border:2px solid darkred;padding:2px 3px 1px 2px;background-color:yellow;\"><a href=\"XXXXTree.html\" style=\"text-decoration:none;\">R</a></span> Reverse table of incoming internal interpage links:<br><span style=\"color:blue;\">" * nowww * "</span></td></tr>\n"
    else
        global out = "<table style=\"border-collapse:collapse;font-family:sans-serif;font-weight:bold;background-color:lightyellow;padding:10px;\"><tr><td colspan=2 style=\"padding:2px 0px 12px 2px;color:gray;font-weight:normal;\"><span style=\"border:2px solid darkred;padding:2px 3px 1px 2px;background-color:yellow;\"><a href=\"XXXTree.html\" style=\"text-decoration:none;\">X</a></span> !QWERTY! outgoing external links (several uninteresting items excluded):<br><span style=\"color:blue;\">" * nowww * "</span>&nbsp;&nbsp;(Validator: !VICTORY!)</td></tr>\n"
    end
    global shit = []
    global shit = collect(keys(DICT))
global shoot = copy(shit)
    ghosts = []
    crt = 0
    shitx = sort(shit, by=lowercase)
    for s in shitx
        s2 = s[1:(length(s) - 5)]
        if length(DICT[s]) == 0
            push!(ghosts, (s2 == "IntoTheLight" ? "<a href=\"https://www.bmccedd.org/IntoTheLight.html\">IntoTheLight</a>" : "<a href=\"" * s2 * ".html\">" * s2 * "</a>"))
        end
        ilss = "" 
        ils = get(ildict, s, 0)
        if ils > 0
            ilss = "(DDD" * string(ils) * ")"
        end
        if ptr != "&lt;&lt;="
            if s2 == "welcome"
                s2a = ""
            else
                s2a = "|" * s2
            end
            ohmygod = "<a style=\"\" href=\"" * s2 * ".html\">" * s2 * "</a>"
            if s2 == "IntoTheLight"
                ohmygod = "<a style=\"\" href=\"https://www.bmccedd.org/IntoTheLight.html\">IntotheLight</a>"
            end
            global out *= "<tr><td colspan=2 class=\"px2\"><span class=\"bY\" id=\"" * Translates[s2] * "\">" * ohmygod * "</span><span class=\"bG\">&nbsp;" * ptr * "</span></td></tr>"
        else
            if s2 == "https://validator.w3"
                global Validated = length(findall(",", DICT[s])) + 1
                DICT[s] = "[ <span style=\"font-style:italic;\">TMTC:</span> " * string(Validated) * " ]"
            end
            crtx = string(crt += 1)

            sFixed = replace(s, "&amp;" => "!!!")
            sFixed = replace(sFixed, r"(!!!|&)" => "&amp;")
            if s != sFixed
                println("\n    \"", s, "\"\n    \"", sFixed, "\"\n")
            end

            crtx = ("&nbsp;" ^ (4 - length(crtx))) * crtx
            global out *= "<tr" * (s == "https://www.bmccedd.org/IntoTheLight.html" ? " id=\"IntoTheLight\"" : "") * "><td colspan=2 class=\"px2\"><span style=\"font-weight:normal;\">" * crtx * ".</span>&nbsp;<span style=\"background-color:yellow;padding:2px 5px;overflow-wrap:anywhere;\"><a href=\"" * sFixed * "\">" * sFixed * "</a></span><span class=\"bG\">" * ilss * " " * ptr * "</span></td></tr>"
        end
        if s2 in ["OnCitation", "Date", "BRADFORD", "EndOfEarth"] && ptr == "&lt;="
            global out *= "<tr><td> </td><td>&#91; <span style=\"font-style:italic;\">TMTC:</span> " * string(1 + length(findall(",", DICT[s]))) * " ]</td></tr>"
        
            if s2 == "Date" 
                #ds = replace(DICT[s], r"<a href=\"#([^\"]+)\">\1</a>" => s"\1.html")
                ds = replace(DICT[s], r"<a href=\"#[a-zA-Z]{1,2}\">([^\"]+)</a>" => s"\1.html")
                global DS = split(ds, ", ")
                global DICK = Dict()
                for shooter in shoot
                    DICK[shooter] = 0
                end
                for ddss in DS
                    DICK[ddss] = 1
                end  
                k = keys(DICK)
                for kk in k
                    if DICK[kk] == 0
                         push!(DATEPROBLEMS, kk[1:length(kk) - 5])
                    end
                 end
                 global DATEPROBLEMS = sort(DATEPROBLEMS)
            end

        elseif s2 != "welcome" || ptr != "&lt;="
            global out *= "<tr><td style=\"padding:2px" * (L2C1FirstPadding ? " 2px 2px 40px" : "") * ";\"> </td><td class=\"px2\">" *
               (ptr == "&lt;=" && s2 == "IntoTheLight" ? "&#91; <a href=\"XXXXTree.html#IntoTheLight\">All incoming links remote</a> <span style=\"font-weight:normal;\">==&gt;</span> ]" : (length(DICT[s]) > 0 ? DICT[s] : "&nbsp;")) *
               "</td></tr>\n"
        else
            losers = ""
            snoTOCs = sort(noTOCs)
            for item in snoTOCs
                loser = item[1:length(item) - 5]
                if loser == "welcome"
                    continue
                end
                losers *= "<a href=\"" * loser * ".html\">" * loser * "</a>, "
            end
            global out *= "<tr><td style=\"padding:2px" * (L2C1FirstPadding ? " 2px 2px 40px" : "") * ";\"> </td><td class=\"px2\">!TOC&#91;" *
               losers[1 : length(losers) - 2] * "]</td></tr>\n"
        end
        global L2C1FirstPadding = false
    end
    gh = replace(replace(replace(string(ghosts), "\\\"" => "SHIT"), "\"" => "\""), "SHIT" => "\"")   
    out *= (length(ghosts) == 0 ? "" : "<tr><td colspan=2 style=\"padding:20px 0px 2px 2px;color:gray;font-weight:normal;\">Ghosts: " * gh * "</td></tr>")
    out = replace(out, "!QWERTY!" => string(crt), count=1)
    return(out)
end

# Shrink code by translating all internal links into 2 character codenames, so "#BRADFORD" might become "#bO"
# This will break at 2705 files
Alphas = [  "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M",
            "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z",
            "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m",
            "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]
global Translates = Dict{String,String}()
global trix = 0

function setID(fn)
    if length(fn) == 1
        Translates[fn] = fn
        return
    end
    ch1 = div(trix,52)
    ch2 = trix - ch1 * 52
    Translates[fn] = Alphas[ch1 + 1] * Alphas[ch2 + 1]
    global trix += 1
end
 
try
    global errId = 1
    THEGODDAMNEDDIR = readdir()
    for itm in THEGODDAMNEDDIR
        if findfirst(".html", itm) == nothing || itm in ignoreds
            continue
        end
        incoming[itm] = []
        itmX = itm[1 : length(itm) - 5]
        setID(itmX)
        push!(AllFilesInCurrentDirectory, itmX)
    end

    zz = replace(RRead("IndexOfSubjects.html", "clean"), r"&#10004;(\(\d+\) )?" => "") 
    z = findall(r"<li><a href=\"([^.]+).html\" title=\"([^\"]+)\"[ >]", zz)
    global FilesInIndexOfSubjects = []
    for itm in z
        shitstain = replace(zz[itm], r"<li><a href=\"([^.]+)\.html\" title=\"([^\"]+)\"[ >]" => s"\1,\2")
        shitstain = replace(shitstain, " " => "_")
        shitcomma = findfirst(",", shitstain).start
        firstshit = shitstain[1:shitcomma - 1]
        lastshit =  shitstain[shitcomma + 1 : length(shitstain)]
        if firstshit != lastshit
            println("\n", hilite(HI_RED, "    Mismatched entry in IndexOfSubjects \"" * firstshit * "\" \"" * lastshit * "\""), "\n")
            exit(16)
        end
        if firstshit in FilesInIndexOfSubjects
            println("Duplicate in IndexOfSubjects: \"", shitstain, "\"")
            exit(16)
        end        
        push!(FilesInIndexOfSubjects,firstshit)
    end
    for itm in AllFilesInCurrentDirectory
        if !(itm in FilesInIndexOfSubjects)
            println("** In AllFilesInCurrentDirectory but not in FilesInIndexOfSubjects: " , itm)
        end
    end
    for itm in FilesInIndexOfSubjects
        if !(itm in AllFilesInCurrentDirectory)
            println("** In FilesInIndexOfSubjects but not in In AllFilesInCurrentDirectory: " , itm)
        end
    end

    global ALLUNUSEDIDS = Dict{String,Array{String}}()
    global ORIGINALUNUSEDIDS = Dict{String,Array{String}}()
    rr = 0
    for f in FilesInIndexOfSubjects
        ftext = RRead(f * ".html", "clean")

        if length(findall(r"[{}]", replace(ftext, r"<head>.*?</head>"s => ""))) != 0
            println("\n\n        " * hilite(HI_RED, "Stray \"{}\" characters in " * f * ".html.") * "\n\n")
        end

        ff = findall(r" id=\"[^\"]+", ftext) 
        rr += length(ff)
        for fff in ff
            t = ftext[fff.start+5:fff.stop]

            ILUsage[f * "#" * t] = 0

            abitch = get!(ALLUNUSEDIDS, f, ["MISSING"])
            bbitch = get!(ORIGINALUNUSEDIDS, f, ["MISSING"])
            if abitch != bbitch
                println("ADDSADSADSADSADSADA")
                exit(16)
            end
            if abitch == ["MISSING"]
                ALLUNUSEDIDS[f] = [t]
                ORIGINALUNUSEDIDS[f] = [t]
                continue
            end
            push!(abitch, t)
            push!(bbitch, t)
        end
    end

    global errId = 2
    vTable = []
    for itm in keys(incoming)
        global pageCt += 1        
        item = itm
        global errId = 4
        sres = RRead(item, "clean")
        global errId = 5
        global SZ += length(sres)
        x = findfirst("href=\"welcome.html#TOC\"", sres)
        if x == nothing
            push!(noTOCs,item)
        end
        ofl = findfirst(r"<!-- OFL \d{3} -->", sres)
        if ofl != nothing
            oflDict[item] = tryparse(Int32, sres[ofl][10:12])            
        end

        global EXTERNAL_LINKS += length(findall(r"<a(rea)? href=\"https?://", sres))
        global TOTAL_LINKS += length(findall(r"<a(rea)? href=\"", sres))
        
        global IMAGE_LINKS += length(findall(r" href=\"w/images[^.<]+\.(jpg|gif|png)\" ", sres))
        global EMPTIES += length(findall(r"<(span|div)>", sres))

        fns = length(findall(r"Footnotes</a>(</span>)+</h3>", sres))
        if fns > 0
            if fns > 1
                println("\n", hilite(HI_RED, "Footnote problem in " * item), "\n")
                exit(16)
            end
            global FootnoteBlocks += 1
        end

        global NYTComments += length(findall("Your comment has been approved!", sres))
        global Bingers += length(findall("This page indexed on Bing", sres))
        global Googlers += length(findall("This page indexed on Google", sres)) 
        
        if startswith(itm, "Quora")
           aa = r"\d{3}&nbsp;&nbsp;([\d,]+)\.</td>"    
           global QUORAS += tryparse(Int32, replace(sres[findfirst(aa,sres)],aa => s"\1"))
        end 

        # "  +2022.04.30 v002  "
        yyyyyy = findall(r">\+[0-9][0-9][0-9][0-9]\.[0-9][0-9]\.[0-9][0-9] v[0-9][0-9][0-9]", sres)
        yLen = length(yyyyyy)
        if yLen != 1 && !(item == "Experiment.html" && yLen == 3)        # KLUDGE
            ErrorQuit("Versioning wrong in file:  " * item)
        end

        # Police department
        if length(findall(r"({{|}})", sres)) != 0
            ErrorQuit("Loose bracket(s):  " * item)
        end
        if length(findall("size:larger;\"> </span", sres)) != 0
            ErrorQuit("Weird \"size:larger;\":  " * item)
        end

        rv = parse(Int32, sres[yyyyyy[yLen]][15:17])
        global SumRevisions += rv
        rva = "0000" * string(rv)
        #write(vFile, rva[length(rva) - 3: length(rva)] * " " * item * "\n")
        push!(vTable, item * " " * rva[length(rva) - 3: length(rva)])

        for i = 1 : HRlen
            if rv >= HighRevisions[i].ct
                if i == HRlen
                    HighRevisions[HRlen] = R(rv,item)
                    break
                end
                nh = []
                if i > 1
                    nh = HighRevisions[1:i - 1]
                end
                global HighRevisions = append!(nh, [R(rv,item)], HighRevisions[i:HRlen - 1])
                break
            end
        end        
            
        x = findall(r"href=\"[a-zA-Z0-9-_!]+\.html#[^\"]+", sres)
        y = []
        for z in x
            w = sres[z]
            v = findfirst("#", w)
            push!(y, [w[7 : v.start - 6], w[v.start + 1 : length(w)]])
            global ILUct += 1
            ilu = w[7 : v.start - 6] * "#" *  w[v.start + 1 : length(w)]
            ILU(ilu)
            goddamnedfilename = w[7 : v.start - 6]
            if goddamnedfilename == "IndexOfSubjects"
                continue
            end
            goddamnedlink = w[v.start + 1 : length(w)]
            ids = ALLUNUSEDIDS[goddamnedfilename]
            if goddamnedlink in ids
                kky = findall(x->x == goddamnedlink, ids)
                if length(kky) > 1
                    println("\n", hilite(HI_RED, "    Duplicate \"id\" name? \"" * item * "\"  \"" * goddamnedfilename *
                         "\"\n        " * sring(kky) * "  \"" * goddamnedlink, "\""), "\n")
                    exit(16)
                end
                if length(kky) == 1
                     deleteat!(ids, kky)
                end 
            end    
        end

        yahoo = []
        xhoo = findall(r" href=\"#[^\"]+", sres)
        global YAHOOS += length(xhoo)
        for zhoo in xhoo
            w = sres[zhoo]
            ww = w[9 : length(w)]
            global ILUct += 1
            ilu = item[1:length(item) - 5] * "#" *  ww
            ILU(ilu)


            ids = ALLUNUSEDIDS[item[1: length(item) - 5]]
            if ww in ids
                kky = findall(x->x == ww, ids)
                if length(kky) > 1
                    println("Duplicate item in ALLUNUSEDIDS: " * item * " \"" * ww * "\"")
                    exit(16)
                end
                if length(kky) == 1
                     deleteat!(ids, kky)
                     if length(findall(x->x == ww, ids)) != 0
                         println("Deleteat failed for: " * item * " \"" * ww * "\"")
                         exit(16)
                     end
                end 
            end



            push!(yahoo, ww)
        end

        zahoo = []
        xhoo = findall(r" id=\"[^\"]+", sres)
        global ZAHOOS += length(xhoo)
        for zhoo in xhoo
            w = sres[zhoo]
            push!(zahoo, w[6 : length(w)])
        end

        for src in yahoo
            if !(src in zahoo)
                println("        " * item * " internal link src missing target: \"" * src * "\"")
            end
        end

        ittm = itm[1 : length(itm) - 5]
        LWIDS[ittm] = y
        x = findall(r" id=\"[^\"]+", sres)
        y = []
        for z in x
            push!(y, sres[z.start + 5 : z.stop])
        end
        IDS[ittm] = y

        x = findall(r"&#[^;]+;", sres)
        for y in x
            z = sres[y]
            if z in HSYMS
                continue
            end
            push!(HSYMS, z)
        end 
                  
        if itm in excludeds
            global fdict[itm] = "&#91; <span style=\"font-style:italic;\">Everything</span> ]"
            continue
        end

        if findfirst(r"aplacetostudy"i,  sres) != nothing
            println((item in ["Dashboard.html", "JSource.html"] ? "" : "** ") * "\"Aplacetostudy\": " * item)
        end

        global MAPS += length(findall("<map name=\"", sres))

        refs = findall(r"href=\"[a-zA-Z0-9-_!]+\.", sres)

        global linkCt += length(findall(r"href=\"w/txts/(VirtualReality|WhitePrivilege)\.txt\"", sres))

        srefs = []
        for r in refs
            ss = sres[r]
            ss = ss[7: (length(ss) - 1)]
            if !(ss in srefs)
                push!(srefs, ss)
            end
            if !(ss * ".html" in THEGODDAMNEDDIR) && !(ss in ["XXXTree",  "XXXRTree", "XXXXTree"])
println("                           ----\"", ss, "\"    ", ss * ".html" in THEGODDAMNEDDIR)
                ErrorQuit("Bad link in " * itm * ": \"" * ss * ".html\".")
            end
            if !(itm in incoming[ss * ".html"])
                push!(incoming[ss * ".html"], itm)
            end
            global linkCt += 1 
        end
        str = ""
        for x in sort(srefs, by=lowercase)
            str *= (str != "" ? ", " : "") * "<a href=\"#" * Translates[x] * "\">" * x * "</a>"
        end
        global fdict[itm] = str
        global ildict[itm] = iq = length(findall(r"<a (style=\"text-decoration:none\" )?href=\"#", sres))
        if !(itm in ["XXXRTree", "XXXTree", "XXXXTree"])
            global ilinkCt += iq
        end

        xls = findall(r"( href=\"|\|link=)http.*?[\" ]", sres)
        for xl in xls
            t = sres[xl]
            t = replace(t, r"( href=\"|\|link=)" => "", count=1)
            t = t[1:length(t) - 1]
            if get(externalLinks, t, "!") == "!"
                externalLinks[t] = [itm]
            else
                if !(itm in externalLinks[t])
                    push!(externalLinks[t], itm)
                end
            end
        end
    end
    vTable = sort(vTable)
    global vDict = "global V = [\"" * replace(string(Dates.now()), r"T(.{8}).*" => s" \1") * "  " * V1ID *
                "\", Dict{String,String}(\n"
    for item in vTable
        a = findfirst(" ", item)
        #vText *= item[a.start + 1:length(item)] * " " * item[1 : a.start - 1] * "\n"
        dictName = item[1 : a.start - 1]
        dictValue = item[a.start + 1:length(item)] 
        global vDict *= "  \"" * dictName * "\" => \"" * dictValue * "\",\n"
        ofl = get!(oflDict, dictName, 0)
        dictValue = "0000000" * string(tryparse(Int32,dictValue) + 1000 * ofl)
        dictValue = dictValue[length(dictValue) - 4:length(dictValue)]
        dv = get!(szDict, dictValue, ["NOTHING"])
        if dv == ["NOTHING"]
            szDict[dictValue] = [dictName]
        else 
            szDict[dictValue] = append!(szDict[dictValue], [dictName])
        end        
    end
    shitshit = sort(collect(keys(szDict)), rev=true)
    hiVersions = ""
    for i = 1 : 12
        s = shitshit[i]
        hiVersions *= "   " * s * "   " * replace(string(szDict[s]), r"([\[\]\"]|\.html)" => "") * "\n"
    end
    szFile = open("HIversions.txt", "w")
    write(szFile, hiVersions)
    close(szFile)
catch ex
    s = string(ex)  
    if length(s) > 200
        s = s[1:200] * "..."
    end
    ErrorQuit("\n      Failure[" * string(errId) * "]: " * string(typeof(ex)) * ".\n" * s);
end

lc = string(linkCt)
if linkCt > 999
    lenlc = length(lc)
    lc = lc[1:lenlc - 3] * "," * lc[lenlc - 2:lenlc]
end

global incoming2 = Dict{String, String}()
for k in keys(incoming)
    ary = incoming[k]
    str = ""
    for x in sort(ary, by=lowercase)
        stp = x[1:length(x)-5]
        str *= (str != "" ? ", " : "") * "<a href=\"#" * Translates[stp] * "\">" * stp * "</a>"
    end
    global incoming2[k] = str
end

T = makeTable(fdict, "=&gt;")

RT = makeTable(incoming2, "&lt;=")

global incoming2 = Dict{String, String}()
for k in keys(externalLinks)
    if findfirst("https://faculty.washington.edu/walter/Killing%20Cats3.html#_edn", k) != nothing
        continue
    end
    ary = externalLinks[k]
    str = ""
    for x in sort(ary, by=lowercase)
        stp = x[1:length(x)-5]
        stp2 = ""
        if stp != "welcome"
            stp2 = "|" * stp
        end 
        str *= (str != "" ? ", " : "") * "<a href=\"" * stp * ".html\">" * stp * "</a>"    # ZZZZZ
    end
    global incoming2[k] = str
end

XT = makeTable(incoming2, "&lt;&lt;=")
XT = replace(XT, "!VICTORY!" => string(Validated), count=1) 

saveTable("Tree", T)
saveTable("RTree", RT)
saveTable("XTree", XT)

println("\n--------\n")

global ct = 0
global chksL = 0
global chksR = 0
shitx = sort(collect(keys(LWIDS)))

for k in shitx
    for kk in LWIDS[k]
        if kk[1]== ""                # local links in same file
            global chksL += 1
            if kk[2] in IDS[k]
                continue
            end
            spacer = ""
            if length(k) < 12
                spacer = " " ^ (12 - length(k))
            end
            println(k, spacer, " ? \"#" * kk[2] * "\"")
            global ct += 1
            continue
        end
        global chksR += 1       
        if kk[2] in IDS[kk[1]]
            continue
        end
        spacer = ""
        if length(k) < 12
            spacer = " " ^ (12 - length(k))
        end
        println(k, spacer, " ? \"" * kk[1] * "#" * kk[2] * "\"")
        global ct += 1
    end
end

global tks = 0
global uks = 0
global uksTotal = 0

global kyy = []
for ky in keys(ALLUNUSEDIDS)
    push!(kyy, string(ky))
end
for ky in sort(kyy)   #   keys(ALLUNUSEDIDS)
    global tks += length(ORIGINALUNUSEDIDS[ky])
    global uks += length(ALLUNUSEDIDS[ky])
    global uksTotal += length(ALLUNUSEDIDS[ky])
    for ignore in keys(ILUIgnoreds) 
        if ignore in ALLUNUSEDIDS[ky]
            global uks -= 1
        end
    end

        aui = []
        for itm in ALLUNUSEDIDS[ky]
            if !(startswith(itm, "cite") || itm in keys(ILUIgnoreds))
                push!(aui, itm)
            end
        end
        
        lenaui = length(aui)
        if lenaui > 0
            global totAUI += lenaui

#----------------
            if SHOWUNUSEDS
                println("** " , length(aui), "   ", ky, "  ", sort(aui))
            end
#----------------

        end

end

global usedMulti = 0
global usedOnce = 0

global multis = 0
global keyCount = 0
global kk = 0
#global kkk = []
global II = 0
for k in keys(ILUsage)
    global keyCount += 1
    ii = ILUsage[k]
    if ii == 0

        kkkk = findfirst("#", k)
        kkkk = k[kkkk.start+1: length(k)]
        if kkkk in keys(ILUIgnoreds)
            global II += 1
        end
    elseif ii == 1
        global usedOnce += 1;
    else
        global usedMulti += 1
        global multis += ii - 1
        global kk += 1
        iii = " " ^ (6 - length(string(ii))) * string(ii)
        #push!(kkk, iii * "  " * string(k))
    end
end

#kkk = sort(kkk, rev=true)
#for j in kkk
#    println(j)
#end

function X(position, data)
    x = data - tryparse(Int32,xdata[position])
    if x != 0 
        XS[position] = "[" * string(x) * "]"
        global xdata[position] = string(data)
        global changed = true
    end
end    

global xdata = split(RRead("TreeData", "asis"), "\n")
global XS = []
for i = 1 : length(xdata)
    push!(XS, "")
end
global changed = false;

X(1,TOTAL_LINKS - EXTERNAL_LINKS)
X(2,EXTERNAL_LINKS)

X(3, NYTComments)
if changed
    xfile = open("TreeData", "w")
    write(xfile, xdata[1] * "\n" * xdata[2] * "\n" * xdata[3])
    close(xfile)
end

iDups = (tks - uksTotal) - ilinkCt

q1 = Duree(DateTime(2023,8,1,16,00), now())
q2 = formatDecimal(QUORAS / (tryparse(Int32, q1[findfirst(r"\d+", q1)]) + 1), 4) * "0000"
q3 = hilite(HI_YELLOW, replace(q2, r"^(\d+\.\d).*" => s"\1")) * replace(q2, r"^\d+\.\d(\d).*" => s"\1") 

println("\n  Links: ",   
        hilite(HI_YELLOW, formatFixed(TOTAL_LINKS - EXTERNAL_LINKS)), XS[1], "i + ", 
        hilite( HI_YELLOW, formatFixed(EXTERNAL_LINKS)), XS[2], "r.  ", formatFixed(SZ),
        ".  Empties: ", hilite(HI_YELLOW, formatFixed(EMPTIES)), ".  Quoras: ", formatFixed(QUORAS), 
                " (" * q3 * ").  Orphan IDs: ", formatFixed(totAUI))
global HR1s = []
global a = open("JUNK5.txt", "w")
write(a, "  " * "+" * replace(replace(noww(), "-" => "."), " " => "+")[1:16] * ". WBTree\n\n")
for i = 1 : HRlen
    i1 = HighRevisions[i].ct
    if i < 7
        push!(HR1s,i1)
    end
    i1 = string(i1)
    pfx = " " ^ (8 - length(i1)) 
    write(a, pfx * i1 * "  " * HighRevisions[i].nm * "\n")
end
close(a)
hrs = string(HR1s)
println("\n  Errs:", (ct == 1 ? "" : HI_YELLOW), ct, (ct == 1 ? "" : HI_END), 
          ".  NYTs: ", hilite(HI_YELLOW, NYTComments), XS[3], ".  B/G: ", 
          hilite(HI_YELLOW, Bingers), "/", hilite(HI_YELLOW, Googlers), 
          ".  Revs: " * hilite(HI_GREEN, replace(hrs[5:length(hrs) - 1], "," => "")) *
          "  (" * hilite(HI_GREEN, formatFixed(string(SumRevisions))) * ")   " * hilite(HI_AQUA,
                  " ** Good to go! **"))

sdp = string(DATEPROBLEMS)
sdp = sdp[4 : length(sdp)]

println("\n",length(DICK), "-", length(DS), "=", 
        length(DATEPROBLEMS), ".  DateProblems ", # .  ?: ",length(shoot), "  ", 
        sdp)    #string(DATEPROBLEMS)

#println("\n    YAHOOS: " , YAHOOS, ". ZAHOOS: " , ZAHOOS, ".")

vFile = open("V1", "w")
write(vFile, "# " * string(pageCt - 1) * " " *           # Don't count IndexOfSubjects
          string(linkCt + EXTERNAL_LINKS + IMAGE_LINKS) * 
          "  sum(map((x) -> tryparse(Int32,string(x)), values(V[2])))\n" * vDict * ")]\n")
close(vFile)

#println("\nSOUGHT: " , SOUGHT)
