#!/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
    putline "$1"
    exit 0
}

function getinput () {
    local line
    echo -n "<" >& 3
    read -r line
    echo -n ">" >& 3
    echo "$line"
}

putline 20
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
    putline 20

    nodefile="$gravepath/node_$node"
    leaffile="$gravepath/leaf_$node"
    leafname=
    leafwkurl=
    if [ -r "$nodefile" ]; then
        read -r q < "$nodefile"
        putline "$q"
        putline
        putline "=> ?y Yes"
        putline "=> ?n No"
        a="$(getinput)"
        [[ "$a" == [yn] ]] || fail "Bad response"
        node="$node$a"
        continue
    elif [ -r "$leaffile" ]; then
        { read -r leafname; read -r leafwkurl; } < "$leaffile"
        putline "Are you $leafname?"
        putline
        putline "=> ?y Yes, that's me."
        putline "=> ?n No, I am someone else."
        a="$(getinput)"
        [[ "$a" == [yn] ]] || fail "Bad response"
        if [ "$a" = y ]; then
            putline 20
            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)"
            [[ "$a" == [rq] ]] || fail "Bad response"
            [ "$a" = q ] && end "Bye then."
            node=
            continue
        fi
    fi

    name=
    while [ -z "$name" ]; do
        putline "10 OK, you win! What is your name?"
        name="$(getinput)"
    done

    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
    putline "Congratulations, $name has been successfully interred."
    putline
    putline "=> ?r Pick a new deader and play again."
    putline "=> ?q Quit."
    a="$(getinput)"
    [[ "$a" == [rq] ]] || fail "Bad response"
    [ "$a" = q ] && end "Bye then."
    node=
done