#!/bin/bash
# (c) 2004 Arthur Huillet
#This code, for what it's worth, is  "protected" by the so-called MIT licence - see COPYING.

ZL_ALLEGRO_VERSION=4.1.18
ZL_CKSUM=215b84351f349f5998a77f99bf1218fa

build_lib() {
echo
echo Untar-ring library

if [ -f allegro-$ZL_ALLEGRO_VERSION.tar.gz ]; then
	tar zxvf allegro-$ZL_ALLEGRO_VERSION.tar.gz
else
	echo Unable to find allegro-$ZL_ALLEGRO_VERSION.tar.gz file !
	exit 2
fi

cd allegro-$ZL_ALLEGRO_VERSION
pwd
./configure
make

echo If there were no errors, just su and make install !
echo Then, run this script again.

exit 1
}



askfor_build() {
echo
echo 'Do you want this script to untar and build the library for you ? [Y]/N'
read NEED_BUILD

if [ -z $NEED_BUILD ]; then
        NEED_BUILD=Y
fi

if [ $NEED_BUILD = Y ]; then
        build_lib
fi
if [ $NEED_BUILD = y ]; then
	build_lib
fi
exit 1
}


download_lib() {
echo

echo Checking for wget...

if [ -z `which wget 2>/dev/null` ]; then
        echo '[EE] Unable to find wget' 
        exit 2
fi

if [ -f allegro-$ZL_ALLEGRO_VERSION.tar.gz ]; then
	echo allegro-$ZL_ALLEGRO_VERSION.tar.gz exists, checking MD5
	FILESUM=`md5sum allegro-$ZL_ALLEGRO_VERSION.tar.gz | sed "s/  allegro-$ZL_ALLEGRO_VERSION.tar.gz//"`
	echo Checksum is : $FILESUM'<-ends'
	if [ $FILESUM = $ZL_CKSUM ]; then
		echo File seems ok
		askfor_build
	else
		echo Resuming download
		wget -c http://heanet.dl.sourceforge.net/sourceforge/alleg/allegro-$ZL_ALLEGRO_VERSION.tar.gz
		askfor_build
	fi
fi

echo Downloading file allegro-$ZL_ALLEGRO_VERSION.tar.gz with wget

wget http://heanet.dl.sourceforge.net/sourceforge/alleg/allegro-$ZL_ALLEGRO_VERSION.tar.gz
askfor_build
}


askfor_dl() {
echo 
echo This script can install the library for you,
echo do you want to proceed [Y]/N ?
read NEED_DL

if [ -z $NEED_DL ]; then
        NEED_DL=Y
fi

if [ $NEED_DL = Y ]; then
        download_lib
fi
if [ $NEED_DL = y ]; then
	download_lib
fi

exit 1
}




echo '
###### Zlog configuration script ######
'
#echo Please give feedback : ahuillet AT users DOT sourceforge DOT net. I believe in this project and I want your opinion.
ALLEG_CONFIG_PATH=`which allegro-config 2>/dev/null`


if [ $ALLEG_CONFIG_PATH ]; then
        ALLEG_VERSION=`sh $ALLEG_CONFIG_PATH --version`
        echo Allegro version is : $ALLEG_VERSION
        if [ $ALLEG_VERSION = $ZL_ALLEGRO_VERSION ]; then
                echo This allegro version should work fine. 
#		echo '
#Please note you could want to regenerate the savegame code. To do so, run "./sav_autocode.sh".'
		echo \"make linux\" builds the program \(should also work for OS X\)
		echo There is no target \"install\" in the Makefile.
        else
                echo '[EE] YOU NEED ALLEGRO' $ZL_ALLEGRO_VERSION
		echo 'You can download it here :'
		echo 'http://prdownloads.sourceforge.net/alleg/allegro-'$ZL_ALLEGRO_VERSION'.tar.gz?download'
		askfor_dl
        fi
else
        echo "[EE] Unable to detect the allegro-config script installed by the Allegro game programming library."
        echo 'You need Allegro' $ZL_ALLEGRO_VERSION 'in order to compile and run Zlog'
	echo 'You can download it here : 
	http://prdownloads.sourceforge.net/alleg/allegro-'$ZL_ALLEGRO_VERSION'.tar.gz?download'	
	askfor_dl
fi
        



