#!/usr/bin/env bash
# For use with gemrepl --format=raw

gravepath=/var/gemini/gemgrave/

shopt -s nullglob
LANG=en_GB.UTF-8

function putline () {
    echo -e "$*\r"
}

function fail () {
    putline "40 $1"
    exit 0
}

function end () {
    putline "20 text/gemini"
    putline "$1"
    exit 0
}

function getinput () {
    local patt="$1"
    local line
    while true; do
        echo -n "<" >& 3
        read -r line
        echo -n ">" >& 3
        if [ -n "$patt" ] && [[ "$line" != $patt ]]; then
            echo -e "10 Please input $patt\r" >&2
        else
            break
        fi
    done
    echo "$line"
}

putline "20 text/gemini"
putline "# Gemini graveyard"
putline "Think of a well-known dead person. A real one, with an entry in the English Wikipedia. Enter the graveyard as your chosen deader, and I will ask yes/no questions to try to find your plot. You win if you're not in there -- then I'll ask you some questions so we can inter you properly."
putline
putline "=> ? Enter"
getinput >/dev/null


node=
while true; do
    nodefile="$gravepath/node_$node"
    leaffile="$gravepath/leaf_$node"
    leafname=
    leafwkurl=
    if [ -r "$nodefile" ]; then
        read -r q < "$nodefile"
        putline "20 text/gemini"
        putline "$q"
        putline
        putline "=> ?y Yes"
        putline "=> ?n No"
        putline "=> ?q Quit"
        a="$(getinput "[ynq]")" 2>&1
        [ "$a" = q ] && end "Bye then."
        node="$node$a"
        continue
    elif [ -r "$leaffile" ]; then
        { read -r leafname; read -r leafwkurl; } < "$leaffile"
        putline "20 text/gemini"
        putline "Are you $leafname?"
        putline
        putline "=> ?y Yes, that's me."
        putline "=> ?n No, I am someone else."
        a="$(getinput "[yn]")" 2>&1
        if [ "$a" = y ]; then
            putline "20 text/gemini"
            putline "Yes, we have you already. Here you lie."
            putline "=> $leafwkurl Wikipedia entry for $leafname"
            putline
            putline "=> ?r Pick a new deader and play again."
            putline "=> ?q Quit."
            a="$(getinput "[rq]")" 2>&1
            [ "$a" = q ] && end "Bye then."
            node=
            continue
        fi
    fi

    putline "10 OK, you win! What is your name?"
    name="$(getinput)"
    [ -z "$name" ] && end "Never mind."

    wkurl=
    while [[ "$wkurl" != https://en.wikipedia.org/wiki/* ]] ||\
        ! curl -fLs "$wkurl" >& /dev/null; do
            putline "10 Please enter the url of your english wikipedia page (https://en.wikipedia.org/wiki/...)"
            wkurl="$(getinput)"
    done
    if [ -n "$leafname" ]; then
        putline "10 What yes/no question should I have asked to differentiate you from $leafname? (Please make the question as broad as possible.)"
        q="$(getinput)"

        y=
        while [[ "$y" != [yYnNqQ]* ]] ; do
            putline "10 Which answer would be correct for $name? [y/n/q]"
            y="$(getinput)"
        done
        if [[ "$y" == [yY]* ]]; then
            newleaf="${leaffile}y"
            oldleaf="${leaffile}n"
        elif [[ "$y" == [nN]* ]]; then
            newleaf="${leaffile}n"
            oldleaf="${leaffile}y"
        else
            end "Bye then."
        fi
        {
            flock -s 9
            ( [ -e "$nodefile" ] || [ -e "$newleaf" ] || [ -e "$oldleaf" ] ) && fail "Sorry, someone seems to have beaten you to this plot!"
            echo "$q" > "$nodefile"
            echo "$name" > "$newleaf"
            echo "$wkurl" >> "$newleaf"
            mv "$leaffile" "$oldleaf"
        } 9>"$gravepath/lockfile"
    else
        {
            flock -s 9
            [ -e "$leaffile" ] && fail "Sorry, someone seems to have beaten you to this plot!"
            echo "$name" > "$leaffile"
            echo "$wkurl" >> "$leaffile"
        } 9>"$gravepath/lockfile"
    fi
    putline "20 text/gemini"
    putline "Congratulations, $name has been successfully interred."
    putline
    putline "=> ?r Pick a new deader and play again"
    putline "=> ?q Quit"
    a="$(getinput "[rq]")" 2>&1
    [ "$a" = q ] && end "Bye then."
    node=
done