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

# 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.4.x or later" >&2
        exit 1
    ;;
    powerpc-apple-darwin8)
# Mac OS X 10.4
# Makefile cross compiling is only supported with gcc-4.0
# ENVP defines the oldest environment on which the program will be able to run
# SDK 10.2.8 requires gcc-3.3 (so we cannot use that SDK)
        SDK_TARGET=10.4
        echo "SDK_TARGET = ${SDK_TARGET}" >> config.mak
        echo "ENVP = MACOSX_DEPLOYMENT_TARGET=${SDK_TARGET}" >> config.mak
        echo "MACOSX_SDK = /Developer/SDKs/MacOSX10.4u.sdk" >> config.mak
        CFLAGS_TUNING=" -arch ppc -mtune=G4"
        EXTRA_CFLAGS=" -D\${ENVP} -mmacosx-version-min=\${SDK_TARGET}"
        CC="gcc -isysroot \${MACOSX_SDK}"
        CXX="g++ -isysroot \${MACOSX_SDK}"
        LD="ld -arch ppc -syslibroot \${MACOSX_SDK}"
        echo "PATH = /bin:/usr/bin:/usr/local/bin" >> config.mak
        cat src/Distributions/darwin.mak >> distro.mak
        ;;
    i686-apple-darwin8)
        SDK_TARGET=10.4
        echo "SDK_TARGET = ${SDK_TARGET}" >> config.mak
        echo "HAVE_DARWIN_OS_ON_INTEL = 1" >> config.mak
        echo "ENVP = MACOSX_DEPLOYMENT_TARGET=${SDK_TARGET}" >> config.mak
        echo "MACOSX_SDK = /Developer/SDKs/MacOSX10.4u.sdk" >> config.mak
        CFLAGS_TUNING=" -march=pentium-m -mtune=prescott"
        EXTRA_CFLAGS=" -D\${ENVP} -isysroot \${MACOSX_SDK} -mmacosx-version-min=\${SDK_TARGET}"
        EXTRA_LDFLAGS=" -isysroot \${MACOSX_SDK} -Wl,-syslibroot,\${MACOSX_SDK}"
        echo "PATH = /bin:/usr/bin:/usr/local/bin" >> config.mak
        cat src/Distributions/darwin.mak >> distro.mak
        ;;
    powerpc-apple-darwin9)
        SDK_TARGET=10.4
        echo "SDK_TARGET = ${SDK_TARGET}" >> config.mak
        echo "ENVP = MACOSX_DEPLOYMENT_TARGET=${SDK_TARGET}" >> config.mak
        echo "MACOSX_SDK = /Developer/SDKs/MacOSX10.4u.sdk" >> config.mak
        CFLAGS_TUNING=" -arch ppc -mtune=G4"
        EXTRA_CFLAGS=" -D\${ENVP} -mmacosx-version-min=\${SDK_TARGET}"
        CC="gcc -isysroot \${MACOSX_SDK}"
        CXX="g++ -isysroot \${MACOSX_SDK}"
        LD="ld -arch ppc -syslibroot \${MACOSX_SDK}"
        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.4
        echo "SDK_TARGET = ${SDK_TARGET}" >> config.mak
        echo "HAVE_DARWIN_OS_ON_INTEL = 1" >> config.mak
        echo "ENVP = MACOSX_DEPLOYMENT_TARGET=${SDK_TARGET}" >> config.mak
        echo "MACOSX_SDK = /Developer/SDKs/MacOSX10.4u.sdk" >> config.mak
        CFLAGS_TUNING=" -march=pentium-m -mtune=prescott"
        EXTRA_CFLAGS=" -D\${ENVP} -isysroot \${MACOSX_SDK} -mmacosx-version-min=\${SDK_TARGET}"
        EXTRA_LDFLAGS=" -isysroot \${MACOSX_SDK} -Wl,-syslibroot,\${MACOSX_SDK}"
        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
        ;;
    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
        ;;
    *mingw32*)
            echo "HAVE_WIN32 = 1" >> config.mak
            echo "PKG_CONFIG_PATH = \$(PREFIX)/lib/pkgconfig" >> config.mak
            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
        ;;
    *)
            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
                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" >> config.mak
elif which curl >/dev/null; 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 test -f /sw/bin/svn; then
        echo "SVN = /sw/bin/svn" >> config.mak
elif test -f /usr/local/bin/svn; then
        echo "SVN = /usr/local/bin/svn" >> config.mak
elif test -f /usr/bin/svn; then
    echo "SVN = /usr/bin/svn" >> config.mak
elif test -f /opt/local/bin/svn; then
    echo "SVN = /opt/local/bin/svn" >> config.mak
elif svn help >/dev/null 2>&1; then
        echo "SVN = `which svn`" >> config.mak
else
        echo "You do not have a subversion client installed." >&2
fi
