#!/bin/sh
#
# Bootstrap development of mpeg4ip with GNU autoconf, automake, and libtool
# Assumes tools are available on the system
#

# Check that we can find libtool
# Frequently it hasn't been installed
if glibtool --version > /dev/null 2> /dev/null
then 
    libtool=glibtool
    if libtoolize --version > /dev/null
    then
	echo "Found link for libtoolize"
    else 
	echo "Need a link for libtoolize to glibtoolize"
	echo "Please see readme for how to make on OS X"
	exit 1
    fi
elif libtool --version >/dev/null
then libtool=libtool
else echo "libtool does not appear to be installed."
     echo "Please install it and then rerun this script."
     exit 1
fi

for tool in aclocal autoheader autoconf automake
do 
  if $tool --version >/dev/null 
  then :
  else echo "$tool does not appear to be installed."
       echo "Please install it and then rerun this script."
       exit 1
  fi
done
#
# Test for SDL
#
target_system=`uname -s`
   if sdl-config --version >/dev/null 2>/dev/null
      then
      echo "Found SDL"
   else
      echo "SDL does not appear to be installed - can't find sdl-config"
      echo "Please install it and rerun this script"
      exit 1
   fi
#
# Test for lame
#
allargs="$@"

if test $target_system = "Linux"; then

    have_lame=no
    if lame --version >/dev/null 2>/dev/null
	then
	lamever=`lame --version | tr '.' ' '`
	lameresult=`util/testlame.sh $lamever`
	if test $lameresult = "yes"; then
	    have_lame=yes
	fi
    fi
    if which faac >/dev/null 2>/dev/null; then
	have_faac=yes
    else
	have_lame=no
    fi

    if test $have_lame = "no"; then
	disabled_mp4live=no
	while test $# -gt 0;
	  do
	  if test $1 = "--disable-server"; then
	      disabled_mp4live=yes
	  else
	      if test $1 = "--disable-mp4live"; then
		  disabled_mp4live=yes
	      fi
	  fi
	  shift
	done
	if test $disabled_mp4live = no; then
	    echo "Error - we cannot detect a valid version of lame or faac and you are trying"
	    echo "to build mpeg4ip with mp4live."
	    echo "To remedy this, either reexecute this program with either the"
	    echo "--disable-mp4live or --disable-server options -OR-"
	    echo "install lame at version 3.92 or greater from http://www.mp3dev.org"
	    echo "and libfaac at verion 1.20 or greater from http://www.audiocoding.com"
	    exit 1
	fi
    fi
fi

set -x

# if src tree is created via cvs export to tar.gz
# then empty directories in the repository are pruned
# this code recreates the empty directories
# if src tree is checked out directly via cvs, this is a no-op
mkdir -p ./config || exit 1

#
# This tries to find all the aclocal files that we need
for dir in /usr/local /usr/contrib /opt/gnome /usr /opt/local
do
   if test -d $dir/share/aclocal -a `which aclocal` != $dir/bin/aclocal
   then
     ACLOCAL_FLAGS="-I $dir/share/aclocal $ACLOCAL_FLAGS"
   fi
done

pwd=`pwd`
if test $target_system != "Darwin"; then
for	dir \
in	$pwd/lib/SDLAudio 
do	cd $dir
  aclocal || aclocal $ACLOCAL_FLAGS || exit 4
  libtoolize --force
  automake --add-missing --foreign || exit 5
  autoconf || exit 6
done
fi

cd $pwd/lib/rtp
aclocal || aclocal $ACLOCAL_FLAGS || exit 3
libtoolize --force
autoheader
automake --add-missing --foreign || exit 1
autoconf || exit 1

cd $pwd 
# top level - we're using autoheader, which needs to be after aclocal  
if `aclocal $ACLOCAL_FLAGS`; then
echo
else
if `aclocal -I doc`; then
ACLOCAL_FLAGS="-I doc $ACLOCAL_FLAGS"
else
if `aclocal -I util $ACLOCAL_FLAGS`; then
ACLOCAL_FLAGS="-I util $ACLOCAL_FLAGS"
else
if `aclocal -I doc -I util $ACLOCAL_FLAGS`; then
ACLOCAL_FLAGS="-I doc -I util $ACLOCAL_FLAGS"
fi
fi
fi
fi

libtoolize --force
grep AM_PATH_GTK_2_0 aclocal.m4 || aclocal -I doc || exit 3
autoheader || exit 1
automake --add-missing --foreign || exit 1
autoconf || exit 1
[ ! -x configure ] || ./configure $allargs ACLOCAL_AMFLAGS="$ACLOCAL_FLAGS" || exit 1
set +x

# Now ready to run make
echo "ready to make"
