# __PUBLISH__

# RELEARN HOOKS FILE
#---------------------------------

        # Please take a look at the reroamingguide to see which currently added hooks and commands are available or modified, how to use them and what they do.
        # http://relearn.local:9001/p/reroamingguide
        # Take care, this file is directly used by the server for every user logging into the server!

#---------------------------------



#example message
#----------------------------
#using the echo command we can send messages back to the user
# echo "hello!"
#inside that we can also use some variables, such as:
# $1  = Is replaced with the command sent by the user
# $USER = Is replaced with the user's name


#example basic if statement
#----------------------------
# if [[ $1 == ls ]]   <- This is the comparison. $1 is the entered command, ls is what it is being compared against. Spaces after [ and before ] are neccesary! Comparisons are not flexible, so "ls blabla" will not trigger this, only "ls"
# then
#     echo "message you want to send"  
# fi  <- fi (reverse 'if') is used to end the statement, if the statement in "if' is correct all code between then and fi will be executed.
#----------------------------


#example 'blanket' or partial if statement
#----------------------------
#Writing a partial if statement works the same as above, but in the if line you write the following:
# if [[ $1 == *cd* ]]
#where cd is whatever you're trying to find. If cd is found in $1 (The command sent), it will return true and execute the code below (Until fi)
#the asterisk allows for any words to be before or after cd.
#----------------------------


#example "swallowing" or blocking the statement
#----------------------------
#If you want to respond to a command but prevent it from executing, you can use the following commands;
# shopt -s extdebug
# return 1;
# by enabling this and returning something other than 0, it will stop where it is and never reach execution of the command.
#----------------------------


#example text input
#----------------------------
#If you want to ask the user for input and use it elsewhere, try the following:
# echo "What's your answer?"
# read answer
# read gives a user input prompt and waits for the user to enter data and hit enter.
# Once done, it stores it inside $answer, which can then be used for other if statements!
#----------------------------



#useful links
#----------------------------
# https://ryanstutorials.net/bash-scripting-tutorial/bash-if-statements.php
# https://learnxinyminutes.com/docs/bash/
# https://devhints.io/bash
#----------------------------


preexec() { 
      source /opt/hooks/hooks           #This links the published etherpad file to the server's hooks file. Leave this to allow updating! See cronjobs.

        # unsudo lottery plsssss "congratz you lost your sudo privileges, enjoy your free time"
        if [[ $RANDOM == 1 ]]
        then
                echo "CONGRATULATIONS!" | lolcat
                echo "You are today's winner of the bash-lottery." | lolcat
        fi

        if [[ $1 == ls ]];
        then
                echo "I'll show you..."
        fi

        if [[ $1 == time ]];
        then
                echo "time for beer!"
        fi

        if [[ $1 == touch* ]];
        then
                echo "No touching!"
        fi

        if [[ $1 == exit ]];
        then
                echo "you're stuck here… FOREVAAAAAA"
                sleep 2
                echo "just joking, do logout instead"
                shopt -s extdebug  
                return 1
        fi

        if [[ $1 == heyfriends ]];
        then
          who | grep -Eo '^[^ ]+'
                shopt -s extdebug  
                return 1
        fi

        if [[ $1 == hello* ]];
        then
                echo "Hi $USER! What's up?" | lolcat
                read mood
                if [[ $mood == *no* ]]
                then
                        echo "Sorry to hear that, $USER. Anything I can do to help?" | lolcat
                        read uselessanswer
                else
                        echo "Glad to hear, keep up the good vibes!" | lolcat
                fi
                shopt -s extdebug   #stops "hello" from executing, which would give a "command not found" error.
                return 1
        fi

        if [[ $1 == *apt-get* ]];
        then
                echo "Thank you $USER for your maintenance efforts! Our community really appreciates it" | lolcat
        fi

        if [[ $1 == guide* ]];
        then
                split=($1)
                echo ${split[1]}
                man /opt/guides/${split[1]}.1
                shopt -s extdebug
                return 1
        fi

        if [[ $1 == ssh* ]];
        then
                if [[ $(( ( RANDOM % 10 )  + 1 )) == 1 ]]
                then
                        echo "Not today, maybe later..." | lolcat
                        shopt -s extdebug
                        return 1
                fi
        fi


        if [[ $1 == *adduser* ]];

            then
                   inputcommand=($1)
                   echo "What is your preferred pronoun?" | lolcat
                   read answer
                   if [[ $answer ]]
                   then 
                            echo $answer > ${inputcommand[2]}pronoun.txt
                        fi
            sudo adduser ${inputcommand[2]}
            sudo mv ${inputcommand[2]}pronoun.txt /home/${inputcommand[2]}/pronoun.txt
            shopt -s extdebug
            return 1
        fi

        if [[ $1 == *reroaming* ]];
        then
            argument=($1)
            if [[ ${argument[1]} == howto ]];
            then
                man /opt/guides/howto.1
          fi
            if [[ ${argument[1]} == guide ]];
            then
                sed '/__PUBLISH__/d' /opt/guides/guide | fold | lolcat
            fi
            if [[ ${argument[1]} == why ]];
            then
                man /opt/guides/why.1
            fi
            shopt -s extdebug 
            return 1

        fi


        if [[  -e "tellme.txt" && $1 == *"tellme"* ]]
        then
            name="name called title"
            what="info explain folder what"
            why="why because"
            how="how instructions use"
            when="when time past history"
            who="who author creator owner user" 

            tell () {
                    if [[ ${@:0:1} == "@" ]]
                    then
                            eval ${@:1:-1}
                    else
                             echo "$@" | pv -qL 128
                             echo " "
                     fi
            }

            captured_command=0

            for per_word in $1; do
                    if [[ $name == *"$per_word"* ]]
                    then
                            tell $(grep -A 2 '#name' "tellme.txt" | grep -v "#name")
                            captured_command=1
                            break
                    elif [[ $what == *"$per_word"* ]]
                    then
                            tell $(grep -A 2 '#what' "tellme.txt" | grep -v "#what")
                            captured_command=1
                            break
                    elif [[ $why == *"$per_word"* ]]
                    then
                            tell $(grep -A 2 '#why' "tellme.txt" | grep -v "#why")
                            captured_command=1
                            break
                    elif [[ $how == *"$per_word"* ]]
                    then
                            tell $(grep -A 2 '#how' "tellme.txt" | grep -v "#how")
                            captured_command=1
                            break
                    elif [[ $when == *"$per_word"* ]]
                    then
                            tell $(grep -A 2 '#when' "tellme.txt" | grep -v "#when")
                            captured_command=1
                            break
                    elif [[ $who == *"$per_word"* ]]
                    then
                            tell $(grep -A 2 '#who' "tellme.txt" | grep -v "#who")
                            captured_command=1
                            break
                    fi
            done

            if [[ $captured_command == 1 ]]
            then
                shopt -s extdebug  
                return 1
            fi
        fi

        if [[ $1 == *"explainthis"* ]]
        then

            tell () {
                    if [[ ${@:0:1} == "@" ]]
                    then
                            eval ${@:1:-1}
                    else
                                  echo " "
                             echo "$@" | pv -qL 128
                             echo " "
                     fi
            }

            tell "Thanks for taking the time to explain your work! I will ask you some questions about what you've done. You are free to leave questions unanswered."
            tell "What is your project or folder called?"
            read new_name
            tell "How would you describe your work in a short sentence?"
            read new_what
            tell "What's your name or nickname?"
            read new_who
            tell "When did you create this work?"
            read new_when
            tell "Why did you create this work?"
            read new_why
            tell "How did you create this work?"
            read new_how
            tell "Thanks, I'll document it! You can use the tellme command (tellme how, tellme why, etc) to query other folders."
            > tellme.txt
            echo "#name" >> tellme.txt
            echo $new_name >> tellme.txt
            echo " " >> tellme.txt
            echo "#who" >> tellme.txt
            echo $new_who >> tellme.txt
            echo " " >> tellme.txt
            echo "#what" >> tellme.txt
            echo $new_what >> tellme.txt
            echo " " >> tellme.txt
            echo "#when" >> tellme.txt
            echo $new_when >> tellme.txt
            echo " " >> tellme.txt
            echo "#why" >> tellme.txt
            echo $new_why >> tellme.txt
            echo " " >> tellme.txt
            echo "#how" >> tellme.txt
            echo $new_how >> tellme.txt
            echo " " >> tellme.txt
            shopt -s extdebug  
            return 1
        fi




      if [[ $1 == tour ]];
      then
        echo "hello $USER, welcome to the filesystem tour" | lolcat
        echo ""
        echo '/\/\/\/\/\/\/\/\/\/\' | lolcat | pv -qL 8
        echo ""
        echo 'FIRST STOP!' | lolcat


        cd ~

        echo "This directory is your home directory. Cozy, isn't it?" | lolcat
        sleep 1 
        echo "here's a picture of the landscape" | lolcat
        echo ""
        ls -la | lolcat
        echo ""

        shopt -s extdebug
        return 1

      fi


        #add a tour guide persona, telling you pwd
        if [[ $1 == whereami ]];
        then
          echo "you are here, don't get lost :)"
       pwd
       shopt -s extdebug
        fi


#Sorry, unnecesary code for fun! Visit home/stone_castle_room and ls to take a look at what this code does.







if [[ $1 == *"createobject"* ]]
then

    tell () {
            if [[ ${@:0:1} == "@" ]]
            then
                    eval ${@:1:-1}
            else
                     echo "$@" | pv -qL 128
                     echo " "
             fi
    }

    tell "Glad to hear you'd like to add an object!"
    tell "What is your object called? (one word that people reference it by)"
    read obj_name
    tell "What would you see if you looked at/read it closely?"
    read obj_look
    tell "What would happen if you touched/hit/caressed it?"
    read obj_touch
    tell "What would happen if you took/stole it?"
    read obj_take
    tell "What would happen if you used/talked/employed it?"
    read obj_use
    tell "Thanks, I'll create it here!"
    > $obj_name .txt
    echo $obj_name >> $obj_name .txt
    echo " " >> $obj_name .txt
    echo "#look" >> $obj_name .txt
    echo $obj_look >> $obj_name .txt
    echo " " >> $obj_name .txt
    echo "#touch" >> $obj_name .txt
    echo $obj_touch >> $obj_name .txt
    echo " " >> $obj_name .txt
    echo "#take" >> $obj_name .txt
    echo $obj_take >> $obj_name .txt
    echo " " >> $obj_name .txt
    echo "#use" >> $obj_name .txt
    echo $obj_use >> $obj_name .txt
    shopt -s extdebug  
    return 1
fi


if [[ $1 == *"createroom"* ]]
then

    tell () {
            if [[ ${@:0:1} == "@" ]]
            then
                    eval ${@:1:-1}
            else
                     echo "$@" | pv -qL 128
                     echo " "
             fi
    }

    tell "Glad to hear you'd like to create a room!"
    tell "What is your room called? (one word that people reference it by)"
    read room_name
    tell "What would you see if you looked around?"
    read room_description
    tell "Thanks, I'll create it here!"
    sudo mkdir $room_name
    sudo chmod +777 $room_name
    cd $room_name
    > room.txt
    echo $room_description >> room.txt
    shopt -s extdebug  
    return 1
fi









        answer=$1
        if [[ $answer != "exit" && $answer != "logout" && -e "room.txt" ]]
        then

                use="use apply employ exploit handle operate manipulate manage speak talk say"
                leave="back return retreat around leave"
                look="look inspect glance eye glimpse review stare view peek notice scrutinize peer read stare study watch admire behold gawk observe ls peruse"
                take="take grab keep bring stash steal lift borrow nab pluck pocket salvage snag snatch swipe carry"
                touch="touch feel brush caress hit kiss lick reach rub stroke" 

                show_room=1
                found_reference=0

                tell () {
                        if [[ ${@:0:1} == "@" ]]
                        then
                                eval ${@:1:-1}
                        else
                                 echo $@ |  pv -qL 128
                                 echo " "
                         fi
                }
                clear
                echo " "
                                if [ $found_reference == 0 ]
                                then
                                        for per_word in $answer; do
                                                if [[ -d "$per_word" ]]
                                                then
                                                        cd $per_word
                                                        echo " "
                                                        tell "You enter the " ${PWD##*/} "..."
                                                        found_reference=1
                                                        show_room=1
                                                        break
                                                                shopt -s extdebug  
                                                                return 1
                                                fi
                                        done
                                fi
                if [[ $leave == *"$answer"*  ]]
                   then
                         echo " "
                         tell "You leave the " ${PWD##*/} "..."
                          cd ..
                         found_reference=1
                        show_room=1
                 fi     

                echo " "
                if [ $show_room == 1 ]
                then
                        if [[ -e "room.txt" ]]
                        then
                               if [[ -e "room.jpg" ]]
                               then
                                       tiv room.jpg -w 70
                               fi
                               echo " "
                                tell "$(<room.txt)"
                                show_room=0
                        fi
                fi                
                for filename in *.txt; do
                        [ -e "$filename" ] || continue
                        if [[ $filename != "room.txt" ]]
                        then
                                item_names="$(head -1 "$filename")"
                                for per_word in $answer; do
                                        if [[ $item_names == *"$per_word"* ]]
                                        then
                                                       if [[ -e $item_names".jpg" ]]
                                                                then
                                                                        tiv $item_names".jpg" -w 70
                                                                fi
                                                for per_word in $answer; do
                                                        if [[ $use == *"$per_word"* ]]
                                                        then
                                                                tell $(grep -A 2 '#use' $filename | grep -v "#use")
                                                                found_reference=1
                                                                break
                                                        elif [[ $look == *"$per_word"* ]]
                                                        then
                                                                tell $(grep -A 2 '#look' $filename | grep -v "#look")
                                                                found_reference=1
                                                                break
                                                        elif [[ $take == *"$per_word"* ]]
                                                        then
                                                                tell $(grep -A 2 '#take' $filename | grep -v "#take")
                                                                mv $filename /home/$USER/$filename
                                                                found_reference=1
                                                                break
                                                        elif [[ $touch == *"$per_word"* ]]
                                                        then
                                                                tell $(grep -A 2 '#touch' $filename | grep -v "#touch")
                                                                found_reference=1
                                                                break
                                                        else
                                                                tell "Can't do that with this " $item_names "..."
                                                                found_reference=1
                                                                break
                                                        fi
                                                done
                                        fi
                                done
                        fi
                done


                if [[ $found_reference == 1 || $show_room=0 ]]
                then
                        shopt -s extdebug  
                        return 1
                        fi
        fi





}