echo "Configuring mpeg2_movie"

USE_MMX=1
CODEC_DIR=/usr/lib/mpeg2_movie/codecs/
PREFIX=/usr/local
LDSTATIC=

if [ -f video/writepic.c ]; then
	VIDEO_EXPORT_ONLY=0
else
	VIDEO_EXPORT_ONLY=1
fi
NOFIREWIRE=--no-firewire

for ac_option
do
case "$ac_option" in
	--no-mmx)
		USE_MMX=0
		;;
	--no-nuv)
		DONT_USE_NUV=1
		;;
	--no-avi)
		DONT_USE_AVI=1
		;;
	--dynamic)
		DYNAMIC_LOADING=1
		;;
	--avifile-0-6)
		AVIFILE_06=1
		;;
	--codec-dir=*)
		CODEC_DIR=`echo $ac_option|sed -e 's/.*=//'` 
		;;
	--prefix=*)
		PREFIX=`echo $ac_option|sed -e 's/.*=//'` 
		;;
	--write-yuv-only)
		VIDEO_EXPORT_ONLY=1
		echo "WARNING"
		echo
		echo "The use of the --write-yuv-only configure option is deprecated, please use"
		echo "the exportvideo make target instead (it still works but will go away rsn)"
		echo
		;;
	--firewire)
		NOFIREWIRE=
		;;
	--ld-dynamic)
		LDSTATIC=
		;;
	--ld-static)
		LDSTATIC="-static -lgmodule -lglib -lgtk -lltdl -ldl -g"
		;;

	-h | --help | -help)
	cat << EOF
Options:
	--no-mmx           Compile with no MMX support.
	--no-nuv           Compile with no nuppelvideo support.
	--no-avi           Compile with no libavifile support.
	--dynamic          Experimental dynamic plugin support for
                           avi and rtjpeg (nuv)
	--codec-dir=dir    change location of modules
			   ($CODEC_DIR)
	--prefix=dir       change install path ($PREFIX)
	--avifile-0-6      avifile 0.6 support (a few method names
	                   changed case)
	--ld-static        compile static binaries (experimental)
	--ld-dynamic       compile dynamically linked binaries (default)
EOF
		exit 0
		;;

	*)
		;;
esac
done


cat > global_config << EOF
# DO NOT EDIT.  EDIT ./configure INSTEAD AND RERUN IT.
EOF

# needed for quicktime, too?
gtk-config --version >/dev/null || {
  echo "gtk-config not found, you need gtk for quicktime and (possibly) dynamic linking!" >&2
  echo "if you cannot or really don't want to install gtk, patch out qt-support or" >&2
  echo "bug aoe until he does!" >&2
  exit 1
}
echo >> global_config "CFLAGS += `gtk-config --cflags`" 
if [ "x$LDSTATIC" != "x" ]; then 
	echo -n "LDFLAGS += " >> global_config
	gtk-config --libs|sed -e's/-ldl//' -e's/-rdynamic//' >> global_config
	echo >> global_config
else
	echo >> global_config "LDFLAGS += `gtk-config --libs`" 
fi
echo >> global_config "PREFIX += $PREFIX"

if test -z "$CFLAGS"; then
	echo >> global_config "CFLAGS += -O2 -funroll-loops -fomit-frame-pointer -malign-loops=2 -malign-jumps=2 -malign-functions=2 -I/usr/local/include"
fi

if [ "${AVIFILE_06}" != "" ]; then
	echo >> global_config "CFLAGS += -DAVIFILE_06"
	echo >> global_config "AVIFILE_06=1"
fi
if [ "${DONT_USE_NUV}" != "" ]; then
	echo >> global_config "CFLAGS += -DDONT_USE_NUV"
	echo >> global_config "DONT_USE_NUV=1"
fi
if [ "${DONT_USE_AVI}" != "" ]; then
	echo >> global_config "CFLAGS += -DDONT_USE_AVI"
	echo >> global_config "DONT_USE_AVI=1"
else 
	avifile-config --cflags >/dev/null || {
	  echo "avifile-config not found!" >&2
	  echo "try --no-avi for disabling libaviplay support" >&2
	  exit 1
	}

	if [ "${DYNAMIC_LOADING}" = "1" ]; then
		echo >> global_config "AVI_CFLAGS += `avifile-config --cflags`" 
		echo >> global_config "AVI_LDFLAGS += `avifile-config --libs`" 
	else
		echo >> global_config "CFLAGS += `avifile-config --cflags`" 
		echo >> global_config "LDFLAGS += `avifile-config --libs`" 
	fi
fi
if [ "${DYNAMIC_LOADING}" = "1" ]; then
	echo >> global_config "CFLAGS += -DDYNAMIC_LOADING"
	echo >> global_config "DYNAMIC_LOADING=1"
fi
if [ "${CODEC_DIR}" != "" ]; then
	echo >> global_config "CFLAGS += -DCODEC_DIR=\\\"$CODEC_DIR\\\""
	echo >> global_config "CODEC_DIR=$CODEC_DIR"
fi
if [ ${VIDEO_EXPORT_ONLY} -eq 1 ]; then
	echo >> global_config "CFLAGS += -DVIDEO_EXPORT_ONLY"
	echo >> global_config "VIDEO_EXPORT_ONLY=1"
fi
if [ ${USE_MMX} = 1 ]; then
	echo >> global_config "CFLAGS += -DX86_CPU"

	cat >> global_config << EOF	
MMXOBJ = \
	bdist1_mmx.o \
	bdist2_mmx.o \
	dist2_mmx.o \
	fdct_mmx.o \
	idct_mmx.o \
	mblock_sad_mmx.o \
	mblock_sad_mmxe.o \
	mblockq_sad_mmxe.o \
	mblock_sub44_sads.o \
	predcomp_mmx.o \
	predcomp_mmxe.o \
	predict_mmx.o \
	quant_mmx.o \
	quantize_x86.o
EOF

fi

if [ "x$LDSTATIC" != "x" ]; then                                
	echo >> global_config "LDFLAGS += $LDSTATIC" 
fi

echo "done"

#######################################################################
# other dirs
#######################################################################

cd libmpeg3

if [ ${USE_MMX} = 1 ]; then
	./configure --no-css
else
	./configure --no-css --no-mmx
fi

cd ../quicktime

if [ ${USE_MMX} = 1 ]; then
	./configure $NOFIREWIRE
else
	./configure $NOFIREWIRE --no-mmx
fi
