#!/bin/sh
# ***************************************************************************
# bootstrap : Set up config.mak
# ***************************************************************************
# Copyright (C) 2003-2009 the VideoLAN team
# $Id$
#
# Authors: Christophe Massiot <massiot@via.ecp.fr>
#          Derk-Jan Hartman <hartman at videolan dot org>
#          Felix Paul Kühne <fkuehne at videolan dot org>
#
# 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.
# ***************************************************************************

if test "$#" -gt "2" -o "$1" = "-h" -o "$1" = "--help" ; then
    echo "Usage: $0 [host [distro]]" >&2
    echo "  Prepare config.mak and distro.mak file." >&2
    exit 1
fi

LANG=C
export LANG
set -e
set -x

BUILD=`gcc -dumpmachine`
if test "$#" -ge "1"; then
    HOST="$1"
else
    HOST="$BUILD"
fi

DISTRO=
if test "$#" = "2"; then
    echo "Forcing distro.mak to use $2.mak"
    DISTRO="$2"
    HOST="$1"
fi

rm -f config.mak
{
    echo "# Automatically generated by bootstrap."
    echo "# Make changes if you know what you're doing."
} > config.mak

rm -f distro.mak
{
    echo "# Automatically generated by bootstrap"
    echo "# Make changes if you know what you're doing."
} > distro.mak

if test "$HOST" != "$BUILD"; then
    CC="${HOST}-gcc"
    CXX="${HOST}-g++"
    LD="${HOST}-ld"
    RANLIB="${HOST}-ranlib"
    AR="${HOST}-ar"
    STRIP="${HOST}-strip"
fi

case $HOST in
    *powerpc*|*ppc*)
         ARCH="ppc"
     ;;
    *darwin10*)
         ARCH="x86_64"
     ;;
    *86_64*)
          ARCH="x86_64"
     ;;
    *86*)
          ARCH="i386"
     ;;
esac
echo "ARCH = $ARCH" >> config.mak

# Check the HAVE_{OS}
case $HOST in
    *darwin*)
        echo "HAVE_DARWIN_OS = 1" >> config.mak
        echo "HAVE_BSD = 1" >> config.mak
    ;;
    *linux*)
        echo "HAVE_LINUX = 1" >> config.mak
    ;;
    *bsd*)
        echo "HAVE_BSD = 1" >> config.mak
    ;;
    *wince*)
        echo "HAVE_WINCE = 1" >> config.mak
    ;;
    *beos*)
        echo "HAVE_BEOS = 1" >> config.mak
    ;;
esac

case $HOST in
    ppc-darwin)
        # Mac OS X < 10.4
        echo "Your version of Mac OS X is too old!" >&2
        echo "Compiling VLC requires 10.5.x or later" >&2
        exit 1
    ;;
    powerpc-apple-darwin8)
        echo "Your version of Mac OS X is too old!" >&2
        echo "Compiling and running VLC requires 10.5.x or later" >&2
        exit 1
    ;;
    i686-apple-darwin8)
        echo "Your version of Mac OS X is too old!" >&2
        echo "Compiling and running VLC requires 10.5.x or later" >&2
        exit 1
    ;;
    powerpc-apple-darwin9)
# Mac OS X 10.5 with Xcode 3, specific rules apply
        SDK_TARGET=10.5
        echo "SDK_TARGET = ${SDK_TARGET}" >> config.mak
        echo "ENVP = MACOSX_DEPLOYMENT_TARGET=${SDK_TARGET}" >> config.mak
        echo "MACOSX_SDK = /Developer/SDKs/MacOSX10.5.sdk" >> config.mak
        echo "HAVE_DARWIN_9 = 1" >> config.mak
        HAVE_DARWIN_9=1
        CFLAGS_TUNING=" -arch ppc -mtune=G4"
        EXTRA_CFLAGS=" -D\${ENVP} -isysroot \${MACOSX_SDK} -mmacosx-version-min=\${SDK_TARGET}"
        EXTRA_LDFLAGS=" -arch ppc -isysroot \${MACOSX_SDK} -mmacosx-version-min=\${SDK_TARGET} -Wl,-syslibroot,\${MACOSX_SDK}"
        CC="/Developer/usr/bin/gcc-4.2"
        CXX="/Developer/usr/bin/g++-4.2"
        LD="ld -arch ppc -syslibroot \${MACOSX_SDK} -mmacosx-version-min=\${SDK_TARGET}"
        echo "PATH = /bin:/usr/bin:/usr/local/bin:" >> config.mak
        cat src/Distributions/darwin.mak >> distro.mak
        if test -e /Developer/SDKs; then
            echo "SDKs found. Everything is fine." >&2
        else
            echo "ERROR:\nYour Developer Tools' SDKs were not found.\nYou need to add extra symbolic links to /Developer to achieve correctly\nbuilt contribs.\nHave a look at the OSX-Compile-HOWTO for details." >&2
            exit 1
        fi
    ;;
    i686-apple-darwin9)
        SDK_TARGET=10.5
        MIN_LD_VERSION_WITH_TEXT_RELOCATION=81
        echo "SDK_TARGET = ${SDK_TARGET}" >> config.mak
        echo "HAVE_DARWIN_OS_ON_INTEL = 1" >> config.mak
        echo "HAVE_DARWIN_9 = 1" >> config.mak
        HAVE_DARWIN_9=1
        if test `ld -v 2> /dev/stdout | sed -E 's/.*ld64-([0-9]+).*/\1/'` -lt ${MIN_LD_VERSION_WITH_TEXT_RELOCATION}; then
            echo "ERROR: Your ld version is unable to compile VLC.\nUpdate to Xcode 3.1 or higher." >&2
            exit 1
        fi
        echo "ENVP = MACOSX_DEPLOYMENT_TARGET=${SDK_TARGET}" >> config.mak
        echo "MACOSX_SDK = /Developer/SDKs/MacOSX10.5.sdk" >> config.mak
        CFLAGS_TUNING=" -march=prescott -mtune=generic -arch i386"
        EXTRA_CFLAGS=" -D\${ENVP} -isysroot \${MACOSX_SDK} -mmacosx-version-min=\${SDK_TARGET}"
        EXTRA_LDFLAGS=" -arch i386 -isysroot \${MACOSX_SDK} -Wl,-syslibroot,\${MACOSX_SDK} -mmacosx-version-min=\${SDK_TARGET}"
        CC="/Developer/usr/bin/gcc-4.2"
        CXX="/Developer/usr/bin/g++-4.2"
        echo "PATH = /bin:/usr/bin:/usr/local/bin" >> config.mak
        cat src/Distributions/darwin.mak >> distro.mak
        if test -e /Developer/SDKs; then
            echo "SDKs found. Everything is fine." >&2
        else
            echo "ERROR:\nYour Developer Tools' SDKs were not found.\nYou need to add extra symbolic links to /Developer to achieve correctly\nbuilt contribs.\nHave a look at the OSX-Compile-HOWTO for details." >&2
            exit 1
        fi
    ;;
    *-apple-darwin10)
        SDK_TARGET=10.5
        echo "SDK_TARGET = ${SDK_TARGET}" >> config.mak
        echo "HAVE_DARWIN_OS_ON_INTEL = 1" >> config.mak
        echo "HAVE_DARWIN_9 = 1" >> config.mak
        echo "HAVE_DARWIN_10 = 1" >> config.mak
        HAVE_DARWIN_10=1
        echo "ENVP = MACOSX_DEPLOYMENT_TARGET=${SDK_TARGET}" >> config.mak
        echo "MACOSX_SDK = /Developer/SDKs/MacOSX${SDK_TARGET}.sdk" >> config.mak
        if test $ARCH = "x86_64"; then
            CFLAGS_TUNING=" -march=core2 -mtune=core2"
        else
            CFLAGS_TUNING=" -march=prescott -mtune=core2"
        fi
        EXTRA_CFLAGS=" -D\${ENVP} -isysroot \${MACOSX_SDK} -mmacosx-version-min=\${SDK_TARGET}"
        EXTRA_LDFLAGS=" -isysroot \${MACOSX_SDK} -Wl,-syslibroot,\${MACOSX_SDK} -mmacosx-version-min=\${SDK_TARGET}"
        echo "PATH = /bin:/usr/bin:/usr/local/bin" >> config.mak
        CC="gcc"
        CXX="g++"
        cat src/Distributions/darwin.mak >> distro.mak
        if test -e /Developer/SDKs; then
            echo "SDKs found. Everything is fine." >&2
        else
            echo "ERROR:\nYour Developer Tools' SDKs were not found.\nYou need to add extra symbolic links to /Developer to achieve correctly\nbuilt contribs.\nHave a look at the OSX-Compile-HOWTO for details." >&2
            exit 1
        fi
    ;;
    i586-pc-beos)
        if test -f /boot/beos/system/lib/libbind.so; then
            EXTRA_LDFLAGS=" -lbind -lsocket"
        else
            EXTRA_LDFLAGS=" -lnet"
        fi
        if test "$HOST" = "$BUILD"; then
#           Compiling on BeOS
            PATH=".:/boot/home/config/bin:/bin:/boot/apps"
            PATH="${PATH}:/boot/preferences:/boot/beos/apps"
            PATH="${PATH}:/boot/beos/preferences"
            PATH="${PATH}:/boot/develop/tools/gnupro/bin"
            echo "PATH = ${PATH}" >> config.mak
            LIBRARY_PATH="${LIBRARY_PATH}:%A/lib:/boot/home/config/lib"
            LIBRARY_PATH="${LIBRARY_PATH}:/boot/beos/system/lib"
            echo "LIBRARY_PATH = ${LIBRARY_PATH}" >> config.mak
        else
#           Cross compiling from Linux
            PATH="/opt/beos-tools/bin:/opt/cross-tools/bin"
            PATH="${PATH}:/usr/bin:/bin"
            echo "PATH = ${PATH}" >> config.mak
        fi
        cat src/Distributions/beos.mak >> distro.mak
    ;;
    *mingw32ce)
        echo "HAVE_WINCE = 1" >> config.mak
        echo "PKG_CONFIG_PATH = \$(PREFIX)/lib/pkgconfig" >> config.mak
        cat src/Distributions/wince.mak >> distro.mak
    ;;
    x86_64-*-mingw*)
        echo "HAVE_WIN64 = 1" >> config.mak
        echo "HAVE_WIN32 = 1" >> config.mak
        echo "PKG_CONFIG_PATH = \$(PREFIX)/lib/pkgconfig" >> config.mak
           EXTRA_CFLAGS="-O3"
        cat src/Distributions/win32.mak >> distro.mak
    ;;
    *mingw32*)
        echo "HAVE_WIN32 = 1" >> config.mak
        echo "PKG_CONFIG_PATH = \$(PREFIX)/lib/pkgconfig" >> config.mak
        EXTRA_CFLAGS="-O3"
        cat src/Distributions/win32.mak >> distro.mak
    ;;
    i686-pc-cygwin)
        echo "HAVE_WIN32 = 1" >> config.mak
        echo "HAVE_CYGWIN = 1" >> config.mak
        CC="gcc -mno-cygwin -isystem /usr/include/mingw"
        CXX="g++ -mno-cygwin -isystem /usr/include/mingw"
        HOST=`$CC -dumpmachine`
        EXTRA_CFLAGS=" -mno-cygwin -isystem /usr/include/mingw"
        EXTRA_CPPFLAGS=" -mno-cygwin -isystem /usr/include/mingw"
        EXTRA_LDFLAGS=" -mno-cygwin"
        echo "PKG_CONFIG_PATH = \$(PREFIX)/lib/pkgconfig" >> config.mak
        cat src/Distributions/win32.mak >> distro.mak
    ;;
    arm-wince-pe)
        echo "PKG_CONFIG_PATH = \$(PREFIX)/lib/pkgconfig" >> config.mak
        EXTRA_CPPFLAGS=" -D_WIN32_WCE"
        cat src/Distributions/wince.mak >> distro.mak
    ;;
    armeb-linux-uclibc)
        echo "HAVE_UCLIBC = 1" >> config.mak
        echo "HAVE_BIGENDIAN = 1" >> config.mak
        EXTRA_CFLAGS="-Os -march=armv5 -msoft-float"
        if test ! -z "${DISTRO}";  then
            cat src/Distributions/${DISTRO}.mak >> distro.mak
        else
            cat src/Distributions/unix.mak >> distro.mak
        fi
    ;;
    *86_64*linux*)
        EXTRA_CFLAGS=" -fPIC"
        EXTRA_CPPFLAGS=" -fPIC"
        EXTRA_LDFLAGS=" -L/usr/lib64"
        echo "LIBRARY_PATH = /usr/lib64" >> config.mak
        echo "PKG_CONFIG_PATH = /usr/lib64/pkgconfig" >> config.mak
        echo "PKG_CONFIG_LIBDIR = /usr/lib64/pkgconfig" >> config.mak
        cat src/Distributions/unix.mak >> distro.mak
    ;;
    *)
        if test "$HOST" = "$BUILD"; then
            if test -d "/usr/lib/pkgconfig"; then
                if test -z "$PKG_CONFIG_PATH"; then
                    echo "PKG_CONFIG_PATH = /usr/lib/pkgconfig" >> config.mak
                fi
                if test -z "$PKG_CONFIG_LIBDIR"; then
                    echo "PKG_CONFIG_LIBDIR = /usr/lib/pkgconfig" >> config.mak
                fi
            fi
            # Try to match distribution
            if test -f /etc/fedora-release; then
                cat src/Distributions/fedora.mak >> distro.mak
            elif test -f /etc/maemo_version; then
                cat src/Distributions/maemo.mak >> distro.mak
            else
                # Default Unix-like systems
                cat src/Distributions/unix.mak >> distro.mak
            fi
        else # unknown distro
            if test ! -z "${DISTRO}";  then
                cat src/Distributions/${DISTRO}.mak >> distro.mak
            else
                cat src/Distributions/unix.mak >> distro.mak
            fi
        fi
    ;;
esac

echo "BUILD = $BUILD" >> config.mak
echo "HOST = $HOST" >> config.mak

echo "PREFIX = `pwd`" >> config.mak

echo "CC = ${CC}" >> config.mak
echo "CXX = ${CXX}" >> config.mak
echo "LD = ${LD}" >> config.mak
echo "RANLIB = ${RANLIB}" >> config.mak
echo "AR = ${AR}" >> config.mak
echo "STRIP = ${STRIP}" >> config.mak
echo "EXTRA_CFLAGS = ${CFLAGS_TUNING} ${EXTRA_CFLAGS}" >> config.mak
echo "EXTRA_CPPFLAGS = ${EXTRA_CPPFLAGS} -isystem \$(PREFIX)/include" >> config.mak
echo "EXTRA_LDFLAGS = ${EXTRA_LDFLAGS}" >> config.mak
echo "EXTRA_PATH = ${EXTRA_PATH}" >> config.mak

if wget --version >/dev/null 2>&1; then
    echo "WGET = `which wget` -c --passive" >> config.mak
elif test -z `curl --version >/dev/null 2>&1`; then
    echo "WGET = `which curl` -L -O" >> config.mak
else
    echo "You need at least wget or curl to fetch the packages." >&2
    exit 1
fi

if svn --version >/dev/null 2>&1; then
    echo "SVN = `which svn`" >> config.mak
else
    echo "You do not have a subversion client in your PATH." >&2
fi

if git --version>/dev/null 2>&1; then
    echo "GIT = `which git`" >> config.mak
else
    echo "You do not have a Git client in your PATH." >&2
fi

if test -z "$CONTRIBS_RELEASE"; then
    echo "EXTRA_CFLAGS += -DNDEBUG" >> config.mak
    set +x
    echo
    echo "*****************************************************************"
    echo "* If you need contribs with all debug information, run this     *"
    echo "* line and compile the libraries on your own.                   *"
    echo "* CONTRIBS_RELEASE=no ./bootstrap                               *"
    echo "*****************************************************************"
fi

if test $HAVE_DARWIN_9; then
    set +x
    echo
    echo "*****************************************************************"
    echo "* We are using GCC-4.2 on OS X, so compilation WILL FAIL if it  *"
    echo "* is NOT installed. *"
    echo "*****************************************************************"
fi

if test $HAVE_DARWIN_10; then
    set +x
    echo
    echo "*****************************************************************"
    echo "* You are running Darwin 10.                                    *"
    echo "* For testing purposes, VLC will be compiled in 64bit mode      *"
    echo "* using the 10.5 SDK for backward-compatibility.                *"
    echo "*****************************************************************"
    set -x
fi
