#!/bin/lua
-- pumpkin - print a random jack-o'-lantern, optionally with a speech bubble.
--
-- Usage:
--   pumpkin            print a random jack-o'-lantern
--   pumpkin "text"     the pumpkin says the text in a speech bubble
--   pumpkin -h         show this help
--
-- Pure ASCII on purpose: the J2ME fonts do not guarantee any Unicode glyph.

os.setproc("name", "pumpkin")

local PHRASES = {
    "Happy Halloween!",
    "Trick or treat!",
    "The candy is mine.",
    "Beware the pumpkin.",
    "One night a year...",
    "Something is watching you."
}

-- Eyes are one character each; the mouth is always five characters wide.
local FACES = {
    { "^", "^", "\\___/" },
    { "o", "o", "\\___/" },
    { ">", "<", "\\/^\\/" },
    { "-", "-", "\\_._/" }
}

local DASHES = "____________________________________________________________"
local SPACES = "                                                            "

local function spaces(n)
    if n <= 0 then return "" end
    return string.sub(SPACES, 1, n)
end

local function dashes(n)
    if n <= 0 then return "" end
    return string.sub(DASHES, 1, n)
end

-- Greedy word wrap. string.format/rep are not available in this runtime.
local function wrap(text, width)
    local words = string.split(text, " ")
    local lines = {}
    local line = ""

    for i = 1, #words do
        local word = words[i]

        if word ~= "" then
            if line == "" then
                line = word
            elseif #line + 1 + #word <= width then
                line = line .. " " .. word
            else
                table.insert(lines, line)
                line = word
            end
        end
    end

    if line ~= "" then table.insert(lines, line) end
    if #lines == 0 then lines[1] = "" end
    return lines
end

local function bubble(text)
    local lines = wrap(text, 34)
    local width = 0

    for i = 1, #lines do
        local n = #lines[i]
        if n > width then width = n end
    end

    print(" " .. dashes(width + 2))
    for i = 1, #lines do
        local line = lines[i]
        print("| " .. line .. spaces(width - #line) .. " |")
    end
    print(" " .. dashes(width + 2))
end

local function art(eyeLeft, eyeRight, mouth)
    return {
        "        _",
        "       / \\",
        "      |   |",
        "   .--'---'--.",
        "  /  " .. eyeLeft .. "    " .. eyeRight .. "  \\",
        " |   " .. mouth .. "   |",
        "  \\          /",
        "   '---------'"
    }
end

local function usage()
    print("pumpkin [text]")
    print("  Print a random jack-o'-lantern. With text, the pumpkin says it")
    print("  inside a speech bubble.")
end

local args = {}
for i = 1, #arg do args[#args + 1] = arg[i] end

if #args == 1 and (args[1] == "-h" or args[1] == "--help") then
    usage()
    return 0
end

local text = string.trim(table.concat(args, " "))
local face = FACES[math.random(#FACES)]

if text == "" then text = PHRASES[math.random(#PHRASES)] end
bubble(text)

local lines = art(face[1], face[2], face[3])
for i = 1, #lines do print(lines[i]) end

return 0
