include("WBInclusions")

if length(ARGS) < 1
    println("  Need a string to search for")
    exit(16)
end
global str = Regex(replace(replace(ARGS[1], "%%1%" => "^"), "%%2%" => ">") , "i")

global VERSION = "015"
global BASE = "/ROM/BO/c9/" 
global DIRS = 0
global DISCARDS = 0
global PROCESSED = 0
global HITS = []
global SIZE = 0
global HREFS = 0
global SRCS = 0

function RD(dir)
    global DIRS += 1
    # println("          dir: \"",dir, "\"")
    files = readdir(dir)
    for f in files
        if isdir(dir * f)
            RD(dir * f * "/")
            continue
        end
        if !endswith(f, ".html")
            global DISCARDS += 1
            continue
        end
        ff = dir * f
        b = RRead(ff, "asis")
        c = findall(str, b)
        if length(c) > 0
            fff = replace(ff[length(BASE) + 1 : length(ff)], "/" => "\\")
            push!(HITS, [ff, length(c)])
            #println("  ", fff)
        end
        global HREFS += length(findall(" href=\"", b))
        global SRCS += length(findall(" src=\"", b))
        global PROCESSED += 1
        global SIZE += length(b)
   end
end
println("")
RD(BASE)
println("  PROCESSED: ", PROCESSED, ".  DIRS: ", DIRS,
        ".  DISCARDS: " , DISCARDS, ".  > JUNK6.html.   (", formatFixed(SIZE) , "  ",
                formatFixed(HREFS), "  ", formatFixed(SRCS) * ")")

a = open("JUNK6.html", "w")
write(a, "<!DOCTYPE html>\n<html lang=\"en\" dir=\"ltr\">\n<head>\n<meta charset=\"UTF-8\">\n<title>Frustra</title>\n<style>\ntd {\n  padding:4px 20px 4px 8px;\n  vertical-align:top;\n}\na {\n  text-decoration:none;\n}\n</style>\n</head>\n<body>\n<div style=\"margin:40px 0px 80px 80px;font-family:sans-serif;font-size:19px;\">\n<table style=\"border-collapse:collapse;\">\n<tr><td colspan=2><span style=\"font-size:24px;\">" * string(str) * "</span></td></tr>\n")
global SHIT = Dict{String, Any}()
for i = 1 : length(HITS)
    hit = HITS[i]
    h = hit[1]
    pfx = length(findall("/", h)) > 4 ? "0" : "1"
    SHIT[pfx * lowercase(h)] = hit
end
global kk = []
for k in keys(SHIT)
    push!(kk, string(k))
end
kkk = sort(kk)
global PREVX = "X"
for kkkk in kkk
    shit = SHIT[kkkk]
    h = shit[1]
    hlen = shit[2]
    kkkk1 = kkkk[1:1]
    sz = kkkk1 != PREVX ? 12 : 0
    write(a, "<tr><td style=\"padding-top:" * string(sz) * "px;\"><a href=\"" * h * "\">" * h[12 : length(h)] * "</a></td><td style=\"text-align:right;padding-top:" * string(sz) * "px;\">" * string(hlen) * "</td></tr>\n")
    global PREVX = kkkk1
end
write(a,"</table></div>\n</body></html>\n")
close(a)
    