using Random
include("WBInclusions")

Version = "026"
if length(ARGS) == 0
    println("\nTzMulti v" * Version * ". Input:  [seedNumber | \"x\"] [text | *filename]\n\n" *
        "    * Take a newspaper.\n    * Take some scissors.\n    * Choose from this paper an article the length you want to make your poem.\n" *
        "    * Cut out the article.\n    * Next carefully cut out each of the words that make up this article and\n              put them all in a bag.\n" *
        "    * Shake gently.\n    * Next take out each cutting one after the other.\n    * Copy conscientiously in the order in which they left the bag.\n" *
        "    * The poem will resemble you.\n    * And there you are--an infinitely original author of charming sensibility,\n" *
        "              even though unappreciated by the vulgar herd.\n    * (Tristan Tzara)")
    exit(16)
end

global ARGS2 = ARGS[2]

if ARGS2[1:1] == "*"
    global ARGS2 = RRead(ARGS2[2 : length(ARGS2)], "asis")
end
    
global Data0 = split(replace(replace(replace(replace(replace(strip(ARGS2),
 
        r"[\r\n]" => " "),
        r"\-\-+" => ""),
        r"[^a-zA-Z0-9'\- ]" => ""),
        r"  +" => " "),
        r" (Mr|Mrs|Mx|Dr|Prof|Messrs) " => s" \1. "),
      # r" ( '|' )" => " "),

" ")

global CHARS = 0
for itm in Data0
    global CHARS += length(itm)
end

global Data = copy(Data0)
global DataJoin = join(Data0, " ")

if (global Seed = tryparse(Int32, ARGS[1])) == nothing
    global Seed = 0
    for chr in DataJoin
        global Seed += Int32(chr)
    end
end

global Fortuna = rand(MersenneTwister(Seed), UInt64, length(Data))

global Out = ""

for ix = 1 : 999999
    len = length(Data)
    if len == 1
        break
    end
    thisIndex = (Fortuna[ix] % len) + 1
    global Out *= Data[thisIndex] * " "
    deleteat!(Data, thisIndex)
end
global Out *= Data[1]

println("\n  TzMulti v" * Version * " Seed: " * string(Seed) * "  (" * string(CHARS) * ":" * string(length(Data0)) * 
            ")\n\n  " * hilite(HI_YELLOW, DataJoin))

println("\n  " * hilite(HI_RED, Out))