#! /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 "  --no-checksums      don't verify checksums (allows to replace tarballs)"
	echo "  --disable-downloads don't download packages from the internet"
    echo "  --cache-dir=DIR     the directory where contrib tarballs are cached"
	echo "  --enable-debug      build with debug symbol and extra checks (disabled by default)"
	echo "  --enable-lto        build with link-time optimization (disabled by default)"
	echo "  --enable-asan       build with address sanitizer (disabled by default)"
	echo "  --ignore-system-libs  build packages regardless of system detection (disabled by default)"
	echo "  --disable-FOO       configure to not build package FOO"
	echo "  --enable-FOO        configure to build package FOO"
}

BUILD=
HOST=
PREFIX=
PKGS_ENABLE=
PKGS_DISABLE=
CACHE_BUILD=
CONF_TARBALLS=
ENABLE_ASAN=
ENABLE_LTO=
IGNORE_SYSTEM_LIBS=

if test ! -f "../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
			;;
		--no-checksums)
			DISABLE_CONTRIB_CHECKSUMS="TRUE"
			;;
		--disable-downloads)
			DISABLE_CONTRIB_DOWNLOADS="TRUE"
			;;
		--cache-dir=*)
			CACHE_DIR="${1#--cache-dir=}"
			CONF_TARBALLS="${CACHE_DIR}"
			;;
		--cache-builds)
			CACHE_BUILD=1
			;;
		--enable-debug)
			ENABLE_DEBUG=1
			;;
		--enable-asan)
			ENABLE_ASAN=1
			;;
		--ignore-system-libs)
			IGNORE_SYSTEM_LIBS=1
			;;
		--enable-lto)
			ENABLE_LTO=1
			;;
		--host=*)
			HOST="${1#--host=}"
			;;
		--prefix=*)
			PREFIX="${1#--prefix=}"
			;;
		--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:-$(command -v cc || command -v gcc)} -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 || exit $?
cat >&3 << EOF
# This file was automatically generated.
# Any change will be overwritten if ../bootstrap is run again.
BUILD := $BUILD
HOST := $HOST
CROSS_COMPILE ?= $HOST-
PKGS_DISABLE := $PKGS_DISABLE
PKGS_ENABLE := $PKGS_ENABLE
DISABLE_CONTRIB_DOWNLOADS := $DISABLE_CONTRIB_DOWNLOADS
DISABLE_CONTRIB_CHECKSUMS := $DISABLE_CONTRIB_CHECKSUMS
CONF_TARBALLS := $CONF_TARBALLS
CACHE_DIR := $CACHE_DIR
CACHE_BUILD := $CACHE_BUILD
ENABLE_DEBUG := $ENABLE_DEBUG
ENABLE_ASAN := $ENABLE_ASAN
ENABLE_LTO := $ENABLE_LTO
IGNORE_SYSTEM_LIBS := $IGNORE_SYSTEM_LIBS
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_ios_sdk()
{
   if test -z "$SDKROOT"
   then
      SDKROOT=`xcode-select -print-path`/Platforms/${IOS_TARGET_PLATFORM}.platform/Developer/SDKs/${IOS_TARGET_PLATFORM}${SDK_VERSION}.sdk
      echo "SDKROOT not specified, assuming $SDKROOT"
   else
      SDKROOT="$SDKROOT"
   fi

   if [ ! -d "${SDKROOT}" ]
   then
      echo "*** ${SDKROOT} does not exist, please install required SDK, or set SDKROOT manually. ***"
      exit 1
   fi
   add_make "IOS_SDK=${SDKROOT}"
   add_make "IOS_TARGET_PLATFORM=${IOS_TARGET_PLATFORM}"
}

check_macosx_sdk()
{
   if [ -z "${OSX_VERSION}" ]
   then
      OSX_VERSION=`xcrun --show-sdk-version`
      echo "OSX_VERSION not specified, assuming $OSX_VERSION"
   fi
   if test -z "$SDKROOT"
   then
      SDKROOT=`xcode-select -print-path`/Platforms/MacOSX.platform/Developer/SDKs/MacOSX$OSX_VERSION.sdk
      echo "SDKROOT not specified, assuming $SDKROOT"
   fi

   if [ ! -d "${SDKROOT}" ]
   then
      SDKROOT_NOT_FOUND=`xcode-select -print-path`/Platforms/MacOSX.platform/Developer/SDKs/MacOSX$OSX_VERSION.sdk
      SDKROOT=`xcode-select -print-path`/SDKs/MacOSX$OSX_VERSION.sdk
      echo "SDKROOT not found at $SDKROOT_NOT_FOUND, trying $SDKROOT"
   fi
   if [ ! -d "${SDKROOT}" ]
   then
      SDKROOT_NOT_FOUND="$SDKROOT"
      SDKROOT=`xcrun --show-sdk-path`
      echo "SDKROOT not found at $SDKROOT_NOT_FOUND, trying $SDKROOT"
   fi

   if [ ! -d "${SDKROOT}" ]
   then
      echo "*** ${SDKROOT} does not exist, please install required SDK, or set SDKROOT manually. ***"
      exit 1
   fi

   add_make "MACOSX_SDK=${SDKROOT}"
   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}"
	[ -z "${ANDROID_ABI}" ] && echo "You must set ANDROID_ABI environment variable" && exit 1
	add_make "ANDROID_ABI := ${ANDROID_ABI}"
	[ -z "${ANDROID_API}" ] && echo "You should set ANDROID_API environment variable (using default android-9)" && ANDROID_API := android-9
	add_make "ANDROID_API := ${ANDROID_API}"
	[ ${ANDROID_ABI} = "armeabi-v7a" ] && add_make_enabled "HAVE_NEON"
	[ ${ANDROID_ABI} = "armeabi-v7a" ] && add_make_enabled "HAVE_ARMV7A"
	[ ${ANDROID_ABI} = "armeabi" -a -z "${NO_ARMV6}" ] && add_make_enabled "HAVE_ARMV6"
}

test -z "$PREFIX" || add_make "PREFIX := $PREFIX"

#
# Checks
#
OS="${HOST#*-}" # strip architecture
case "${OS}" in
	apple-darwin*)
		add_make_enabled "HAVE_DARWIN_OS" "HAVE_BSD"
		if test -z "$BUILDFORIOS"
		then
			check_macosx_sdk
			add_make_enabled "HAVE_MACOSX"
		else
			check_ios_sdk
			add_make_enabled "HAVE_IOS" "HAVE_NEON"
		fi
		;;
	*bsd*)
		add_make_enabled "HAVE_BSD"
		;;
	*android*)
		check_android_sdk
		add_make_enabled "HAVE_LINUX" "HAVE_ANDROID"
		case "${HOST}" in
			*arm*)
			add_make "PLATFORM_SHORT_ARCH := arm"
			;;
			*i686*)
			add_make "PLATFORM_SHORT_ARCH := x86"
			;;
			*mipsel*)
			add_make "PLATFORM_SHORT_ARCH := mips"
			;;
		esac
		;;
	*linux*)
		add_make_enabled "HAVE_LINUX"
		;;
	*wince*)
		add_make_enabled "HAVE_WINCE"
		;;
	*mingw*)
		add_make_enabled "HAVE_WIN32"
		;;
	*solaris*)
		add_make_enabled "HAVE_SOLARIS"
		;;
esac

# If the DISABLE_PIPEWIRE variable is set, then we build FFmpeg
# without pipewiregrab (see contrib/src/ffmpeg/rules.mak)
# This is currently needed because some of the Linux distributions
# we support don't have a recent enough version of PipeWire:
# - Debian 11
# - Ubuntu 20.04
# - openSUSE Leap 15.4
# However, we should be able to get rid of this in the future once
# we stop supporting the above platforms.
test "$DISABLE_PIPEWIRE" && add_make_enabled "DISABLE_PIPEWIRE"

#
# Results output
#
test -e Makefile && unlink Makefile
ln -sf ../src/main.mak Makefile || exit $?
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 list-tarballs list required source tarballs
 * 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
 * make cyclonedx     generate a CycloneDX SBOM file
EOF

mkdir -p ../tarballs || exit $?
