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

penguin_bite () {
    cat << EOF
The penguin gnaws at your Bag of Holding, hoping for a
morsel.  Finding nothing, it takes a nip at your hand for 1
point of damage. (let "HP=HP-1").  You may avoid this damage
if you have a sword and wish to attack the penguin.

EOF
printf "Do you want to attack the penguin? y/n  "
	read RESP
	if [ "$RESP" = "y" -o "$RESP" = "Y" ]; then
        cat << EOF
You attack the penguin, quickly doing away with it with one
quick slash.  This draws the attention of the other penguins
in the room.  They band together and rush at you. You fight
your best.

EOF
        let HP=HP-5
        export HP
        if [ "$HP" -le 0 ]; then
            cat << EOF
The mob of penguins reduces your hit points to zero.  You
are slain by the mob.

EOF
            gameover
            exit 1
        else
            cat << EOF
But a mob of penguins does not tolerate oppression, and you
soon find yourself sealed in a block of ice.  You stay,
perfectly preserved for 128 years. When you are thawed, you
may continue your adventure.

EOF
        fi
	else
        let HP=HP--
        export HP
	    echo "You keep things in perspective, and do not lash out at the penguin."
	fi
}

cat << EOF
One of the birds finally notices your presence.  It turns
its head sideways, seemingly beckoning for you to come
closer.

EOF
printf "Do you get closer? y/n  "
read RESP

if [ "$RESP" = "y" -o "$RESP" = "Y" ]; then
    cat << EOF
You take a step forward, and the bird rushes toward you, its
mouth open wide.

EOF
    printf "Do you happen to have a fish? y/n  "
    read RESP

    if [ "$RESP" = "y" -o "$RESP" = "Y" ]; then
        if grep  --quiet --only-matching fish <<< $I; then
            cat << EOF

You toss the fish at the penguin,  which catches the fish
mid-air and gobbles it down.

Don't forget to remove the fish from your inventory.  Try
the following command:

export I=\$(sed "s/fish//; s/,,/,/" <<< \$I)

EOF
        else
            echo "You don't have a fish!"
            echo
            penguin_bite
        fi
   else
        penguin_bite
   fi
else
    cat << EOF
You back away from the penguin carefully.  It loses interest
in you and waddles away.

EOF

fi
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
##
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#

#
# Well that didn't work!
