# GPAC MPEG-4 SDK configure script (c) 2003-2005 Jean Le Feuvre
#		inspired from ffmpeg configure by Fabrice Bellard (c) 2000-2002

# set temporary file name
if test ! -z "$TMPDIR" ; then
    TMPDIR1="${TMPDIR}"
elif test ! -z "$TEMPDIR" ; then
    TMPDIR1="${TEMPDIR}"
else
    TMPDIR1="/tmp"
fi

TMPC="${TMPDIR1}/gpac-conf-${RANDOM}-$$-${RANDOM}.c"
TMPH="${TMPDIR1}/gpac-conf-${RANDOM}-$$-${RANDOM}.h"
TMPCPP="${TMPDIR1}/gpac-conf-${RANDOM}-$$-${RANDOM}.cpp"
TMPE="${TMPDIR1}/gpac-conf-${RANDOM}-$$-${RANDOM}"
TMPO="${TMPDIR1}/gpac-conf-${RANDOM}-$$-${RANDOM}.o"
TMPS="${TMPDIR1}/gpac-conf-${RANDOM}-$$-${RANDOM}.S"

# default parameters
DESTDIR=""
prefix="/usr/local"
mandir=""
cross_prefix=""
dxsdk_path=""
moz_path="local"
cc="gcc"
ar="ar"
ranlib="ranlib"
make="make"
strip="strip"
cpu=`uname -m`
debuginfo="no"
sdl_path=""
sdl_local="no"
sdl_static="no"

#GPAC module config
js_flags="XP_UNIX"
js_lib="js"
iso_fragments="yes"
make_readonly="no"
has_mingw_directx="no"
has_js="no"
has_ft="no"
has_jpeg="no"
has_png="no"
has_xvid="no"
has_mad="no"
has_faad="no"
has_ffmpeg="no"
has_amr_nb_fixed="no"
has_amr_nb="no"
has_amr_wb="no"
has_ogg="no"
has_vorbis="no"
has_theora="no"
has_oss_audio="no"
has_x11_shm="no"
disable_svg="no"
no_gcc_opt="no"
use_fixed_point="no"
use_memory_tracking="no"
has_opengl="no"
has_ssl="no"
has_ipv6="no"
gprof_build="no"

win32="no"
mingw32="no"
cygwin="no"
linux="no"
freebsd="no"
darwin="no"
alt_macosx_dir=""
Mac_Applications=""
extralibs="-lm"
bigendian="no"
SHFLAGS=-shared
need_inet_aton="no"
LDFLAGS="$LDFLAGS -Wl,--warn-common"
CFLAGS=""
CPPFLAGS=""
GPAC_SH_FLAGS=-lpthread
DYN_LIB_SUFFIX="so"
X11_PATH="/usr/X11R6"
is_64="no"

#Configure Usage
if test x"$1" = x"-h" -o x"$1" = x"--help" ; then
cat << EOF

Usage: configure [options]
Options: [defaults in brackets after descriptions]

EOF
echo "GPAC configuration options:"
echo "  --help                   print this message"
echo "  --prefix=PREFIX          install in PREFIX [$prefix]"
echo "  --mandir=DIR             man documentation in DIR [PREFIX/man]"
echo ""
echo "  --source-path=PATH       path of source code [$source_path]"
echo "  --cross-prefix=PREFIX    use PREFIX for compile tools [$cross_prefix]"
echo "  --cc=CC                  use C compiler CC [$cc]"
echo "  --make=MAKE              use specified make [$make]"
echo "  --extra-cflags=ECFLAGS   add ECFLAGS to CFLAGS [$CFLAGS]"
echo "  --extra-ldflags=ELDFLAGS add ELDFLAGS to LDFLAGS [$LDFLAGS]"
echo "  --extra-libs=ELIBS       add ELIBS [$ELIBS]"
echo "  --cpu=CPU                force cpu to CPU  [$cpu]"
echo "  --sdl-cfg=SDL_PATH       specify path to sdl-config for local install [$sdl_path]"
echo "  --enable-sdl-static      use static SDL linking [default=no]"
echo "  --X11-path=X11_PATH      specify path for X11 includes and libraries [$X11_PATH]"
echo "  --dxsdk-path=DX_PATH     specify directX SDK for MinGW [$dxsdk_path]"
echo "  --mozdir=MOZ_PATH        specify mozilla main directory path for system install"
echo ""
echo "  --enable-debug           produce debug version"
echo "  --enable-gprof           enable profiling"
echo "  --track-memory           enables tracking of all memory allocated by gpac"
echo "  --disable-fragments      disable movie fragments in ISO media support"
echo "  --disable-opt            disable GCC optimizations"
echo "  --disable-ipv6           disable IPV6 support"
echo "  --disable-wx             disable wxWidgets support"
echo "  --disable-oss-audio      disable OSS audio"
echo "  --disable-x11-shm        disable X11 shared memory support"
echo "  --disable-fragments      disable movie fragments in ISO media support"
echo "  --enable-readonly        force ISO media to be read-only"
echo "  --disable-svg            disable SVG support"
echo "  --enable-fixed-point     enable fixed-point math (THIS MODIFIES GPAC MATH.H HEADER)"
echo "  --force-fixed-point      force fixed-point math without changing gpac math.h header"
echo "  --disable-opengl         disable OpenGL support"
echo "  --disable-ssl            disable OpenSSL support"
echo "  --enable-amr-nb-fixed    enable AMR NB fixed-point decoder"
echo "  --enable-amr-nb          enable AMR NB library"
echo "  --enable-amr-wb          enable AMR WB library"
echo "  --enable-amr             enable both AMR NB and WB libraries"
echo ""
echo "Extra libraries configuration. You can turn a libray off or force using the local version in gpac/extra_lib/"
echo "  --use-js=OPT             force SpiderMonkey ECMAScript OPT=[no,local]"
echo "  --use-ft=OPT             force FreeType OPT=[no,local]"
echo "  --use-jpeg=OPT           force JPEG OPT=[no,local]"
echo "  --use-png=OPT            force PNG OPT=[no,local]"
echo "  --use-faad=OPT           force FAAD OPT=[no,local]"
echo "  --use-mad=OPT            force MAD OPT=[no,local]"
echo "  --use-xvid=OPT           force XVID OPT=[no,local]"
echo "  --use-ffmpeg=OPT         force FFMPEG OPT=[no,local]"
echo ""
echo "NOTE: The object files are build at the place where configure is launched"
exit 1
fi

for opt do
  case "$opt" in
  --prefix=*) prefix=`echo $opt | cut -d '=' -f 2`
  ;;
  --mandir=*) mandir=`echo $opt | cut -d '=' -f 2`
  ;;
  --source-path=*) source_path=`echo $opt | cut -d '=' -f 2`
  ;;
  --cross-prefix=*) cross_prefix=`echo $opt | cut -d '=' -f 2`
  ;;
  --cc=*) cc=`echo $opt | cut -d '=' -f 2`
  ;;
  --make=*) make=`echo $opt | cut -d '=' -f 2`
  ;;
  --extra-cflags=*) CFLAGS="$CFLAGS ${opt#--extra-cflags=}"
  ;;
  --extra-ldflags=*) LDFLAGS="$LDFLAGS ${opt#--extra-ldflags=}"
  ;;
  --extra-libs=*) extralibs=${opt#--extra-libs=}
  ;;
  --cpu=*) cpu=`echo $opt | cut -d '=' -f 2`
  ;;
  esac
done

case "$cpu" in
  i386|i486|i586|i686|i86pc|BePC)
    cpu="x86"
  ;;
  x86_64|amd64)
    cpu="x86"
    is_64="yes"
    canon_arch="`cc -dumpmachine | sed -e 's,\([^-]*\)-.*,\1,'`"
    if [ x"$canon_arch" = x"x86_64" -o x"$canon_arch" = x"amd64" ]; then
      if [ -z "`echo $CFLAGS | grep -- -m32`"  ]; then
        cpu="x86_64"
        #that's a bit crude...
        CFLAGS="$CFLAGS -fPIC -DPIC"
      fi
    fi
  ;;
  armv4l|arm)
    cpu="armv4l"
  ;;
  alpha)
    cpu="alpha"
  ;;
  "Power Macintosh"|ppc)
    cpu="powerpc"
  ;;
  mips)
    cpu="mips"
  ;;
  *)
    cpu="unknown"
  ;;
esac

# find source path
# find source path
source_path="`echo $0 | sed -e 's#/configure##'`"
source_path_used="yes"
if test -z "$source_path" -o "$source_path" = "." ; then
    source_path=`pwd`
    source_path_used="no"
else
    source_path="`cd \"$source_path\"; pwd`"
fi

# OS specific
targetos=`uname -s`
case $targetos in
BeOS)
js_flags=-DXP_BEOS
prefix="/boot/home/config"
CFLAGS="$CFLAGS -DPIC -fomit-frame-pointer"
# 3 gcc releases known for BeOS, each with ugly bugs
gcc_version="$($cc -v 2>&1 | grep version | cut -d ' ' -f3-)"
case "$gcc_version" in
2.9-beos-991026*|2.9-beos-000224*) echo "R5/GG gcc"
;;
*20010315*) echo "BeBits gcc"
CFLAGS="$CFLAGS -fno-expensive-optimizations"
;;
esac

SHFLAGS=-nostart
# no need for libm, but the inet stuff
# Check for BONE
if (echo $BEINCLUDES|grep 'headers/be/bone' >/dev/null); then
extralibs="-lbind -lsocket"
else
need_inet_aton="yes"
extralibs="-lnet"
fi ;;

SunOS)
make="gmake"
LDFLAGS=""
need_inet_aton="yes"
extralibs="$extralibs -lsocket -lnsl"
;;

FreeBSD)
make="gmake"
LDFLAGS="$LDFLAGS -export-dynamic"
CFLAGS="$CFLAGS -pthread"
GPAC_SH_FLAGS=-pthread
freebsd="yes"
js_flags="-DXP_UNIX -I/usr/include/js"
;;

BSD/OS)
extralibs="-lpoll -lgnugetopt -lm"
make="gmake"
;;

Darwin)
js_flags=-DXP_MAC
if test -d /sw/bin ; then
 alt_macosx_dir="/sw"
 CFLAGS="-I/sw/include"
 LDFLAGS="-L/sw/lib"
elif test -d /opt/local/bin ; then
 alt_macosx_dir="/opt/local"
 CFLAGS="-I/opt/local/include"
 LDFLAGS="-L/opt/local/lib"
fi
cc="cc"
Mac_Applications="/Applications"
SHFLAGS="-dynamiclib"
DYN_LIB_SUFFIX="dylib"
extralibs=""
GPAC_SH_FLAGS=""
strip="strip -x"
LDFLAGS="$LDFLAGS -read_only_relocs warning"
darwin="yes"
gcc_version=`$cc -v 2>&1 | grep version | cut -d ' ' -f3`
case "$gcc_version" in
*2.95*) 
CFLAGS="$CFLAGS -no-cpp-precomp -pipe -fomit-frame-pointer"
;;
3.*)
CFLAGS="$CFLAGS -no-cpp-precomp -pipe -fomit-frame-pointer -mdynamic-no-pic -fno-common"
;;
4.*) 
CFLAGS="$CFLAGS -no-cpp-precomp -pipe -fomit-frame-pointer -fno-common"
;;
esac
;;

MINGW32*)
js_flags="-DXP_PC -D_declspec=__declspec"
mingw32="yes"
win32="yes"
extralibs="$extralibs -lws2_32 -lwinmm"
;;

CYGWIN*)
js_flags=-DXP_PC 
extralibs="$extralibs -lws2_32 -lwinmm"
cygwin="yes"
win32="yes"
;;

Linux)
js_flags="-DXP_UNIX -I/usr/include/js"
linux="yes"
#LDFLAGS="$LDFLAGS -rdynamic"
;;
*) ;;
esac


#look for zlib
cat > $TMPC << EOF
#include <zlib.h>
int main( void ) { if (zlibVersion() != ZLIB_VERSION) { puts("zlib version differs !!!"); return 1; } return 0; }
EOF

has_zlib="no"
if $cc -o $TMPO $TMPC -lz 2> /dev/null  ; then
has_zlib="system"
elif $cc -o $TMPO $TMPC -I$local_inc/zlib -L$local_lib -lz 2> /dev/null  ; then
has_zlib="local"
else
  echo "error: zlib not found on system or in local libs"
  exit 1
fi

#check GCC flags support
if $cc -o $TMPO $TMPC -lz -fno-strict-aliasing 2> /dev/null  ; then
CFLAGS="$CFLAGS -fno-strict-aliasing"
fi
CPPFLAGS="$CFLAGS"

if $cc -o $TMPO $TMPC -lz -Wno-pointer-sign 2> /dev/null  ; then
CFLAGS="$CFLAGS -Wno-pointer-sign"
fi


#check dlopen
cat > $TMPC << EOF
#include <dlfcn.h>
int main( void ) { return (int) dlopen("foo", 0); }
EOF

if $cc -o $TMPE $TMPC > /dev/null 2>&1 ; then
dlopen="yes"
elif $cc -o $TMPE $TMPC -ldl > /dev/null 2>&1 ; then
GPAC_SH_FLAGS="$GPAC_SH_FLAGS -ldl"
else
	if test "$win32" = "no"; then
    echo "error: dlopen not found on system"
    exit 1
	fi
fi


#OK check for all local & systems lib
local_inc=$source_path/extra_lib/include
local_lib=$source_path/extra_lib/lib/gcc

#look for spidermonkey JS support

cat > $TMPC << EOF
#include <jsapi.h>
int main( void ) { return 0; }
EOF

#regular spidermonkey
if $cc -o $TMPO $TMPC $js_flags -ljs 2> /dev/null  ; then
has_js="system"
else
#debian spidermonkey (smjs)
js_flags="-DXP_UNIX -I/usr/include/smjs"
if $cc -o $TMPO $TMPC $js_flags -lsmjs 2> /dev/null  ; then
has_js="system"
js_lib="smjs"
else
#try local
js_flags="-DXP_UNIX -I$local_inc/js"
if $cc -o $TMPO $TMPC $js_flags -L$local_lib -ljs 2> /dev/null  ; then
has_js="local"
fi
fi
fi
#end JS test


#look for freetype support
if test "`which freetype-config`" != ""; then

ft_cflags="`freetype-config --cflags`"
ft_lflags="`freetype-config --libs`"

cat > $TMPC << EOF
#include <ft2build.h>
#include FT_FREETYPE_H
#include FT_GLYPH_H
#include FT_OUTLINE_H
int main( void ) { return 0; }
EOF

if $cc -o $TMPO $TMPC $ft_cflags $ft_lflags 2> /dev/null  ; then
has_ft="system"
else
ft_cflags="-I$local_inc/freetype"
ft_lflags="-L$local_lib -lfreetype"
if $cc -o $TMPO $TMPC $ft_cflags $ft_lflags 2> /dev/null  ; then
has_ft="local"
fi
fi

fi
#end freetype test



#look for OpenGL support
cat > $TMPC << EOF
#include <GL/gl.h>
#include <GL/glu.h>
int main( void ) { return 0; }
EOF

if test "$win32" = "yes" ; then
LINK3D="-lopengl32 -lglu32"
else
LINK3D="-lGL -lGLU"
fi

if $cc -o $TMPO $TMPC $LINK3D 2> /dev/null  ; then
has_opengl="yes"
fi

#look for OpenSSL support
cat > $TMPC << EOF
#include <openssl/ssl.h>
#include <openssl/x509.h>
#include <openssl/err.h>
#include <openssl/rand.h>
int main( void ) { return 0; }
EOF

if test "$win32" = "yes" ; then
LINK_SSL="-lssleay32 -leay32"
else
LINK_SSL="-lssl -lcrypto"
fi

if $cc -o $TMPO $TMPC $LINK_SSL 2> /dev/null  ; then
has_ssl="yes"
fi


#look for JPEG support
cat > $TMPC << EOF
#include <stdio.h>
#include <jpeglib.h>
int main( void ) { return 0; }
EOF

if $cc -o $TMPO $TMPC -ljpeg 2> /dev/null  ; then
has_jpeg="system"
elif test "$alt_macosx_dir" != "" ; then
 if cc -o $TMPO $TMPC -I$alt_macosx_dir/include -L$alt_macosx_dir/lib -ljpeg 2> /dev/null ; then
 has_jpeg="system"
 fi
elif $cc -o $TMPO $TMPC -I$local_inc/jpeg -L$local_lib -ljpeg 2> /dev/null  ; then
has_jpeg="local"
fi

#look for PNG support
cat > $TMPC << EOF
#include <png.h>
int main( void ) { return 0; }
EOF

if $cc -o $TMPO $TMPC -lpng -lz 2> /dev/null  ; then
has_png="system"
elif test "$alt_macosx_dir" != "" ; then
 if $cc -o $TMPO $TMPC -I$alt_macosx_dir/include -L$alt_macosx_dir/lib -lpng 2> /dev/null ; then
 has_png="system"
 fi
elif $cc -o $TMPO $TMPC -I$local_inc/png -L$local_lib -lpng 2> /dev/null  ; then
has_png="local"
fi

#look for MAD support
cat > $TMPC << EOF
#include <mad.h>
int main( void ) { return 0; }
EOF

if $cc -o $TMPO $TMPC -lmad 2> /dev/null  ; then
has_mad="system"
elif test "$alt_macosx_dir" != "" ; then
 if $cc -o $TMPO $TMPC -I$alt_macosx_dir/include -L$alt_macosx_dir/lib -lmad 2> /dev/null ; then
 has_mad="system"
 fi
elif $cc -o $TMPO $TMPC -I$local_inc/mad -L$local_lib -lmad 2> /dev/null  ; then
has_mad="local"
fi

#look for XVID support
cat > $TMPC << EOF
#include <xvid.h>
int main( void ) { return 0; }
EOF

if $cc -o $TMPO $TMPC -lxvidcore 2> /dev/null  ; then
has_xvid="system"
elif test "$alt_macosx_dir" != "" ; then
 if $cc -o $TMPO $TMPC -I$alt_macosx_dir/include -L$alt_macosx_dir/lib -lxvidcore 2> /dev/null ; then
 has_xvid="system"
 fi
elif $cc -o $TMPO $TMPC -I$local_inc/xvid -L$local_lib -lxvidcore 2> /dev/null  ; then
has_xvid="local"
fi

#look for FAAD support
cat > $TMPC << EOF
#include <faad.h>
int main( void ) { return 0; }
EOF

if $cc -o $TMPO $TMPC -lfaad -lm 2> /dev/null  ; then
has_faad="system"
elif test "$alt_macosx_dir" != "" ; then
 if $cc -o $TMPO $TMPC -I$alt_macosx_dir/include -L$alt_macosx_dir/lib -lfaad 2> /dev/null ; then
 has_faad="system"
 fi
elif $cc -o $TMPO $TMPC -I$local_inc/faad -L$local_lib -lfaad -lm 2> /dev/null  ; then
has_faad="local"
fi


#look for FFMPEG support
cat > $TMPC << EOF
#include <stddef.h>
#include <ffmpeg/avcodec.h>
int main( void ) { return 0; }
EOF

if $cc -o $TMPO $TMPC -lz -lavcodec -lavformat 2> /dev/null  ; then
has_ffmpeg="system"
elif test "$alt_macosx_dir" != "" ; then
 if $cc -o $TMPO $TMPC -I$alt_macosx_dir/include -L$alt_macosx_dir/lib -lz -lavcodec -lavformat 2> /dev/null ; then
 has_ffmpeg="system"
 fi
elif $cc -o $TMPO $TMPC -I$local_inc -L$local_lib -lz -lavcodec -lavformat 2> /dev/null  ; then
has_ffmpeg="local"
fi


#look for vorbis support
cat > $TMPC << EOF
#include <vorbis/codec.h>
int main( void ) { return 0; }
EOF

if $cc -o $TMPO $TMPC -lvorbis 2> /dev/null  ; then
has_vorbis="system"
elif test "$alt_macosx_dir" != "" ; then
 if $cc -o $TMPO $TMPC -I$alt_macosx_dir/include -L$alt_macosx_dir/lib -lvorbis 2> /dev/null ; then
 has_vorbis="system"
 fi
elif $cc -o $TMPO $TMPC -I$local_inc -L$local_lib -lvorbis -lm 2> /dev/null  ; then
has_vorbis="local"
fi

#look for theora support
cat > $TMPC << EOF
#include <theora/theora.h>
int main( void ) { return 0; }
EOF

if $cc -o $TMPO $TMPC -ltheora 2> /dev/null  ; then
has_theora="system"
elif test "$alt_macosx_dir" != "" ; then
 if $cc -o $TMPO $TMPC -I$alt_macosx_dir/include -L$alt_macosx_dir/lib -ltheora -logg 2> /dev/null ; then
 has_theora="system"
 fi
elif $cc -o $TMPO $TMPC -I$local_inc -L$local_lib -ltheora -logg -lm 2> /dev/null  ; then
has_theora="local"
fi

#look for OGG support
cat > $TMPC << EOF
#include <ogg/ogg.h>
int main( void ) { return 0; }
EOF

if $cc -o $TMPO $TMPC -logg 2> /dev/null  ; then
has_ogg="system"
elif test "$alt_macosx_dir" != "" ; then
 if $cc -o $TMPO $TMPC -I$alt_macosx_dir/include -L$alt_macosx_dir/lib -logg 2> /dev/null ; then
 has_ogg="system"
 fi
elif $cc -o $TMPO $TMPC -I$local_inc -L$local_lib -logg -lm 2> /dev/null  ; then
has_ogg="local"
else
has_vorbis=no
has_theora=no
fi


#look for OSS support
cat > $TMPC << EOF
#include <sys/ioctl.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/soundcard.h>
int main( void ) { return 0; }
EOF

if $cc -o $TMPO $TMPC 2> /dev/null  ; then
has_oss_audio="SYS"
else
cat > $TMPC << EOF
#include <sys/ioctl.h>
#include <unistd.h>
#include <fcntl.h>
#include <soundcard.h>
int main( void ) { return 0; }
EOF
if $cc -o $TMPO $TMPC 2> /dev/null  ; then
has_oss_audio="YES"
fi
fi

#look for wxWidgets support
has_wx="no"
wx_too_old="no"

if test "`which wx-config`" != "" ; then

wx_version=`wx-config --version | sed 's/[^0-9]//g'`

if test "$wx_version" -lt 250 ; then
wx_too_old="yes"
has_wx="yes"
else
if test "$wx_version" -lt 260 ; then
has_wx="yes"
wx_cflags=`wx-config --cppflags`
wx_lflags=`wx-config --libs=core`
else
has_wx="yes"
wx_cflags=`wx-config --cppflags core, base`
wx_lflags=`wx-config --libs core, base`
fi

if test "$darwin" = "yes" ; then
wx_lflags="-Wl,-bind_at_load $wx_lflags -lstdc++" #10.4 needs it, not sure about 10.3
fi

fi

cat > $TMPCPP << EOF
#include <wx/wx.h>
int main( void ) { return 0; }
EOF

if $cc $wx_cflags -o $TMPO $TMPCPP $wx_lflags > /dev/null 2>&1 ; then
wx_version=`wx-config --version | sed 's/[^0-9]//g'`
if test "$wx_version" -lt 254 ; then
wx_too_old="yes"
else
has_wx="yes"
fi
fi

fi
#end wx test


#look for X11 shared memory support
cat > $TMPC << EOF
#include <X11/Xlib.h>
#include <X11/extensions/XShm.h>
#include <sys/ipc.h>
#include <sys/shm.h>
int main( void ) { return 0; }
EOF

if $cc -o $TMPO $TMPC 2> /dev/null  ; then
has_x11_shm="yes"
fi

# look for IPv6
cat > $TMPC << EOF
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
int main( void ) {
  struct sockaddr_storage saddr;
  struct ipv6_mreq mreq6;
  getaddrinfo(0,0,0,0);
  getnameinfo(0,0,0,0,0,0,0);
  IN6_IS_ADDR_MULTICAST(0);
}
EOF

if $cc -o $TMPE $TMPC > /dev/null 2>&1  ; then
has_ipv6="yes"
fi



for opt do
  case "$opt" in
  --sdl-cfg=*) sdl_path=`echo $opt | cut -d '=' -f 2`; sdl_local="yes"
  ;;
  --enable-sdl-static=*) sdl_static="yes"
  ;;
  --X11-path=*) X11_PATH=`echo $opt | cut -d '=' -f 2`
  ;;
  --dxsdk-path=*) dxsdk_path=`echo $opt | cut -d '=' -f 2`
  ;;
  --mozdir=*) moz_path=`echo $opt | cut -d '=' -f 2`
  ;;
  --enable-amr-nb-fixed) has_amr_nb_fixed="yes"
  ;;
  --enable-amr-nb) has_amr_nb="yes"
  ;;
  --enable-amr-wb) has_amr_wb="yes"
  ;;
  --enable-amr) has_amr_wb="yes"; has_amr_nb="yes"
  ;;
  --disable-oggvorbis) has_oggvorbis="no"
  ;;
  --enable-debug) debuginfo="yes"; no_gcc_opt="yes"
  ;;
  --enable-gprof) gprof_build="yes";
  ;;
  --disable-opt) no_gcc_opt="yes"
  ;;
  --disable-ipv6) has_ipv6="no"
  ;;
  --disable-wx) has_wx="no"
  ;;
  --disable-fragments) iso_fragments="no"
  ;;
  --enable-readonly) make_readonly="yes"
  ;;
  --disable-oss-audio) has_oss_audio="no"
  ;;
  --disable-x11-shm) has_x11_shm="no"
  ;;
  --disable-svg) disable_svg="yes"
  ;;
  --enable-fixed-point) use_fixed_point="yes"
  ;;
  --force-fixed-point) use_fixed_point="forced"
  ;;
  --track-memory) use_memory_tracking="yes"
  ;;
  --disable-opengl) has_opengl="no"
  ;;
  --disable-ssl) has_ssl="no"
  ;;
  --use-faad=*) has_faad=${opt#--use-faad=}
  ;;
  --use-js=*) has_js=${opt#--use-js=}
  ;;
  --use-ft=*) has_ft=${opt#--use-ft=}
  ;;
  --use-mad=*) has_mad=${opt#--use-mad=}
  ;;
  --use-xvid=*) has_xvid=${opt#--use-xvid=}
  ;;
  --use-jpeg=*) has_jpeg=${opt#--use-jpeg=}
  ;;
  --use-ffmpeg=*) has_ffmpeg=${opt#--use-ffmpeg=}
  ;;
  --use-png=*) has_png=${opt#--use-png=}
  ;;
  esac
done

#GCC opt
if test "$no_gcc_opt" = "no"; then
CFLAGS="-O3 $CFLAGS"
fi

#look for DX support
dx_path="system"
has_mingw_directx="no"
if test "$win32" = "yes" ; then

cat > $TMPC << EOF
#include <ddraw.h>
int main( void ) { return 0; }
EOF

if $cc -o $TMPO $TMPC 2> /dev/null  ; then
has_mingw_directx="yes"
else
dx_path="$dxsdk_path"
if $cc -o $TMPO $TMPC -I$dxsdk_path/include -L$dxsdk_path/lib -lddraw 2> /dev/null  ; then
has_mingw_directx="yes"
fi
fi

fi


#look for SDL support
sdl_too_old=no
has_sdl=no
sdl_config="sdl-config"
if test "$sdl_local" = "yes"; then
sdl_config="$sdl_path/sdl-config"
sdl_static="yes"
fi

if test "`which $sdl_config`" != ""; then

cat > $TMPC << EOF
#include <SDL.h>
#undef main
int main( void ) { return SDL_Init (SDL_INIT_VIDEO); }
EOF

if test "$sdl_static" = "yes"; then
sdl_lib_flags=`$sdl_config --static-libs`
else
sdl_lib_flags=`$sdl_config --libs`
fi
sdl_cflags=`$sdl_config --cflags`

if $cc -o $TMPO $sdl_cflags $TMPC $sdl_lib_flags  > /dev/null 2>&1  ; then
_sdlversion=`$sdl_config --version | sed 's/[^0-9]//g'`
if test "$_sdlversion" -lt 121 ; then
sdl_too_old=yes
else
has_sdl=yes
fi
fi

fi
#end SDL check


#look for libxml2 support
has_xml2="no"
xml2_too_old="no"

if test "`which xml2-config`" != "" ; then

xml2_cflags=`xml2-config --cflags`
xml2_lflags=`xml2-config --libs`

cat > $TMPC << EOF
#include <stdio.h>
#include <string.h>
#include <libxml/tree.h>
#include <libxml/parser.h>
#include <libxml/xpath.h>
#include <libxml/xpathInternals.h>
int main( void ) { return 0; }
EOF

if $cc $xml2_cflags -o $TMPO $TMPC $xml2_lflags > /dev/null 2>&1 ; then
xml2_version=`xml2-config --version | sed 's/[^0-9]//g'`
if test "$xml2_version" -lt 260 ; then
xml2_too_old="yes"
else
has_xml2="yes"
fi
fi

fi
#end xml2 tes


# Checking for CFLAGS
if test -z "$CFLAGS"; then
    CFLAGS="-O3"
fi

if test "$win32" = "yes" ; then
    cross_prefix=""
fi

if test "$mingw32" = "yes" ; then
    cross_prefix=""
fi

cc="${cross_prefix}${cc}"
ar="${cross_prefix}${ar}"
ranlib="${cross_prefix}${ranlib}"
strip="${cross_prefix}${strip}"

if test -z "$cross_prefix" ; then

# big/little endian test
cat > $TMPC << EOF
#include <inttypes.h>
int main(int argc, char ** argv){
	volatile uint32_t i=0x01234567;
	return (*((uint8_t*)(&i))) == 0x67;
}
EOF

if $cc -o $TMPO $TMPC 2>/dev/null ; then
$TMPO && bigendian="yes"
else
echo big/little endian test failed
fi

else

# if cross compiling, cannot launch a program, so make a static guess
if test "$cpu" = "powerpc" -o "$cpu" = "mips" ; then
    bigendian="yes"
fi

fi

if test x"$mandir" = x""; then
mandir="${prefix}/man"
fi

if test "$cpu" = "armv4l"; then
X11_PATH=""
fi

version=`grep '#define GPAC_VERSION ' $source_path/include/gpac/tools.h | cut -d '"' -f 2`
echo ""
echo "** System Configuration"
echo "Install prefix: $prefix"
echo "Source path: $source_path"
echo "C compiler: $cc"
echo "make: $make"
echo "CPU: $cpu"
echo "Big Endian: $bigendian"
if test $cpu = "mips"; then
echo "MMI enabled: $mmi"
fi
echo ""
echo "** GPAC $version Core Configuration **"
echo "debug version: $debuginfo"
echo "GProf enabled: $gprof_build"
echo "Memory tracking enabled: $use_memory_tracking"
echo "read-only version: $make_readonly"
echo "fixed-point version: $use_fixed_point"
echo "IPV6 Support: $has_ipv6"
echo "IsoMedia MovieFragments support: $iso_fragments"
echo "SVG Support disabled: $disable_svg"
echo ""

echo "** Detected libraries **"
echo "zlib: $has_zlib"

if test "$win32" != "yes" ; then
if test "$has_oss_audio" = "no"; then
echo "OSS Audio: no"
else
echo "OSS Audio: yes"
fi
echo "X11 Shared Memory support: $has_x11_shm (path: $X11_PATH)"
fi

echo "SDL Support: $has_sdl"
if test "$sdl_too_old" = "yes" ; then
echo "SDL Version too old - please upgrade for SDL support"
fi
echo "libXML2: $has_xml2"
if test "$xml2_too_old" = "yes" ; then
echo "XML2 Version too old - please upgrade for libXML2 support (SVG parser)"
fi
echo "OpenGL support: $has_opengl"
echo "OpenSSL support: $has_ssl"

if test "$win32" = "yes" ; then
echo "DirectX Support: $has_mingw_directx"
fi

if test "$wx_too_old" = "yes" ; then
has_wx="no"
echo "wxWidgets Version too old - please upgrade to 2.6.0 for wxWidgets support"
fi
if test "$has_wx" = "yes" ; then
echo "wxWidgets support: Version $wx_version"
else
echo "wxWidgets support: no"
fi


echo ""
echo "** Extra Libraries used **"
echo "SpiderMonkey: $has_js"
echo "FreeType: $has_ft"
echo "JPEG: $has_jpeg"
echo "PNG: $has_png"
echo "MAD: $has_mad"
echo "FAAD: $has_faad"
echo "XVID: $has_xvid"
echo "FFMPEG: $has_ffmpeg"
echo "Xiph OGG: $has_ogg"
if test "$has_ogg" = "no"; then
has_ogg="no"
else
echo "Xiph Vorbis: $has_vorbis"
echo "Xiph Theora: $has_theora"
fi

echo ""
if test "$has_amr_nb_fixed" = "yes" ; then
  echo "*** AMR NB FIXED-POINT NOTICE ***"
  echo "Make sure you have downloaded TS26.073 from:"
  echo "http://www.3gpp.org/ftp/Specs/archive/26_series/26.073/26073-600.zip"
  echo "or through gpac_extra_libs and extracted src to modules/amr_dec/amr_nb"
  echo "without overwriting typedefs.h file"
  echo ""
fi

echo ""
if test "$has_amr_nb" = "yes" ; then
  echo "*** AMR NB NOTICE ***"
  echo "Make sure you have downloaded TS26.104 from:"
  echo "http://www.3gpp.org/ftp/Specs/archive/26_series/26.104/26104-610.zip"
  echo "or through gpac_extra_libs and extracted src to modules/amr_float_dec/amr_nb_ft"
  echo "without overwriting typedefs.h file"
  echo ""
fi


echo ""
if test "$has_amr_wb" = "yes" ; then
  echo "*** AMR WB NOTICE ***"
  echo "Make sure you have downloaded TS26.204 from:"
  echo "http://www.3gpp.org/ftp/Specs/archive/26_series/26.204/26204-600.zip"
  echo "or through gpac_extra_libs and extracted src to modules/amr_float_dec/amr_wb_ft"
  echo "without overwriting typedefs.h file"
  echo ""
fi

if test "$use_memory_tracking" = "yes"; then
echo "!! WARNING: GPAC Memory tracking is enabled !!"
echo "!! This may corrupt third-party tools using libgpac !!"
echo "!! Use at your own risk, and only for GPAC benchmarking !!"
echo ""
fi

#needs gmon for win32 gprof
if test "$gprof_build" = "yes"; then
if test "$win32" = "yes"; then
extralibs="$extralibs -lgmon"
fi
fi


echo "Creating config.mak"
echo "# Automatically generated by configure - do not modify" > config.mak

#overwrite fixed-point in math.h
if test "$use_fixed_point" = "yes"; then
if grep "#define GPAC_NO_FIXED_POINT" $source_path/include/gpac/math.h > /dev/null 2>&1 ; then
sed -e 's/#define GPAC_NO_FIXED_POINT/#define GPAC_FIXED_POINT/' $source_path/include/gpac/math.h >$TMPH
cp -f $TMPH $source_path/include/gpac/math.h
fi
elif test "$use_fixed_point" = "forced"; then
CFLAGS="$CFLAGS -DGPAC_FIXED_POINT"
else
if grep "#define GPAC_FIXED_POINT" $source_path/include/gpac/math.h > /dev/null 2>&1 ; then
sed -e 's/#define GPAC_FIXED_POINT/#define GPAC_NO_FIXED_POINT/' $source_path/include/gpac/math.h >$TMPH
cp -f $TMPH $source_path/include/gpac/math.h
fi
fi

echo "prefix=$prefix" >> config.mak
echo "DESTDIR=$DESTDIR" >> config.mak
echo "moddir=$prefix/lib/gpac" >> config.mak
echo "mandir=$mandir" >> config.mak
echo "MAKE=$make" >> config.mak
echo "CC=$cc" >> config.mak
echo "AR=$ar" >> config.mak
echo "RANLIB=$ranlib" >> config.mak
echo "STRIP=$strip" >> config.mak
echo "OPTFLAGS=$CFLAGS" >> config.mak
echo "CPPFLAGS=$CPPFLAGS" >> config.mak
echo "LDFLAGS=$LDFLAGS" >> config.mak
echo "SHFLAGS=$SHFLAGS" >> config.mak

if test "$cpu" = "x86" ; then
  echo "TARGET_ARCH_X86=yes" >> config.mak
elif test "$cpu" = "armv4l" ; then
  echo "TARGET_ARCH_ARMV4L=yes" >> config.mak
elif test "$cpu" = "alpha" ; then
  echo "TARGET_ARCH_ALPHA=yes" >> config.mak
elif test "$cpu" = "sparc64" ; then
  echo "TARGET_ARCH_SPARC64=yes" >> config.mak
elif test "$cpu" = "powerpc" ; then
  echo "TARGET_ARCH_POWERPC=yes" >> config.mak
elif test "$cpu" = "mips" ; then
  echo "TARGET_ARCH_MIPS=yes" >> config.mak
fi


if test "$bigendian" = "yes" ; then
  echo "IS_BIGENDIAN=yes" >> config.mak
fi
echo "EXTRALIBS=$extralibs" >> config.mak
echo "VERSION=$version" >>config.mak

if test "$win32" = "yes" ; then
  echo "CONFIG_WIN32=yes" >> config.mak
elif test "$linux" = "yes" ; then
  echo "CONFIG_LINUX=yes" >> config.mak
elif test "$freebsd" = "yes" ; then
  echo "CONFIG_FREEBSD=yes" >> config.mak
elif test "$darwin" = "yes" ; then
 echo "CONFIG_DARWIN=yes" >> config.mak
 echo "mac_apps=$Mac_Applications" >> config.mak
fi

if test "$win32" = "no" ; then
	echo "GPAC_SH_FLAGS=$GPAC_SH_FLAGS" >> config.mak
	echo "DYN_LIB_SUFFIX=$DYN_LIB_SUFFIX" >> config.mak
else
	echo "DYN_LIB_SUFFIX=dll" >> config.mak
fi

echo "CONFIG_JS=$has_js" >> config.mak
if test "$has_js" = "no" ; then
	has_js="no"
else
echo "JS_FLAGS=$js_flags" >> config.mak
echo "JS_LIBS=$js_lib" >> config.mak
fi
echo "CONFIG_ZLIB=$has_zlib" >> config.mak
echo "CONFIG_FT=$has_ft" >> config.mak
echo "CONFIG_JPEG=$has_jpeg" >> config.mak
echo "CONFIG_PNG=$has_png" >> config.mak
echo "CONFIG_FAAD=$has_faad" >> config.mak
echo "CONFIG_MAD=$has_mad" >> config.mak
echo "CONFIG_XVID=$has_xvid" >> config.mak
echo "CONFIG_OGG=$has_ogg" >> config.mak
echo "CONFIG_VORBIS=$has_vorbis" >> config.mak
echo "CONFIG_THEORA=$has_theora" >> config.mak
echo "CONFIG_FFMPEG=$has_ffmpeg" >> config.mak
echo "CONFIG_OSS_AUDIO=$has_oss_audio" >> config.mak
echo "DISABLE_SVG=$disable_svg" >> config.mak
echo "HAS_OPENGL=$has_opengl" >> config.mak
if test "$has_opengl" = "yes" ; then
  echo "OGL_LIBS=$LINK3D" >> config.mak
fi
echo "HAS_OPENSSL=$has_ssl" >> config.mak
if test "$has_ssl" = "yes" ; then
  echo "SSL_LIBS=$LINK_SSL" >> config.mak
fi

echo "CONFIG_SDL=$has_sdl" >> config.mak
if test "$has_sdl" = "yes" ; then
  echo "SDL_CFLAGS=$sdl_cflags" >> config.mak
  echo "SDL_LIBS=$sdl_lib_flags" >> config.mak
fi
if test "$has_ft" = "no" ; then
	has_ft="no"
else
  echo "FT_CFLAGS=$ft_cflags" >> config.mak
  echo "FT_LIBS=$ft_lflags" >> config.mak
fi
echo "CONFIG_AMR_NB=$has_amr_nb_fixed" >> config.mak
echo "CONFIG_AMR_NB_FT=$has_amr_nb" >> config.mak
echo "CONFIG_AMR_WB_FT=$has_amr_wb" >> config.mak
echo "DEBUGBUILD=$debuginfo" >> config.mak
echo "GPROFBUILD=$gprof_build" >> config.mak
echo "ISOFRAGMENTS=$iso_fragments" >> config.mak
echo "GPACREADONLY=$make_readonly" >> config.mak

echo "CONFIG_IPV6=$has_ipv6" >> config.mak

if test "$win32" = "yes" ; then
echo "CONFIG_DIRECTX=$has_mingw_directx" >> config.mak
if test "$has_mingw_directx" = "yes" ; then
echo "DX_PATH=$dx_path" >> config.mak
fi
fi

echo "USE_WXWIDGETS=$has_wx" >> config.mak
if test "$has_wx" = "yes"; then
echo "WX_CFLAGS=$wx_cflags" >> config.mak
echo "WX_LFLAGS=$wx_lflags" >> config.mak
fi

echo "HAS_LIBXML2=$has_xml2" >> config.mak
if test "$has_xml2" = "yes"; then
echo "XML2_CFLAGS=$xml2_cflags" >> config.mak
echo "XML2_LFLAGS=$xml2_lflags" >> config.mak
fi

echo "MOZILLA_DIR=$moz_path" >> config.mak


if test "$has_oss_audio" != "no"; then
echo "OSS_INC_TYPE=$has_oss_audio" >> config.mak
fi

if test "$has_x11_shm" = "yes"; then
echo "USE_X11_SHM=$has_x11_shm" >> config.mak
fi

if test "$is_64" = "yes"; then
echo "X11_LIB_PATH=$X11_PATH/lib64" >> config.mak
else
echo "X11_LIB_PATH=$X11_PATH/lib" >> config.mak
fi
echo "X11_INC_PATH=$X11_PATH/include" >> config.mak

#overwrite memory tracking in setup.h
if test "$use_memory_tracking" = "yes"; then
if grep "#define GPAC_MEMORY_TRACKING 0" $source_path/include/gpac/setup.h > /dev/null 2>&1 ; then
sed -e 's/#define GPAC_MEMORY_TRACKING 0/#define GPAC_MEMORY_TRACKING 1/' $source_path/include/gpac/setup.h >$TMPH
cp -f $TMPH $source_path/include/gpac/setup.h
fi
else
if grep "#define GPAC_MEMORY_TRACKING 1" $source_path/include/gpac/setup.h > /dev/null 2>&1 ; then
sed -e 's/#define GPAC_MEMORY_TRACKING 1/#define GPAC_MEMORY_TRACKING 0/' $source_path/include/gpac/setup.h >$TMPH
cp -f $TMPH $source_path/include/gpac/setup.h
fi
fi


# build tree in object directory if source path is different from current one
if test "$source_path_used" = "yes" ; then

    echo "Creating compilation tree image"
    SRC_DIRS="src src/utils src/isomedia src/ietf src/odf src/bifs src/scenegraph src/terminal src/mcrypt src/media_tools src/scene_manager src/renderer src/laser" 
    
    APP_DIRS="applications applications/mp4box applications/mp4client applications/mp4box applications/osmozilla applications/osmo4_wx"

    for dir in $SRC_DIRS ; do
         mkdir -p $dir
    done
    ln -sf $source_path/Makefile Makefile
    ln -sf $source_path/src/Makefile src/Makefile
    ln -sf $source_path/src/libgpac.def src/libgpac.def

    for dir in $APP_DIRS ; do
         mkdir -p $dir
         ln -sf $source_path/$dir/Makefile $dir/Makefile
    done

    MOD_DIRS="modules modules/aac_in modules/amr_dec modules/amr_float_dec modules/bifs_dec modules/ctx_load modules/dummy_in modules/dx_hw modules/ffmpeg_in modules/ft_font modules/img_in modules/isom_in modules/laser_dec modules/mp3_in modules/mpegts_in modules/odf_dec modules/ogg modules/oss_audio modules/raw_out modules/render2d modules/render3d modules/rtp_in modules/saf_in modules/sdl_out modules/soft_raster modules/svg_in modules/svg_loader modules/timedtext modules/wav_out modules/x11_out modules/xvid_dec"
    cur_dir="`pwd`"
    for dir in $MOD_DIRS ; do
            mkdir -p $dir
            ln -sf $source_path/$dir/Makefile $dir/Makefile
            cd $source_path/$dir
            if ls *.def > /dev/null 2>&1; then
             def_file="`ls *.def`"
             cd $cur_dir
             ln -sf $source_path/$dir/$def_file $dir/$def_file
            else
             cd $cur_dir
            fi
    done
    if test "$has_mingw_directx" = "yes"; then
      ln -sf $source_path/modules/dx_hw/hand.cur modules/dx_hw/hand.cur
      ln -sf $source_path/modules/dx_hw/collide.cur modules/dx_hw/collide.cur
    fi

    cd $cur_dir

    echo "SRC_LOCAL_PATH=no" >> config.mak
else
    echo "SRC_LOCAL_PATH=yes" >> config.mak
fi

echo "SRC_PATH=$source_path" >> config.mak


rm -f $TMPO $TMPC $TMPS $TMPCPP

if [ ! -d "./bin" ]; then
	mkdir ./bin
fi
if [ ! -d "./bin/gcc" ]; then
	mkdir ./bin/gcc
fi
if [ ! -d "./bin/gcc/temp" ]; then
	mkdir ./bin/gcc/temp
fi

echo "Done - type 'make help' for make info, 'make' to build"
