#! /bin/sh
# Copyright (C) 2003-2011 the VideoLAN team
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.

#
# Command line handling
#
usage()
{
	echo "Usage: $0 [--build=BUILD] [--host=HOST] [--prefix=PREFIX]"
	echo "  --build=BUILD    configure for building on BUILD"
	echo "  --host=HOST      cross-compile to build to run on HOST"
	echo "  --prefix=PREFIX  install files in PREFIX"
	echo "  --disable-FOO    configure to not build package FOO"
	echo "  --enable-FOO     configure to build package FOO"
	echo "  --disable-disc   configure to not build optical discs packages"
	echo "  --disable-sout   configure to not build stream output packages"
	echo "  --enable-small   optimize libraries for size with slight speed decrease [DANGEROUS]"
}

BUILD=
HOST=
PREFIX=
PKGS_ENABLE=
PKGS_DISABLE=
BUILD_ENCODERS="1"
BUILD_DISCS="1"

if test ! -f "../../contrib/src/main.mak"
then
	echo "$0 must be run from a subdirectory"
	exit 1
fi

while test -n "$1"
do
	case "$1" in
		--build=*)
			BUILD="${1#--build=}"
			;;
		--help|-h)
			usage
			exit 0
			;;
		--host=*)
			HOST="${1#--host=}"
			;;
		--prefix=*)
			PREFIX="${1#--prefix=}"
			;;
		--disable-disc)
			BUILD_DISCS=
			;;
		--disable-sout)
			BUILD_ENCODERS=
			;;
		--enable-small)
			ENABLE_SMALL=1
			;;
		--disable-*)
			PKGS_DISABLE="${PKGS_DISABLE} ${1#--disable-}"
			;;
		--enable-*)
			PKGS_ENABLE="${PKGS_ENABLE} ${1#--enable-}"
			;;
		*)
			echo "Unrecognized options $1"
			usage
			exit 1
			;;
	esac
	shift
done

if test -z "$BUILD"
then
	echo -n "Guessing build system... "
	BUILD="`cc -dumpmachine`"
	if test -z "$BUILD"; then
		echo "FAIL!"
		exit 1
	fi
	echo "$BUILD"
fi

if test -z "$HOST"
then
	echo -n "Guessing host system...  "
	HOST="$BUILD"
	echo "$HOST"
fi

if test "$PREFIX"
then
	# strip trailing slash
	PREFIX="${PREFIX%/}"
fi

#
# Prepare files
#
echo "Creating configuration file... config.mak"
exec 3>config.mak
cat >&3 << EOF
# This file was automatically generated.
# Any change will be overwritten if ../bootstrap is run again.
BUILD := $BUILD
HOST := $HOST
PKGS_DISABLE := $PKGS_DISABLE
PKGS_ENABLE := $PKGS_ENABLE
EOF

add_make()
{
	while test -n "$1"
	do
		echo "$1" >&3
		shift
	done
}

add_make_enabled()
{
	while test -n "$1"
	do
		add_make "$1 := 1"
		shift
	done
}

check_macosx_sdk()
{
   [ -z "${OSX_VERSION}" ] && echo "OSX_VERSION not specified, assuming 10.5" && OSX_VERSION=10.5
   SDK="/Developer/SDKs/MacOSX${OSX_VERSION}.sdk"
   if [ ! -d "${SDK}" ]
   then
	   echo "
*** ${SDK} does not exist, please install required SDK, or use export OSX_VERSION=10.6 ***
"
	   exit 1
   fi
   add_make "OSX_VERSION ?= ${OSX_VERSION}"
}

check_android_sdk()
{
	[ -z "${ANDROID_NDK}" ] && echo "You must set ANDROID_NDK environment variable" && exit 1
	add_make "ANDROID_NDK := ${ANDROID_NDK}"
	test -z "${NO_NEON}" && add_make_enabled "HAVE_NEON"
}

test -z "$PREFIX" || add_make "PREFIX := $PREFIX"
test -z "$BUILD_DISCS" || add_make_enabled "BUILD_DISCS"
test -z "$BUILD_ENCODERS" || add_make_enabled "BUILD_ENCODERS"
test -z "$ENABLE_SMALL" || add_make_enabled "ENABLE_SMALL"

#
# Checks
#
OS="${HOST#*-}" # strip architecture
case "${OS}" in
	apple-darwin*)
		check_macosx_sdk
		add_make_enabled "HAVE_MACOSX" "HAVE_DARWIN_OS" "HAVE_BSD"
		;;
	*darwin*)
		add_make_enabled "HAVE_DARWIN_OS" "HAVE_BSD"
		;;
	*bsd*)
		add_make_enabled "HAVE_BSD"
		;;
	linux-androideabi)
		check_android_sdk
		add_make_enabled "HAVE_LINUX" "HAVE_ANDROID"
		add_make "PATH = ${ANDROID_NDK}/toolchains/arm-linux-androideabi-4.4.3/prebuilt/linux-x86/bin/:${PATH}"
		;;
	*linux*)
		add_make_enabled "HAVE_LINUX"
		;;
	*wince*)
		add_make_enabled "HAVE_WINCE"
		;;
	*mingw*)
		add_make_enabled "HAVE_WIN32"
		;;
esac

#
# Results output
#
test -e Makefile && unlink Makefile
ln -sf ../../contrib/src/main.mak Makefile
cat << EOF
Bootstrap completed.

Run "make" to start compilation.

Other targets:
 * make install      same as "make"
 * make prebuilt     fetch and install prebuilt binaries
 * make list         list packages
 * make fetch        fetch required source tarballs
 * make fetch-all    fetch all source tarballs
 * make distclean    clean everything and undo bootstrap
 * make mostlyclean  clean everything except source tarballs
 * make clean        clean everything
 * make package      prepare prebuilt packages
EOF

mkdir -p ../../contrib/tarballs