#!/bin/sh
################################################################
# $Id: cURL.sh 7 2007-10-23 22:16:42Z julian $
# cURL Installation Script for UnrealIRCd
# by FBSD-DEV (FreeBSD Development Projects).
# The EasyBSD source code is release under the BSD License.
################################################################

DLFOLDER="$HOME/depend"
INSTALL="$HOME/curl"
VER_CURL="7.17.0"
VER_ARES="1.4.0"
DOWNLOAD="wget"

echof() {
        echo -e "\e[1;31m`basename $0`:\e[m $1"
}

fetch() {
        file="`eval echo $1 | sed -e's,^http.*/,,'`"
        echof "downloading $file"
        $DOWNLOAD -q $1
	echof "inflating $file"
        tar xzf $file
}

if [ -d $INSTALL ]; then
        echo
        echo
        echo "*** $INSTALL already exists ***"
        echo "    Press Ctrl-C within the next 5 seconds to cancel the install"
        echo "    Else, wait, and the install will continue, but will destroy existing data"
        echo
        echo
        sleep 5
	rm -rf $INSTALL
fi

if [ -d $DLFOLDER ]; then
       echo
       echo
       echo "*** $DLFOLDER already exists ***"
       echo "   Press Ctrl-C within the next 5 seconds to cancel the install"
       echo "   Else, wait, and the install will contuniue, but will destroy exist data"
       echo
       echo
       sleep 5
       rm -rf $DLFOLDER
fi

if [ `uname` = "FreeBSD" ]; then
	if [ -z `which gmake` ]; then
	        echo
	        echo
	        echo "*** GMAKE not installed! ***"
	        echo "    Please install gmake to continue running script"
		exit
	fi
	MAKE='gmake'
	MAKEi='gmake install'
elif [ `uname` = "Linux" ]; then
	MAKE='make'
	MAKEi='make install'
else
        MAKE='make'
        MAKEi='make install'
fi

SSLFLAG=""
while [ -z "$SSLFLAG" ] ; do
        echo ""
        echo "Should libcurl be built with SSL support?"
        echo -n "-> " $c
        read cc
        case "$cc" in
                [Yy]*)
                        SSLFLAG="--with-ssl"
                        ;;
                [Nn]*)
                        SSLFLAG="--without-ssl"
                        ;;
                *)
                        echo
                        echo "You must enter either Yes or No"
                        echo
                        ;;
        esac
done

mkdir $DLFOLDER
cd $DLFOLDER

fetch http://daniel.haxx.se/projects/c-ares/c-ares-$VER_ARES.tar.gz
fetch http://curl.haxx.se/download/curl-$VER_CURL.tar.gz

echof "building c-ares"
cd $DLFOLDER/c-ares-$VER_ARES
(./configure --prefix=$DLFOLDER/ares && ${MAKE} && ${MAKEi}) >/dev/null 2>&1
if [ $? -ne 0 ]; then
	echo
	echo
	echof "Setup has failed!"
	echo "	Please report this problem -> http://bugs.fbsd-dev.org"
	exit 1
fi

mkdir $INSTALL 
mkdir $INSTALL/lib
cp $DLFOLDER/ares/lib/libcares.a $INSTALL/lib/
ln -s $INSTALL/lib/libcares.a $INSTALL/lib/libares.a
ln -s $DLFOLDER/ares/lib/libcares.a $DLFOLDER/ares/lib/libares.a

echof "building cURL"
cd $DLFOLDER/curl-$VER_CURL
CPPFLAGS="-I$DLFOLDER/ares/include" 
(./configure --prefix=$INSTALL --disable-shared --disable-thread --enable-ares=$DLFOLDER/ares --disable-ipv6 $SSLFLAG && ${MAKE} && ${MAKEi}) >/dev/null 2>&1
if [ $? -ne 0 ]; then
	echo
	echo
	echof "Setup has failed!"
	echo "	Please report this problem -> http://bugs.fbsd-dev.org"
	exit 1
fi

rm -rf $DLFOLDER

echo
echo
echo "libcurl has been installed. When running ./Config specify:"
echo "$INSTALL for the directory you installed libcurl to."

