#!/bin/sh

export version=6.22.1
export soversion=6

show_help()
{
	cat << EOF
Non-autotool config script for MLT.

Help options:

  --help                  - this information

General build options:

  --prefix=directory         - install prefix for path (default: $prefix)
  --libdir=directory         - lib directory (default: $prefix/lib)
  --datadir=directory        - data directory (default: $prefix/share)
  --mandir=directory         - man documentation directory (default: $prefix/share/man)
  --rename-melt              - Give melt executable a different name (it will not be versioned)
  --enable-extra-versioning  - Version melt and the data and modules directories
  --enable-gpl               - Enable GPLv2 components
  --enable-gpl3              - Enable GPLv3 components
  --enable-debug             - Compile without optimizations support (default: off)
  --disable-debug            - Compile without debug support (default: on)
  --disable-mmx              - Compile without MMX support (default: on)
  --disable-sse              - Compile without SSE support (default: on)
  --disable-sse2             - Compile without SSE2 support (default: on)
  --arch='arch'              - Compile for a specific architecture (default: none)
  --cpu='cpu'                - Compile for a specific CPU (default: none)
  --target-os='os'           - Cross-compile to a specific OS (default: $(uname -s))
  --target-arch='arch'       - Cross-compile to a specific CPU architecture
  --disable-windeploy        - Keep bin/ lib/ layout on Windows

Module disable options:

EOF

	for i in src/modules/*
	do
		[ -d $i ] && [ "`basename $i`" != "CVS" ] && echo `basename $i` `[ -f $i/gpl ] && echo [GPL]`
	done |
	awk '{ printf( "  --disable-%-14.14s- Disable the %s module %s\n", $1, $1, $2 ); }'

	echo
	echo "  NOTE: libraries marked [GPL] will not be built unless --enable-gpl is stipulated."
	echo
}

build_config()
{
	(
		echo "version=$version"
		echo "soversion=$soversion"
		echo "prefix=$prefix"
		echo "libdir=$libdir"
		echo "bindir=$prefix/bin"
		echo "datadir=$datadir"
		echo "mandir=$mandir"
		echo "extra_versioning=$extra_versioning"
		echo "melt_noversion=$melt_noversion"
		echo "targetos=$targetos"
		echo "windeploy=$windeploy"

		[ "$mmx" = "true" ] && 
		echo "MMX_FLAGS=-DUSE_MMX -mmmx"

		[ "$sse" = "true" ] && 
		echo "SSE_FLAGS=-DUSE_SSE -msse"

		[ "$sse2" = "true" ] && 
		echo "SSE2_FLAGS=-DUSE_SSE2 -msse2"

		[ "$debug" = "true" ] && 
		echo "DEBUG_FLAGS=-g"

		echo "LARGE_FILE=-D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE"

		[ "$amd64" = "true" ] && echo "ARCH_X86_64=1" && echo "CFLAGS+=-DARCH_X86_64"
		[ "$arch" != "" ] && echo "TARGETARCH=-march=$arch"
		[ "$cpu" != "" ] && echo "TARGETCPU=-mcpu=$cpu"
		if [ "$optimisations" = "true" ]
		then
			echo "OPTIMISATIONS=-O2 -pipe"
			if $($CC --version | grep gcc > /dev/null); then
				# Since gcc 4.6, this optimization enabled with -O1 causes filter_line_sse2 to crash.
				echo "OPTIMISATIONS+=-fno-tree-dominator-opts"
				# Since gcc 4.6, this optimization enabled with -O2 causes filter_line_sse2 to crash.
				echo "OPTIMISATIONS+=-fno-tree-pre"
			fi
		fi

		echo "CFLAGS+=-Wall -DPIC \$(TARGETARCH) \$(TARGETCPU) \$(OPTIMISATIONS) \$(MMX_FLAGS) \$(SSE_FLAGS) \$(SSE2_FLAGS) \$(DEBUG_FLAGS) \$(LARGE_FILE)"
		echo "CXXFLAGS+=-Wall -DPIC \$(TARGETARCH) \$(TARGETCPU) \$(OPTIMISATIONS) \$(MMX_FLAGS) \$(SSE_FLAGS) \$(SSE2_FLAGS) \$(DEBUG_FLAGS) \$(LARGE_FILE)"

		case $targetos in
		Darwin)
		echo "CFLAGS+=-fPIC"
		echo "SHFLAGS=-dynamiclib"
		;;
		Linux|GNU/kFreeBSD|GNU)
		[ "$optimisations" = "true" ] &&
			echo "OPTIMISATIONS+=-ffast-math"
		echo "CFLAGS+=-fPIC -pthread"
		echo "SHFLAGS=-shared"
		echo "LIBDL=-ldl"
		echo "RDYNAMIC=-rdynamic"
		echo "LDFLAGS+=-Wl,--no-undefined -Wl,--as-needed"
		;;
		FreeBSD)
		[ "$optimisations" = "true" ] &&
			echo "OPTIMISATIONS+=-ffast-math"
		echo "CFLAGS+=-fPIC -pthread"
		echo "SHFLAGS=-shared"
		echo "RDYNAMIC=-rdynamic"
		echo "LDFLAGS+=-Wl,--no-undefined -Wl,--as-needed"
		;;
		NetBSD)
		[ "$optimisations" = "true" ] &&
			echo "OPTIMISATIONS+=-ffast-math"
		echo "CFLAGS+=-fPIC -pthread"
		echo "SHFLAGS=-shared"
		echo "RDYNAMIC=-rdynamic"
		echo "LDFLAGS+=-Wl,--no-undefined -Wl,--as-needed"
		;;
		OpenBSD)
		echo "CFLAGS+=-fPIC"
		echo "CXXFLAGS+=-fPIC"
		echo "SHFLAGS=-shared"
		echo "RDYNAMIC=-rdynamic"
		echo "LDFLAGS+=-Wl,--as-needed"
		;;
		MinGW)
		[ "$windeploy" = false ] &&
			echo "CFLAGS+=-DNODEPLOY"
		[ "$optimisations" = "true" ] &&
			echo "OPTIMISATIONS+=-ffast-math"
		echo "SHFLAGS=-shared"
		echo "LIBDL=-ldl"
		echo "RDYNAMIC="
		echo "LDFLAGS+=-Wl,--no-undefined -Wl,--as-needed"
		# If the host system is MinGW running on Windows, then cause make to
		# treat all .c files as C and not use CXX. mingw32-make is case
		# insensitive, and .C is considered C++.
		if uname -s | grep -iq mingw ; then
			echo "%.o: %.c"
			printf "\t\$(CC) -c \$(CFLAGS) \$(CPPFLAGS) -o \$@ \$<\n"
		fi
		;;		
		*)
		;;
		esac
		echo "LIBSUF=$LIBSUF"
		echo "moduledir=${moduledir}"
		echo "mltdatadir=${mltdatadir}"
		echo "unversionedmoduledir=${unversionedmoduledir}"
		echo "unversionedmltdatadir=${unversionedmltdatadir}"
		echo "meltname=${meltname}"
	) > config.mak

	echo "#!/bin/sh" > mlt-config
	(
		echo export version=$version
		echo export prefix=$prefix
		echo export libdir=$libdir
		echo export bindir=$prefix/bin
	) >> mlt-config

	cat < mlt-config-template >> mlt-config

	echo -n > packages.dat
}

build_pkgconfig()
{
	echo prefix="$prefix" > mlt-framework.pc
	(
		echo exec_prefix=$prefix
		echo libdir=$libdir
		echo includedir=$prefix/include
		echo datadir=$datadir
		echo mandir=$mandir
		echo version=$version
		echo cflags=`grep ^framework packages.dat | cut -f 2`
		echo libs=`grep ^framework packages.dat | cut -f 3`
		echo moduledir=${moduledir}
		echo mltdatadir=${mltdatadir}
		echo meltbin=${prefix}/bin/${meltname}
	) >> mlt-framework.pc
	cat mlt-framework.pc.in >>mlt-framework.pc

	echo prefix="$prefix" > mlt++.pc
	(
		echo exec_prefix=$prefix
		echo libdir=$libdir
		echo includedir=$prefix/include
		echo datadir=$datadir
		echo mandir=$mandir
		echo version=$version
		echo cflags=`grep ^mlt++ packages.dat | cut -f 2`
		echo libs=`grep ^mlt++ packages.dat | cut -f 3`
	) >> mlt++.pc
	cat mlt++.pc.in >>mlt++.pc
}

# Debug mode
# set -x

# Define build directory for scripts called
export build_dir=`dirname $0`
export prefix=/usr/local
export libdir=""
export datadir=""
export mandir=""
export help=0
export optimisations=true
export debug=true
export mmx=true
export sse=true
export sse2=true
export gpl=false
export gpl3=false
export arch=
export cpu=
export targetos=$(uname -s)
export targetarch=
export amd64=false
export extra_versioning=false
export melt_noversion=false
export windeploy=true

# Define the compiler used in tests (gcc is not installed everywhere)
: ${CC:=gcc}

# Test for a supported C++ compiler.
if $($CC --version | grep -is -e gcc > /dev/null)
then
	echo "Found gcc version $($CC -dumpversion)"
	if $CC -dumpversion | awk '{split($0, a, "."); if ((lshift(a[1],16) + lshift(a[2],8) + a[3]) < lshift(4,16) + lshift(8,8) + 1) exit 0; else exit 1;}'
	then
		echo "The minimum version of gcc supported is 4.8.1 (for C++11)."
		exit 1
	fi
elif $($CC --version | grep -is -e clang > /dev/null)
then
	echo "Found clang version $($CC -dumpversion)"
	if $CC -dumpversion | awk '{split($0, a, "."); if ((lshift(a[1],8) + a[2]) < lshift(3,8) + 3) exit 0; else exit 1;}'
	then
		echo "The minimum version of clang supported is 3.3 (for C++11)."
		exit 1
	fi
else
	echo "MLT requires either the gcc or clang C/C++ compilers."
	exit 1
fi

# Iterate through arguments
for i in "$@"
do
	case $i in
		--help )			help=1 ;;
		--prefix=* )			prefix="${i#--prefix=}" ;;
		--libdir=* )			libdir="${i#--libdir=}" ;;
		--datadir=* )			datadir="${i#--datadir=}" ;;
		--mandir=* )			mandir="${i#--mandir=}" ;;
		--rename-melt=* )		meltname="${i#--rename-melt=}"; melt_noversion=true ;;
		--enable-extra-versioning )	extra_versioning=true ;;
		--enable-debug )		optimisations=false ;;
		--disable-debug )		debug=false ;;
		--disable-mmx )			mmx=false; sse=false; sse2=false ;;
		--disable-sse )			sse=false; sse2=false ;;
		--disable-sse2 )		sse2=false ;;
		--enable-gpl )			gpl=true ;;
		--enable-gpl3 )			gpl3=true ;;
		--arch=* )			arch="${i#--arch=}" ;;
		--cpu=* )			cpu="${i#--cpu=}" ;;
		--target-os=* )			targetos="${i#--target-os=}" ;;
		--target-arch=* )		targetarch="${i#--target-arch=}" ;;
		--disable-windeploy )		windeploy=false ;;
	esac
done

if [ -z "${meltname}" ]
then
	if [ "$extra_versioning" = "false" ]
	then
		meltname=melt
	else
		meltname=melt${soversion}
	fi
fi

# Chose appropriate suffix for libraries
case $targetos in
	Darwin)
	LIBSUF=".dylib"
	if [ "$targetarch" = "" ]
	then
		sysctl -a hw | grep "x86_64: 1" > /dev/null
		[ "$?" = "0" ] && targetarch="amd64"
	fi
	;;
	Linux|FreeBSD|NetBSD)
	LIBSUF=".so"
	;;
	MINGW??_NT-*|MinGW|mingw)
	targetos="MinGW"
	LIBSUF=".dll"
	;;
	*)
	LIBSUF=".so"
	;;
esac
export LIBSUF

# Determine if we are compiling for 64-bit Intel architecture
[ "$targetarch" = "" ] && targetarch=$(uname -m)
[ "$targetarch" = "amd64" -o "$targetarch" = "x86_64" ] && amd64=true

# Determine the libdir if it's not specified in the args
[ "$libdir" = "" ] && libdir=$prefix/lib
[ "$datadir" = "" ] && datadir=$prefix/share
[ "$mandir" = "" ] && mandir=$prefix/share/man

export unversionedmoduledir=${libdir}/mlt
export unversionedmltdatadir=${datadir}/mlt
if [ "$extra_versioning" = "false" ]
then
export moduledir=${libdir}/mlt
export mltdatadir=${datadir}/mlt
else
export moduledir=${libdir}/mlt-${soversion}
export mltdatadir=${datadir}/mlt-${soversion}
fi

# Double check MMX (Darwin, Linux and FreeBSD supported, may end up disabling MMX on other platforms incorrectly)
if [ "$mmx" = "true" ]
then
	case $targetos in
		Darwin)
		sysctl -a hw | grep "mmx: 1" > /dev/null || mmx=false
		;;
		Linux)
		grep mmx /proc/cpuinfo > /dev/null 2>&1 || mmx=false
		;;
		FreeBSD)
		[ "$(make -V MACHINE_CPU:Mmmx -f /dev/null)" ] || mmx=false
		;;
		*)
		grep mmx /proc/cpuinfo > /dev/null 2>&1 || mmx=false
		;;
	esac
fi

# Double check SSE (Darwin, Linux and FreeBSD supported, may end up disabling SSE on other platforms incorrectly)
if [ "$sse" = "true" ]
then
	case $targetos in
		Darwin)
		sysctl -a hw | grep "sse: 1" > /dev/null || sse=false
		;;
		Linux)
		grep sse /proc/cpuinfo > /dev/null 2>&1 || sse=false
		;;
		FreeBSD)
		[ "$(make -V MACHINE_CPU:Msse -f /dev/null)" ] || sse=false
		;;
		*)
		grep sse /proc/cpuinfo > /dev/null 2>&1 || sse=false
		;;
	esac
fi

# Double check SSE2 (Darwin, Linux and FreeBSD supported, may end up disabling SSE2 on other platforms incorrectly)
if [ "$sse2" = "true" ]
then
	case $targetos in
		Darwin)
		sysctl -a hw | grep "sse2: 1" > /dev/null || sse2=false
		;;
		Linux)
		grep sse2 /proc/cpuinfo > /dev/null 2>&1 || sse2=false
		;;
		FreeBSD)
		[ "$(make -V MACHINE_CPU:Msse2 -f /dev/null)" ] || sse2=false
		;;
		*)
		grep sse2 /proc/cpuinfo > /dev/null 2>&1 || sse2=false
		;;
	esac
fi

# Show help if requested
if [ $help = 1 ]
then
	show_help
else
	# Log the configuration history
	date >> config.log
	echo "$0 $@" >> config.log

	build_config
fi

# Iterate through each of the components
for i in framework modules melt mlt++ swig
do
	if [ -x src/$i/configure ]
	then
		[ $help = 0 ] && echo "Configuring `basename $i`:"
		olddir=`pwd`
		cd src/$i
		CC="$CC" ./configure "$@"
		[ $? != 0 ] && exit 1
		cd $olddir
	fi
done

# Build the pkg-config files
build_pkgconfig

# Report license Usage
if [ $help != 1 ]
then
	if [ "$gpl" = "false" ]
	then
		echo "LGPLv2.1 license used; GPL components disabled"
	elif [ "$gpl3" = "false" ]
	then
		echo "GPLv2 license used; GPLv3 components disabled"
	else
		echo "GPLv3 license used"
	fi
fi
