#!/bin/sh

# configuration is being done in a script because starting with GCC-3.2
# the compiler flags are changing too much between minor releases to detect
# with Makefile scripts alone.  For now it just tells you if you have the
# prerequisite compilers.

ERROR=0
TOPDIR=`pwd`




# test for nasm
if [ -x /usr/bin/nasm -o -x /usr/local/bin/nasm ]; then HAVE_NASM=y; else HAVE_NASM=n; fi

# test for yasm
if [ -x /usr/bin/yasm -o -x /usr/local/bin/yasm ]; then HAVE_YASM=y; else HAVE_YASM=n; fi

if [ `uname -m` = i686 ];
then
	if [ $HAVE_NASM = n ]; 
	then echo " *** Nasm is required.  Download it from nasm.sourceforge.net"; 
	ERROR=1
	fi
fi

if [ $HAVE_YASM = n ]; 
then echo " *** Yasm is required.  Download it from www.tortall.net/projects/yasm/"; 
ERROR=1
fi

# test for -msse support

rm -f a.out
cat > conftest.c << EOF
int main()
{
	return 0;
}
EOF

gcc -msse conftest.c > /dev/null 2>&1

if [ -x a.out ]; then HAVE_GCC=y; else HAVE_GCC=n; fi

rm -f a.out conftest.c

if [ $HAVE_GCC = n ]; 
then echo " *** GCC 3.2.2 or greater is required.  Download it from gcc.gnu.org"; 
ERROR=1
fi

if [ $ERROR = 1 ];
then exit;
fi

# configure them all
# Requires some 

# version 2.0 of faad2 produced defective Makefile
LIBFAAD_PATH=`expr thirdparty/faad2*/`

echo "CONFIGURING FAAD"
cd $LIBFAAD_PATH && ./configure && sed 's/       /\t/' Makefile > Makefile2 && mv -f Makefile2 Makefile && cd ../..
#cd $LIBFAAD_PATH && ./configure && cd ../..

if [ $? -ne 0 ]; then echo "Giving up and going to a movie."; exit 1; fi

echo "CONFIGURING FAAC"
cd thirdparty/faac* && ./configure && cd ../..

if [ $? -ne 0 ]; then echo "Giving up and going to a movie."; exit 1; fi

echo "CONFIGURING FFMPEG"
# Currently using the last version which works.  The latest version doesn't.
# For the latest version,
# delete faad tests in configure so it passes before faad is compiled.
# Change -O3 to -O2 to fix compiler errors.
# 030209
#cd thirdparty/ffmpeg* && ./configure --enable-pthreads --enable-libfaad --enable-gpl --disable-ffplay --extra-cflags=-I$TOPDIR/$LIBFAAD_PATH/include --extra-libs=-L$TOPDIR/$LIBFAAD_PATH/libfaad/.libs/ --disable-ffmpeg --disable-ffserver --disable-ffplay  && cd ../..
# 052906
cd thirdparty/ffmpeg* && ./configure --enable-pthreads --enable-gpl --disable-ffplay --extra-cflags=-I$TOPDIR/$LIBFAAD_PATH/include --extra-libs=-L$TOPDIR/$LIBFAAD_PATH/libfaad/.libs/ --disable-ffserver --disable-ffplay  && cd ../..

if [ $? -ne 0 ]; then echo "Giving up and going to a movie."; exit 1; fi

echo "CONFIGURING X264"
# Currently using the last version which works with mplayer.
# To get the latest version to work, comment out the exit after No suitable assembler found.
# add asm=no and AS= in place of exit
cd thirdparty/x264* && ./configure --enable-pthread && cd ../..

if [ $? -ne 0 ]; then echo "Giving up and going to a movie."; exit 1; fi

echo "CONFIGURING LIBDV"
cd thirdparty/libdv* && ./configure --disable-gtk --enable-shared=no && cd ../..

if [ $? -ne 0 ]; then echo "Giving up and going to a movie."; exit 1; fi

echo "CONFIGURING LIBOGG"
cd thirdparty/libogg* && ./configure && cd ../..

if [ $? -ne 0 ]; then echo "Giving up and going to a movie."; exit 1; fi

echo "CONFIGURING LIBVORBIS"
LIBOGG_PATH=`expr thirdparty/libogg*` && \
cd thirdparty/libvorbis* && \
CFLAGS="-I$TOPDIR/$LIBOGG_PATH/include -L$TOPDIR/$LIBOGG_PATH/src/" ./configure --disable-oggtest --enable-shared=no && \
cd ../..

if [ $? -ne 0 ]; then echo "Giving up and going to a movie."; exit 1; fi



if [ `uname -m` = i686 ];
then

echo "CONFIGURING JPEG-MMX"
cd thirdparty/jpeg-mmx* && ./configure && cd ..

fi


# success
if [ $ERROR = 0 ];
then echo "Configured successfully.  Type 'make' to build it.";
fi



