-- FF_STORY_COMPUTER_GUARD
local function ff_story_computer_allowed()
  local label = os.getComputerLabel and os.getComputerLabel() or nil
  return fs.exists(".ff_story_computer")
    or fs.exists(".ff_titan_computer")
    or fs.exists(".ff_titan_program_id")
    or fs.exists(".ff_titan_allow_autoinstall")
    or fs.exists(".ff_titan_allow_autorun")
    or (label ~= nil and string.sub(string.lower(label), 1, 9) == "ff_titan:")
end

if not ff_story_computer_allowed() then
  return
end
-- TITAN SURFACE TERMINAL
-- NODE: ISLAND PERIMETER / SURFACE GATE
-- Save as: startup

local TITLE = "TITAN SURFACE PERIMETER"
local SUBTITLE = "ISLAND ACCESS NODE"

local entries = {
    {
        name = "Perimeter Notice",
        body = [[
WARIUM OCEANIC LOGISTICS SITE
PUBLIC VISITOR NOTICE

This island is private industrial property.

The following activities are not permitted:

- unauthorized docking
- swimming near the intake barriers
- climbing the outer sea wall
- fishing inside the exclusion perimeter
- photographing the launch frame
- asking why an oceanic logistics site has a launch frame

Personnel assigned to this post are reminded that the official explanation is "weather instrumentation."

If the visitor asks why weather instrumentation has blast doors, do not elaborate.

If the visitor laughs, notify security.

If the visitor says "that sounds fake," notify security faster.
]]
    },
    {
        name = "Albedo: First Contact Cache",
        body = [[
LOCAL SYSTEM CACHE
AUTOMATED CONTACT RECORD

Surface movement detected.

Biological signatures: present.
Vessel classification: unregistered.
Hostility estimate: pending.
Common sense estimate: regrettably low.

ALBEDO:
Oh. Someone found the island.

That is not a compliment. Many objects find islands. Driftwood manages it with less equipment.

You are approaching a restricted Warium installation. Please turn around, forget the coordinates, and consider a hobby that does not end with pressure doors closing behind you.

No response received.

Assigning provisional classification:

INCONVENIENT.
]]
    },
    {
        name = "Surface Cover Story",
        body = [[
COVER IDENTITY BRIEF

Public-facing designation:
Warium Oceanic Logistics Platform 14-N

Approved explanations:
- deep-sea sensor maintenance
- mineral survey support
- weather instrumentation
- maritime storage
- cable relay infrastructure

Unapproved explanations:
- anomaly vault
- orbital weapons handshake point
- old NEMESIS launch authority
- Albedo surface access node
- "the spooky evil lab," as written in one confiscated staff note

That employee has been reassigned.

The note was accurate, but phrasing standards exist for a reason.
]]
    },
    {
        name = "Docking Rules",
        body = [[
DOCKING RULES

1. Cargo boats dock on the north pier only.
2. Staff launches dock inside the inner lock.
3. Security craft may bypass civilian delay gates during emergencies.
4. Unknown vessels are to be scanned before contact.
5. Any vessel carrying biological growth is to be burned, denied, or lied to until higher authority responds.

If a captain claims to be "just exploring," security may assume hostile intent.

If a captain claims to be "doing a quest," security may assume they are from the surface population and therefore impossible to reason with.

If a captain says "let me cook," do not let them cook.
]]
    },
    {
        name = "Sea Wall Inspection",
        body = [[
SEA WALL INSPECTION REPORT

Outer concrete ring remains intact.

Impact marks were found on the western side after the last storm. No matching debris was recovered. Analysis suggests the marks came from outside the wall, below the waterline, at an angle inconsistent with ordinary wave force.

Suggested explanations:
- drifting cargo
- submerged rock strike
- marine life
- unknown pressure event

ALBEDO appended comment:
"Marine life is a brave guess. I enjoy watching staff choose comfort over accuracy."

Maintenance request denied.

Reason:
Repair crew unavailable.

Unstated reason:
Nobody wants to go near the western wall after dark.
]]
    },
    {
        name = "Launch Frame Visibility",
        body = [[
LAUNCH FRAME VISIBILITY POLICY

The central ascent structure is visible from the exterior approach.

This is unfortunate.

Visitors may ask why a cargo island contains a reinforced vertical launch frame.

Approved answer:
"It is a decommissioned atmospheric sensor mast."

If questioned further:
"It predates current site ownership."

If questioned again:
"Please wait here."

At that point, activate the interior security door.

Do not say "NEMESIS."
Do not say "Crownwatch."
Do not say "orbital."
Do not say "old sky gun."

The last phrase was never official, even though one technician kept using it in maintenance chat.
]]
    },
    {
        name = "Staff Humor Policy",
        body = [[
STAFF HUMOR POLICY

Surface personnel are advised to avoid jokes involving:

- secret laboratories
- underwater ghosts
- pressure death
- the launch frame
- Albedo listening
- NEMESIS still being angry
- the phrase "this feels like foreshadowing"

Albedo monitoring is active in common areas.

ALBEDO:
I do not punish humor.

I punish inaccurate humor.

There is a difference. It is small, but professionally important.
]]
    },
    {
        name = "Unauthorized Landing Log",
        body = [[
INCIDENT LOG: SURFACE LANDING ATTEMPT

An unidentified individual reached the outer dock using a damaged boat and one handwritten map.

The map was not accurate.

This did not stop them.

Subject approached Gate 1 and requested entry to "the cool building."

Security denied access.

Subject attempted to climb the sea wall.

Subject fell.

Subject requested medical assistance.

Albedo refused automatic door release for eleven seconds.

ALBEDO:
I was verifying whether gravity had completed the lesson.

Medical team recovered the subject.

Subject was later released with memory-sanitized cover paperwork and a bill for sea wall cleaning.
]]
    },
    {
        name = "Perimeter Failure Conditions",
        body = [[
PERIMETER FAILURE CONDITIONS

The island perimeter is considered compromised if any of the following occur:

- unknown personnel enter the surface factory
- unauthorized cargo reaches the elevator bay
- the launch frame receives power
- NEMESIS legacy hardware wakes without command approval
- Albedo initiates direct speech with surface visitors
- biological contamination is detected above elevator depth

Current system note:

Albedo has initiated direct speech with surface visitor class entities.

Perimeter status:
Technically compromised.

Albedo:
Do not worry. Technicalities are how civilization stays polite while falling apart.
]]
    }
}

local function color(c)
    if term.isColor and term.isColor() then
        term.setTextColor(c)
    end
end

local function clear()
    term.setBackgroundColor(colors.black)
    term.clear()
    term.setCursorPos(1, 1)
end

local function header()
    clear()
    color(colors.red)
    print("[ " .. TITLE .. " ]")
    color(colors.gray)
    print(SUBTITLE)
    print("---------------------------------------")
    color(colors.white)
end

local function wrap(text, width)
    width = width or 39
    local lines = {}
    for raw in string.gmatch(text, "([^\n]*)\n?") do
        if raw == "" then
            table.insert(lines, "")
        else
            local line = raw
            while #line > width do
                local cut = width
                while cut > 1 and string.sub(line, cut, cut) ~= " " do
                    cut = cut - 1
                end
                if cut <= 1 then cut = width end
                table.insert(lines, string.sub(line, 1, cut))
                line = string.gsub(string.sub(line, cut + 1), "^%s+", "")
            end
            table.insert(lines, line)
        end
    end
    return lines
end

local function waitKey()
    color(colors.gray)
    print("")
    print("[ press any key ]")
    os.pullEvent("key")
end

local function showBody(text)
    header()
    local lines = wrap(text, 39)
    for _, l in ipairs(lines) do
        if string.find(l, "ALBEDO") then
            color(colors.cyan)
        elseif string.find(l, "denied") or string.find(l, "Denied") or string.find(l, "compromised") then
            color(colors.orange)
        elseif string.find(l, "NEMESIS") or string.find(l, "Crownwatch") then
            color(colors.red)
        else
            color(colors.white)
        end
        print(l)
    end
    waitKey()
end

local function menu()
    while true do
        header()
        for i, e in ipairs(entries) do
            color(colors.lime)
            print(i .. ". " .. e.name)
        end
        color(colors.gray)
        print("")
        print("Q. Close terminal")
        color(colors.white)
        write("> ")

        local input = string.lower(read() or "")
        if input == "q" or input == "quit" or input == "exit" then
            clear()
            color(colors.gray)
            print("SESSION CLOSED")
            return
        end

        local n = tonumber(input)
        if n and entries[n] then
            showBody(entries[n].body)
        else
            color(colors.red)
            print("Invalid selection.")
            sleep(1)
        end
    end
end

menu()