#!/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

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

# Pass our args to configure
case	`uname -s`
in	Darwin)
	rm -f server/lib/sndfile/config.* server/lib/sndfile/configure
	rm -f server/lib/sndfile/ltmain.sh server/lib/sndfile/aclocal.m4
	rm -f lib/rtp/config.guess lib/rtp/config.sub lib/rtp/configure
	;;
esac


if test -f /usr/share/libtool/config.guess
then
   CONFIG_GUESS=/usr/share/libtool/config.guess
else
   if test -f /usr/local/share/libtool/config.guess
   then
      CONFIG_GUESS=/usr/local/share/libtool/config.guess
   else
      echo "Can't find config.guess to copy"
      exit 1
   fi
fi

if test -f /usr/share/libtool/config.sub
then
   CONFIG_SUB=/usr/share/libtool/config.sub
else
   if test -f /usr/local/share/libtool/config.sub
   then
      CONFIG_SUB=/usr/local/share/libtool/config.sub
   else
      echo "Can't find config.sub to copy"
      exit 1
   fi
fi

for dir in /usr/local /usr/contrib /opt/gnome
do
   if test -e $dir/share/aclocal -a `which aclocal` != $dir/bin/aclocal
   then
     ACLOCAL_FLAGS="-I $dir/share/aclocal $ACLOCAL_FLAGS"
   fi
done

pwd=`pwd`
for	dir \
in	$pwd/server/lib/sndfile $pwd/lib/SDL $pwd/lib/rtp 
do	cd $dir
	[ ! -f Makefile.am ] || aclocal || aclocal $ACLOCAL_FLAGS || exit 3
	[ ! -f Makefile.am ] || automake --add-missing --foreign || exit 1
	[ ! -f configure.in ] || autoconf || exit 1
	[ -f config.guess ] || cp $CONFIG_GUESS . || exit 1
	[ -f config.sub ] || cp $CONFIG_SUB . || exit 1
	[ ! -x configure ] || ./configure "$@" || exit 1
done

cd $pwd 
# top level - we're using autoheader, which needs to be after aclocal  
aclocal || aclocal $ACLOCAL_FLAGS || aclocal -I util || aclocal -I util $ACLOCAL_FLAGS || exit 3
autoheader || exit 1
automake --add-missing --foreign || exit 1
autoconf || exit 1
[ -f config.guess ] || cp $CONFIG_GUESS . || exit 1
[ -f config.sub ] || cp $CONFIG_SUB . || exit 1
[ ! -x configure ] || ./configure "$@" || exit 1
set +x

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