#!/bin/bash

echo "Warning, this script will download a new version of myfunctions.bas library file. The old version WILL BE REMOVED!!!
Do you want to continue???"
while [[ 1 ]]
do
	echo Please press y or n:
	read ANSWER
	if [[ $ANSWER == "y" ]]
	then 
		echo "You said \"y\"
Let's go!!! "
		break
	elif [[ $ANSWER == "n" ]]
	then
		echo "You said \"n\" "
		exit 0
	fi
	echo What does it mean: \"$ANSWER\" ??
done



if [[ ! -d ~/Scripts ]]
then 
	echo Creating a new foler \"~/Scripts\" ...
	mkdir ~/Scripts || exit 1
	echo Folder created
else
	echo Folder \"~/Scripts\" already exists
fi
cd ~/Scripts
if [[ -e exercises:2015_ethz_mmm:myfunctions.zip ]]
then	
	echo Removing an old library archive from the folder ...
	rm exercises:2015_ethz_mmm:myfunctions.zip || exit 1
	echo Removed successfully
fi

echo Downloading a new library archive ...
wget http://www.cp2k.org/_media/exercises:2015_ethz_mmm:myfunctions.zip &> /dev/null || exit 1
echo A new zip archive with the library has been successfully downloaded


if [[ -e myfunctions.bash ]]
then
	echo Removing the old library ...
	rm myfunctions.bash || exit 1
	echo Removed successfully
fi

echo Unzipping the new library ...
unzip exercises:2015_ethz_mmm:myfunctions.zip || exit 1
echo Unzipped successfully
chmod 755 myfunctions.bash

echo EVERYTHING IS FINE, Enjoy!

echo "--------------------------------------------------------------------------------------------------------------"
echo Hint: To load the library into the computer memory use the following command:
echo . ~/Scripts/myfunctions.bash
echo "--------------------------------------------------------------------------------------------------------------"
echo New library contains the following functions:
grep "function" myfunctions.bash | awk '{ print $2 }'
echo "To get information about what each function is doing just enter the name of the function without any argument" 
echo "--------------------------------------------------------------------------------------------------------------"

