#!/usr/bin/env bash
#
# If you are reading this, you have wandered out of bounds
# and are reading the code that drives the game.
#
#                    Congratulations!
#
# Learning Linux is all about curiosity, so read this code and see
# if you can figure out what it does.
#
# When you're ready to continue playing the game, though, stick to
# the scrolls. If you're stuck, go to Gitlab and create an issue.
# We're happy to provide hints.
#

ITEM=trinket

function askPutBackOnAltar() {
    echo "Do you want to put the trinket back on the altar? y/n  "
    read RESP
    return $([ "$RESP" = "y" -o "$RESP" = "Y" ])
}

function askPutInInventory() {
    echo "Do you want to take the trinket for yourself? y/n  "
    read RESP
    return $([ "$RESP" = "y" -o "$RESP" = "Y" ])
}

function askPutBackOnGround() {
    echo "Do you want to put the trinket back on the ground ?"
    echo "y/n  "
    read RESP
    return $([ "$RESP" = "y" -o "$RESP" = "Y" ])
}

function putOnAltar() {
    cat << EOF
You place the trinket upon the altar.  You feel a gentle
breeze pass through the room.

EOF
    touch .bless
}

function putInInventory() {
    echo "Remember to add the trinket to your inventory!"
    echo ""
    touch .curse
}

function leaveOnGround() {
    cat << EOF
You leave the trinket where it lies.  There is a great
sadness here.  You feel compelled to move on.

EOF
    touch .curse
}

function remindRemoveInventory() {
    cat << EOF
Don't forget to remove the trinket from your inventory.  Try
the following command:

export I=$(sed "s/$ITEM,//" <<< "$I")
EOF
}

echo "In the corner is a decaying old altar of some forgotten god."

if grep --quiet "$ITEM" <<< "$I" ; then
    if askPutBackOnAltar ; then
        putOnAltar
        remindRemoveInventory
        rm -f .curse
    elif askPutBackOnGround ; then
        leaveOnGround
        remindRemoveInventory
    else
        echo -e "You keep the trinket in your inventory\n"
    fi
elif [ -f .bless ]; then
    if askPutInInventory ; then
        putInInventory
        rm .bless
    elif askPutBackOnGround ; then
        leaveOnGround
        rm .bless
    else
        echo -e "You leave the trinket on the altar\n"
    fi
elif [ -f .curse ]; then
    if askPutBackOnAltar ; then
        putOnAltar
        rm .curse
    elif askPutInInventory ; then
        putInInventory
    else
        leaveOnGround
    fi
else
    cat << EOF
There is a small trinket on the ground.  It looks religious
in nature.

EOF
    if askPutBackOnAltar ; then
        putOnAltar
    elif askPutInInventory ; then
        putInInventory
    else
        leaveOnGround
    fi
fi














































































































































































































































































# Hmm, that didn't work.
#
# Remember, when files end in a *
# it means they are applications.
# Try this:
#
# ./altar
#
# If you're not seeing the * after
# this file name, you're probably
# forgetting to use ls -F


