#!/bin/bash
cd /tmp
rm -rf /tmp/wikilist
rm -rf /usr/lib/wikilist
rm /usr/share/applications/wikilist.desktop 2> /dev/null
mkdir /tmp/wikilist
cd /tmp/wikilist

function updatepkglist {
	echo "Updating package list..."
	gambasversions=`dpkg -l | grep gambas | awk '{print $3}' | uniq`
	#echo $gambasversions
	if [[ "$gambasversions" = *3.5.1* ]] || [[ "$gambasversions" = *3.5.2* ]] || [[ "$gambasversions" = *3.5.3* ]] || [[ "$gambasversions" = *3.5.4* ]] || [[ "$gambasversions" = *3.6* ]]
	then
		echo "Already up-to-date."
	else
		apt-get -qq update
	fi
}
function addkeyandupdate {
	ping -c1 keyserver.ubuntu.com &> /dev/null
	if [ "$?" -ne 0 ]
	then
		echo "Trying another keyserver (keyserver.ubuntu is down)"
		apt-key adv --keyserver pgp.mit.edu --recv-keys 6CAEE58D
	else
		apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 6CAEE58D
	fi
	updatepkglist
}

yaourtpath=`which yaourt 2>/dev/null`
if [ -n "$yaourtpath" ]; then
	yaourt -S --noconfirm gambas3-ide gambas3-gb-args gambas3-gb-libxml gambas3-gb-net gambas3-gb-net-curl perl
fi
redhatversion=`cat /etc/redhat-release 2>/dev/null`
if [ -n "$redhatversion" ]; then
	echo "Installing Gambas on Red Hat. Make sure your Gambas version is >=3.5.1. This should be the case if you are using Fedora 18 or higher."
	yum -y install gambas3-ide gambas3-gb-libxml gambas3-gb-net gambas3-gb-net-curl perl
	echo "Finished installing Gambas. Your current Gambas version is: "
	gbc3 -V
fi

ubuntucodename=`lsb_release -sc`
debianversion=`lsb_release -sr`
aptgetpath=`which apt-get 2>/dev/null`
if [ -n "$aptgetpath" ]; then
	if [ "$ubuntucodename" = "trusty" ] || [ "$ubuntucodename" = "saucy" ] || [ "$ubuntucodename" = "precise" ] || [ "$ubuntucodename" = "lucid" ]
	then
		add-apt-repository -y ppa:gambas-team/gambas3
		if [ "$?" -ne 0 ]
		then
			echo "deb http://ppa.launchpad.net/gambas-team/gambas3/ubuntu $ubuntucodename main">/etc/apt/sources.list.d/gambasppasid.list
			addkeyandupdate
		else
			updatepkglist
		fi
	fi
	if [ "$ubuntucodename" = "raring" ] || [ "$ubuntucodename" = "quantal" ] || [[ "$debianversion" = 7* ]] || [ "$ubuntucodename" = "qiana" ] || [ "$ubuntucodename" = "petra" ] || [ "$ubuntucodename" = "olivia" ] || [ "$ubuntucodename" = "nadia" ] || [ "$ubuntucodename" = "maya" ]
	then
		echo "deb http://ppa.launchpad.net/gambas-team/gambas3/ubuntu saucy main">/etc/apt/sources.list.d/gambasppasid.list
		addkeyandupdate
	fi
	echo "Installing Gambas"
	apt-get -qq install gambas3-ide gambas3-gb-args gambas3-gb-libxml gambas3-gb-net gambas3-gb-net-curl perl
	#apt-get -qq install gambas3-dev
fi

#wget http://hugsmile.eu/file/wikilist/
#VERSIONWL=`cat index.html | tail -n8 | head -n1 | grep -o '<a href=['"'"'"][^"'"'"']*['"'"'"]' | sed -e 's/^<a href=["'"'"']//' -e 's/["'"'"']$//'`
VERSIONWL="0.5rc10"
echo "Installing $VERSIONWL"
#wget -nv -r -np -nH -R "index.html*" http://hugsmile.eu/file/wikilist/$VERSIONWL/autotools/
wget -nv -r -np -nH -l1 http://hugsmile.eu/file/wikilist/$VERSIONWL/autotools/ -A "WikiList*.tar.gz"
#wget -nv --accept "*.tar.gz" --level 1 http://hugsmile.eu/file/wikilist/$VERSIONWL/autotools/
cd file/wikilist/$VERSIONWL/autotools/
tar xzf W*.tar.gz
cd W*/W*
echo "The extracted tar.gz file contains:"
ls -A
rm index* 2>/dev/null

mv wikilist.png /usr/share/icons/wikilist.png
cp wikilist.desktop /usr/share/applications
mkdir /usr/lib/wikilist
mv * .[^.]* /usr/lib/wikilist
rm -rf /tmp/wikilist

echo "Compiling WikiList"
gbc3 -p /usr/lib/wikilist
if [ "$?" -eq 0 ]
then
	echo "Installation completed succesfully. You can find WikiList in your menu."
else
	gambaspath = `which gbc3`
	if [ -z "$gambaspath" ]
	then
		echo "Gambas compiler is not installed. Please check your Gambas version."
	else
		echo "Gambas compiler failed to compile WikiList. Please report this bug to the developers, including the Gambas compiler version (gbc3 -V)."
	fi
fi
