#!/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.
#

#import global functions from toplevel directory
FUNCTIONS=$(pwd)
FUNCTIONS=${FUNCTIONS%entrance*}entrance/.functions
. $FUNCTIONS

roll() {
    printf "Enter a number:  "
    read NUM
    ALPHA=$RANDOM
    OMEGA=$RANDOM
    A=`echo $ALPHA | cut -b 1-2`
    O=`echo $OMEGA | cut -b 1-2`
    let "NUM=$ALPHA % $OMEGA"
    MONSTER=$RANDOM
    MONSTER=`echo $MONSTER | cut -b 1-2`
    NUM=`echo $NUM | cut -b 1-2`

    echo "The monster rolled " $MONSTER
    echo "You rolled " $NUM

    if [ -e ../../../.bless ]; then
	let "NUM=NUM+2"
	echo "+2 bonus from a mysterious wintry patron!"
    fi
}

resolve() {
    if [[ $MONSTER -eq $NUM ]]; then
        cat << EOF
You dodge an attack, but you miss your mark as a result.
Roll again...

EOF
	    roll
    fi

    if [[ $MONSTER -lt $NUM ]]; then
        cat << EOF
A hit! A palpable hit!  You have slain the spirit of the
evil wizard.

EOF
        cat << EOF > treasure
#!/usr/bin/env bash
if ! grep  --quiet --only-matching emerald <<< \$I; then
    cat << eof
You have found an *emerald*!  Add it to your inventory.

eof
else
    cat << eof
This treasure has already been taken.

eof
fi
EOF
       chmod +x treasure
       cat << EOF > platinum
#!/usr/bin/env bash
if ! grep  --quiet --only-matching platinum <<< \$I; then
    cat << eof
You have found a sack full of *platinum* coins!  Add it to
your inventory.

eof
else
    cat << eof
This treasure has already been taken.

eof

fi
EOF
        chmod +x platinum
    else
        cat << EOF
Your sword swings harmlessly through the air.  You take 5
points damage. Deduct this from your HP variable:

let HP=HP-5

EOF
        let HP=HP-5
        if [ "$HP" -le 0 ]; then
            cat << EOF
You suddenly feel cold, the life force draining from your
body.  You have been slain by the ghost of an evil wizard.

GAME OVER!

EOF
            gameover
            exit 1
        fi
	    roll
        resolve
    fi
}

cat << EOF
The room shakes, a gust of wind blasts you from nowhere.
You sense that a presence has entered the room.  The pain
you suddenly feel assures you that you are under attack by a
ghostly entity!

If you have a sword, you can attack.  Otherwise, you should
run.

EOF
printf "Do you want to attack? y/n  "
read RESP

if [ "$RESP" = "y" -o "$RESP" = "Y" ];then
    roll
    resolve
else
    cat << EOF
You wisely turn and flee.  Quickly cd to the previous
directory.

EOF
fi


















































































































































































































































































# Hmm, that didn't work.
# Remember, when files end in a *
# it means they are applications.
# Try this:
#
# ./ghost

