#!/bin/sh

bz="false"

BUNZIP=/usr/bin/bunzip2
TAR=@TAR@
ZCAT=@ZCAT@
WGET=@WGET@
CURL=@CURL@

if [ -f "$WGET" ] ; then
    DOWNLOAD_CMD=$WGET
else
    if [ -f "$CURL" ] ; then
        DOWNLOAD_CMD="$CURL -O"
    fi
fi

base=http://files.freeswitch.org/downloads/libs/
tarfile=$1
url=`echo $tarfile | grep "://"`

if [ `echo $tarfile | grep bz2`  ] ; then
    bz="true"
    UNZIPPER=$BUNZIP
else
    UNZIPPER=$ZCAT
fi

if [ ! -z $url ] ; then
    base=$tarfile/
    tarfile=$2
fi

if [ ! -d $tarfile ] ; then

    if [ $bz = "true" ] ; then
	uncompressed=`echo $tarfile | sed "s/\.tar\.bz2//g"`
    else 
	uncompressed=`echo $tarfile | sed "s/\.tar\.gz//g"`
	uncompressed=`echo $uncompressed | sed "s/\.tgz//g"`
    fi

    if [ ! -f $tarfile ] ; then
	rm -fr $uncompressed
	$DOWNLOAD_CMD $base$tarfile
	if [ ! -f $tarfile ] ; then
	    echo cannot find $tarfile
	    exit 1
	fi
    fi
    if [ ! -d $uncompressed ] ; then
	$UNZIPPER -c -d $tarfile | $TAR xf -
    fi
fi

exit 0