#using Pkg; Pkg.add("HTTP")

include("WBInclusions")
include("w/DICT")

APFS = "120" # WBafter:APFS

# Get images!
global k = keys(D)
global IMGarry = []

for ky in k
    x = D[ky]
    y = "w/images/" * x[1] * "/" * x[2] * "/" * ky
    z = string(Dates.unix2datetime(mtime(y)))
    push!(IMGarry, z[1:10] * " " * z[12:16] * "            " * hilite(LO_GRAY, "w/images/") * x[1] * "/" * x[2] * "/" * ky)
end


global incoming = Dict{String, Array{String}}()

nowww = noww()

global arry = []
global ctr = 24
global ctrTrick = ""
if length(ARGS) > 0
    x = tryparse(Int32, ARGS[1])
    if x != nothing && x > 0
        global ctr = x
    else
        global ctrTrick = "!"
    end
end

global xtras = [
    "w/WBKountout.html",
    "z.txt",
    "w/css/WBmiracle.css",
    # "V2",
    # "V1",
]
global xtras = vcat(xtras, TrackedScripts)

global xcludeds = [
    "JUNK.html",
    "JUNK2.html",
    "JUNK6.html",
    "JUNK7.html",
    "JUNK10.html",
    "XXXTree.html",
    "XXXXTree.html",
    "XXXRTree.html"
]
    
try
    global errId = 1
    a = readdir()
    for itm in a
        if findfirst(".html", itm) == nothing || itm in xcludeds
            continue
        end 
        t0 = string(Dates.unix2datetime(mtime(itm)))
        push!(arry, t0[1:10] * " " * t0[12:16] * "    " * itm)
    end
    for itm in xtras
        tx = string(Dates.unix2datetime(mtime(itm)))
        push!(arry, tx[1:10] * " " * tx[12:16] * "        " * itm)
    end
catch ex
    s = string(ex)  
    if length(s) > 200
        s = s[1:200] * "..."
    end
    println("\n      Failure[" * string(errId) * "]: ", typeof(ex), ".\n", s);
    exit(16)
end

yxxx = tryparse(Int32, nowww[12:13]) + UTCoffset
fudge = yxxx > 23 ? " **" : ""
xxx = "00" * string(yxxx)
xxx = xxx[length(xxx) - 1: length(xxx)]

pct = formatDecimal(Dates.value(now() - APtS_End) / (86400 * 10 * 843), 2) * "%"

println("\n  WBafter v" * APFS * "     " * hilite(HI_YELLOW, 
           nowww[1:10] * " " * xxx * nowww[14:16] * " UTC" * fudge) * "(" * 
               string(UTCoffset) * "h)" * ctrTrick * " " *
               hilite(LO_GRAY, string(ctr)) * "    " * replace(replace(APtS_ET, 
                       r"d(.*)" => s"dZZZ\1"), "ZZZ" => LO_GRAY) * HI_END * " (" * pct * ")")

# On +2024.01.23 I was so discouraged/depressed that I gave up on this but next morning
# I saw how to do it and going carefully step by step, did it. Easily. No screaming.

n = string(now())
savemeDir = "/SAVEME"
Files = readdir(savemeDir)
times = []
for itm in Files
    if !startswith(itm, r"BO|ROM|bradstuff")
        continue
    end
    push!(times,mtime(savemeDir * "/" * itm))
end
sortedTimes = sort(times)
sLen = length(sortedTimes)
howLongAgo = Duree(Dates.unix2datetime(sortedTimes[sLen]), Dates.now(UTC))

println("                   " * n[1:10] * " " * n[12:16] * "               Bkup: " * 
               hilite(HI_YELLOW, "-" * howLongAgo) * "   Z: " * 
               hilite(HI_YELLOW, string(Date(now())-Date(2022,2,23))[1:3]) * " days.  " * wh())

# Check backup files for scripts
global wbFirst = true
for d in ["./", "./w/"]
    cd(d)
    a = readdir("./")
    for itm in a
        if !endswith(itm, "1") || !isfile(itm) || itm in ["DICT1", "V1", "V2", "SZ1"]
            continue
        end
        if RRead(itm, "raw") == RRead(itm[1:length(itm) - 1], "raw")
            continue
        end
        println((wbFirst ? "\n" : "") * "             WB not match: \"" * itm * "\"")
        global wbFirst = false
    end
end
cd("../")

#if RRead("WBBKUP", "raw") != RRead("WBBKUP1", "raw")
#    println((wbFirst ? "\n" : "") * "             WB not match: \"WBBKUP\"")
#    global wbFirst = false
#end    

println(" ")

global ydm = "xxxxxxxxxx"

b = open("WBLOG-TS", "r")
c = readline(b)
close(b)
push!(arry,c[1:16] * "    " * hilite(HI_AQUA, "~-------"))

println("  " * hilite(LO_GRAY, formatFixed(length(arry)) * " " * formatFixed(length(IMGarry))) *
            hilite(HI_YELLOW, "           V1, V2" * " " ^ 22) *
            hilite(LO_GRAY, "j:" * string(VERSION)))

z = sort(vcat(arry, IMGarry), rev=true)
 
for i = 1 : min(ctr,length(z))
    zi = z[i] 
    if zi[1:10] == ydm
        println("            " * zi[11:length(zi)])
        continue
    end
    global ydm = zi[1:10]
    println("  " *zi)
end
#close(y)

  