include("WBInclusions")

global VERSION = "004"
global BASE = "/ROM/BO/c9/" 
global OKs = Dict()
AGOs = []
USERS = []

EMP("global C9IMAGDICT = " * RRead("C9IMAGDICT", "asis"))

global ix = 1
function RD(dir)
    # println("          dir: \"",dir, "\"")
    files = readdir(dir)
    for f in files
        if isdir(dir * f)
            RD(dir * f * "/")
            continue
        end
        if !(endswith(f, ".jpg") || endswith(f, ".gif") || endswith(f, ".png"))
            continue
        end
        global ok = false
        global notok = 0
        global q = ""
        global force = false
        while !ok
            try
                if isfile("\\Oldimages\\" * f * q)
                    xx = get(OKs, lowercase(f), "SSSSS")
                    mismatch = ""
                    if (xx != "SSSSS") && (RRead(dir * f, "raw") != RRead(xx, "raw")) 
                        mismatch = " " * HI_RED * "MISMATCH!" * HI_END
                    end
                    println("\\Oldimages\\" * f * q * " ?? " * dir * f  * " ?? " * get(OKs, lowercase(f), HI_AQUA * xx * HI_END) * mismatch) 
                    ok = true
                    
                    continue
                end
                cp(dir * f, "\\Oldimages\\" * f * q)
                mt = stat(dir * f)
#println("    " * dir * f * "     " , string(stat(dir *  f).mtime))
                #println("OK    ", dir * f , "   ",  "\\Oldimages\\" * f * q)
                OKs[f] = lowercase(dir * f)
                push!(AGOs,[stat(dir * f).mtime, dir*f])
                ok = true
            catch ex
                local s = string(ex)  
                if length(s) > 200
                    s = s[1:200] * "..."
                end
                println("Failure: ", dir * f * "  " * typeof(ex), ".\n", s);
                ok = true 
            end
        end
        global ix += 1
   end
end

foreach(rm, filter(contains("."), readdir("/OLDIMAGES",join=true))) # What a shit!


RD(BASE)

LIST1 = []
LIST2 = []
global USED = 0


AGOss = sort(AGOs,by = x -> x[1], rev = true)
for item in AGOss
    xxx = item[2][12:length(item[2])]
    xxxx = ""
    xxxxx = ""
    if (a = get(C9IMAGDICT[2], xxx, "NUTHIN"))  != "NUTHIN"
        xxxx = " <span class=\"bZ\">" * replace(replace(replace(string(a), r"Dict{String, (Int32|Any)}" => ""), r"[()]" => ""), r"\" => (\d+)," => s"\"&nbsp;=>&nbsp;\1,") * "</span>"
        xxxxx = " <span class=\"bZ\">USED</span>"
        global USED += 1        
        for u in keys(a)
            if !(u in USERS)
                push!(USERS, u)
            end
        end 
    end
    push!(LIST2, replace(string(Dates.unix2datetime(item[1])), r"T(\d{2}):(\d{2}).*" => s" \1:\2") * 
            "</td><td style=\"vertical-align:top;\">" * xxxxx * "<a href=\"" * item[2] * "\" class=\"bY\">"  * xxx * "</a></td><td>&nbsp;</td><td style=\"vertical-align:top;\">" * xxxx)
end

AGOst = sort(AGOs, by = x -> lowercase(x[2]))
for item in AGOst
    xxx = item[2][12:length(item[2])]
    xxxx = ""
    xxxxx = ""
    if (a = get(C9IMAGDICT[2], xxx, "NUTHIN"))  != "NUTHIN"
        xxxx = " <span class=\"bZ\">" * replace(replace(replace(string(a), r"Dict{String, (Int32|Any)}" => ""), r"[()]" => ""), r"\" => (\d+)," => s"\"&nbsp;=>&nbsp;\1,") * "</span>"
        xxxxx = " <span class=\"bZ\">USED</span>"
    end
    push!(LIST1, replace(string(Dates.unix2datetime(item[1])), r"T(\d{2}):(\d{2}).*" => s" \1:\2") * 
            "</td><td style=\"vertical-align:top;\">" * xxxxx * "<a href=\"" * item[2] * "\" class=\"bY\">"  * xxx * "</a></td><td>&nbsp;</td><td>" * xxxx)
end
#println("\n\n")

global junk10 = open("JUNK10.html", "w")
write(junk10, "<!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     .bZ {\n        color:darkred;\n        font-weight:bold;\n        padding:2px 0px 0px 0px;\n        font-family:sans-serif;\n    }\n    .bY {\n        text-decoration:none;\n        white-space:nowrap;\n        color:black;\n        font-family:sans-serif;\n        font-weight:bold;\n        padding:2px 0px 0px 12px;\n        font-size:18px;\n    }\n    .bX {\n        white-space:nowrap;\n        padding:2px 0px 0px 12px;\n        color:blue;\n        font-family:sans-serif;\n        font-style:italic;\n        font-weight:bold;\n        font-size:16px;\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")

write(junk10, "<p id=\"Top\" style=\"font-size:20px;font-family:sans-serif;color:black;font-weight:bold;\">c9images v" * VERSION * "&nbsp; [" * C9IMAGDICT[1] * "]<br>USED: " * string(USED) * ". &nbsp;Total: " * formatFixed(length(LIST1)) * ". &nbsp;&nbsp;<a href=\"#Chrono\" style=\"font-weight:bold;font-family:sans-serif;\">Chronological order</a></p>\n" * 
        "<table\n    class=\"tbl\" style=\"margin:20px 0px 20px 20px;\">\n")    

for i = 1 : length(LIST1)
    write(junk10, "<tr><td class=\"bX\" style=\"vertical-align:top;\">" * LIST1[i] * "</td></tr>\n")
end
write(junk10, "<tr id=\"Chrono\"><td colspan=4 style=\"padding:22px 0px 24px 0px;\"><span style=\"font-size:24px;font-weight:bold;\">&mdash;&mdash; Chronological order (<a href=\"#Top\" style=\"font-style:italic;font-weight:normal;\">Top</a>) &mdash;&mdash;</span></td></tr>\n")

for i = 1 : length(LIST1)
    write(junk10, "<tr><td class=\"bX\" style=\"vertical-align:top;\">" * LIST2[i] * "</td></tr>\n")
end

global U = ""
Uc = ""

for u in sort(USERS, by = x -> lowercase(x))
    global U *= Uc * "<a href=\"" * u * "\" style=\"text-decoration:none;font-family:sans-serif;font-weight:bold;\">" * u * "</a>"
    global Uc = ", "
end

write(junk10,"</table>\n<p style=\"font-family:sans-serif;font-weight:bold;font-size:20px;\">Users:&nbsp; " * U * ".</p>")

write(junk10, "\n</div>\n<div style=\"width:100%;margin:60px 0px 20px 0px;text-align:right;\"><div class=\"floatright\"><a href=\"https://validator.w3.org/\" title=\"This page has been validated as HTML 5.\"><img alt=\"This page has been validated as HTML 5.\" src=\"w/images/c/ca/HTML5V.png\" style=\"width:80px;height:29px;padding-right:50px;\"></a></div>\n</div></body>\n</html>")
close(junk10)

println("ix: ", ix - 1)

