#!/bin/sh



# For Ubuntbunt, you need:
# yasm
# nasm
# libc6-dev
# g++
# build-essential
# libz-dev
# texinfo
# libpng-dev
# libglut-dev
# libxv-dev
# libasound2-dev
# libbz2-dev 

ERROR=0

# test for nasm
OBJDIR=`uname -m`
TOPDIR=`pwd`

if [ $OBJDIR = i686 ];
then

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

	if [ "$HAVE_NASM" = "n" ]; 
	then echo " *** Nasm is required."; 
	ERROR=1
	fi

fi

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

if [ $HAVE_YASM = n ]; 
then echo " *** Yasm is required."; 
ERROR=1
fi

# should test for the following packages
# libpng, zlib, X11 development packages
# texinfo, libasound2-dev, libbz2-dev
# libxcb-1.1 with LIBXCB_ALLOW_SLOPPY_LOCK


# test for videodev2.h

rm -f a.out
cat > conftest.c << EOF
#include <asm/types.h>
#include <sys/time.h>
#include <linux/videodev2.h>
int main()
{
	return 0;
}
EOF

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

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


# test for dvb

rm -f a.out
cat > conftest.c << EOF
#include <linux/dvb/dmx.h>
#include <linux/dvb/frontend.h>
int main()
{
	return 0;
}
EOF

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

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

rm -f a.out conftest.c




# 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




# test for OpenGL 2.0


rm -f a.out
cat > conftest.c << EOF
#include <GL/gl.h>
#include <GL/glext.h>
#include <GL/glu.h>
int main()
{
	glUseProgram(0);
	return 0;
}
EOF

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

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

rm -f a.out conftest.c








if [ $ERROR = 1 ];
then echo "Giving up and going to a movie."
exit 1
fi





# fix libraries
echo CONFIGURING QUICKTIME
cd quicktime* && ./configure && cd ..

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

echo CONFIGURING LIBMPEG3
cd libmpeg3* && ./configure && cd ..

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

echo CONFIGURING FFTW
cd thirdparty/fftw* && CFLAGS=-fPIC ./configure --disable-fortran && cd ../..

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

echo CONFIGURING MJPEGTOOLS
cd thirdparty/mjpegtools* && \
CFLAGS="-I$TOPDIR/quicktime/thirdparty/jpeg -L$TOPDIR/quicktime/thirdparty/jpeg -L/usr/lib/X11" \
./configure --enable-shared=no && \
cd ../..


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


echo CONFIGURING SNDFILE
cd thirdparty/libsndfile* && chmod a+x ./configure && ./configure && \
cd ../..

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

echo CONFIGURING RAW1394
cd thirdparty/libraw1394* && \
./configure --enable-shared=no && \
ln -sf src libraw1394 && \
cd ../..

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

echo CONFIGURING AVC1394
cd thirdparty/libavc1394* && \
RAW1394_PATH=`expr $TOPDIR/thirdparty/libraw1394*` && \
PKG_CONFIG_PATH=$RAW1394_PATH CFLAGS=-I$RAW1394_PATH/ LDFLAGS=-L$RAW1394_PATH/src/.libs ./configure --enable-shared=no && \
cd ../..

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

echo CONFIGURING IEC61883
RAW1394_PATH=`expr $TOPDIR/thirdparty/libraw1394*` && \
cd thirdparty/libiec61883* && \
PKG_CONFIG_PATH=$RAW1394_PATH CFLAGS=-I$RAW1394_PATH/ LDFLAGS=-L$RAW1394_PATH/src/.libs ./configure --enable-shared=no && \
cd ../..

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

echo CONFIGURING THEORA
LIBOGG_PATH=`expr $TOPDIR/quicktime/thirdparty/libogg*` && \
LIBVORBIS_PATH=`expr $TOPDIR/quicktime/thirdparty/libvorbis*` && \
cd thirdparty/libtheora* && \
PKG_CONFIG_PATH=$LIBOGG_PATH:$LIBVORBIS_PATH CFLAGS="-I$LIBOGG_PATH/include -L$LIBOGG_PATH/src -I$LIBVORBIS_PATH/include -L$LIBVORBIS_PATH/lib" ./configure --enable-shared=no && \
cd ../..

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

echo CONFIGURING FLAC
cd thirdparty/flac* && \
./configure && \
cd ../..

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

echo CONFIGURING ILMBASE
cd thirdparty/ilmbase* && \
PTHREAD_LIBS=-lpthread CFLAGS=-lpthread ./configure --prefix=$TOPDIR/thirdparty/ && \
cd ../..

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

echo CONFIGURING EXR
cd thirdparty/openexr* && \
PTHREAD_LIBS=-lpthread CFLAGS=-lpthread PKG_CONFIG_PATH=$TOPDIR/thirdparty/lib/pkgconfig LD_LIBRARY_PATH=$TOPDIR/thirdparty/lib/ ./configure --prefix=$TOPDIR/thirdparty/ --disable-ilmbasetest && \
cd ../..

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

# write configuration header
echo "Writing hvirtual_config.h"
cat > hvirtual_config.h << EOF
// Configuration file made by configure.  Don't edit.
EOF

if [ "$HAVE_VIDEO4LINUX2" = "y" ];
then 
	echo "#define HAVE_VIDEO4LINUX2" >> hvirtual_config.h
	echo "Have Video4Linux 2"
else
	echo "Don't have Video4Linux 2"
fi

if [ "$HAVE_DVB" = "y" ];
then 
	echo "#define HAVE_DVB" >> hvirtual_config.h
	echo "Have DVB"
else
	echo "Don't have DVB"
fi


if [ "$HAVE_GL" = "y" ];
then 
cat >> hvirtual_config.h << EOF 
#ifndef HAVE_GL
#define HAVE_GL
#endif
EOF
	echo "Have OpenGL 2.0"
else
	echo "Don't have OpenGL 2.0"
fi




# success
if [ "$ERROR" = "0" ];
then 
	echo "Configured successfully.  Type 'make' to build me.";
else
	echo "Error in configuration."
fi



