#!/bin/sh
#
# This script is used to build wxWidgets on Mac OS X in a way that pwsafe 
# will run on as many MacOS versions as possible when built on that machine.
# That also means the wxWidgets build generated by this script would depend
# on the compilers and SDKs supported on the version of OS X it is running on.
# 
# If was initially written for building wx2.8 on Snow Leopard such that the 
# pwsafe binary would run on OS X 10.4+.  It followed the documentation here:
#   http://wiki.wxwidgets.org/Development:_wxMac
#
# See the sections "Building on Snow Leopard for Snow Leopard" and
# "Building on Snow Leopard for Snow Leopard"
#

# Build release configuration by default
DEBUG_FLAG=--disable-debug

# If unspecified, assume the user is building wxWidgets inside a subdir of its root dir
CONF_SCRIPT=../configure

# GUI toolkit to use.  Could be "carbon" or "cocoa".
TOOLKIT=undefined

# In a dry run, we just echo the commands
DRY_RUN=

# The 10.4 SDK path, if not in the default location (i.e. /Developer/SDKs/MacOSX10.4u.sdk/
SDKPATH=

# This is where gcc, g++ and ld required to build with 10.4 SDK should be
TOOLCHAIN_DIR=

usage() {
    cat <<__END_USAGE__

`basename $1`: Runs wxWidgets configure script appropriately for building pwsafe
              You then run make to actually build it

Usage (all flags are optional):
	-d
	    Create Debug configuration.  Absence of this flag creates the Release configuration

	-c <path to wxWidgets configure script>
	    Default is to assume it is in the parent directory of current dir

	-t (carbon|cocoa)
	    The OS X GUI toolkit to be used, either "carbon" or "cocoa".  wx2.8 only supports carbon

	-n
	    Dry run.  Will just echo the command(s) to be used to build wxWidgets

	-k <SDK directory path>
	    You can get the path to any installed SDK that Xcode is aware of like this
	    xcodebuild -version -sdk macosx10.9 Path

	-t <toolchain dir>
	    Directory where gcc-4.0, g++4.0, etc. should be.  Only required for building wx2.8 on OS X 10.9
__END_USAGE__
}

error() {
	echo "$*" 1>&2
	exit 1
}

while getopts "dc:t:nk:t:" opt; do
	case $opt in
		d)
			DEBUG_FLAG=--enable-debug
			;;
		c)
			if [ -x $OPTARG ]
			then
				CONF_SCRIPT=$OPTARG
			else
				error Missing or non-executable configure script: $OPTARG
			fi
			;;
		t)
			case "$OPTARG" in
				carbon) TOOLKIT=CARBON;;
				cocoa)  TOOLKIT=COCOA;;
				*)      error Unexpected toolkit: $OPTARG. Its either \"carbon\" or \"cocoa\" \(default\) 1>&2
						;;
			esac
			;;	
        k)
            if [ -d $OPTARG ]
            then
                SDKPATH=$OPTARG
            else
				error Invalid directory passed for SDK path: $OPTARG
            fi
            ;;
        t)
            if [ -d $OPTARG ]
            then
                TOOLCHAIN_DIR=$OPTARG
            else
				error Invalid directory passed for toolchain dir: $OPTARG
            fi
            ;;
		n)
			DRY_RUN="echo "
			;;
		?)
            usage $0
			exit 1
			;;
	esac
done

WX_VER=`$CONF_SCRIPT -V | grep 'wxWidgets configure' | cut -d' ' -f3`

# This might be required for 10.7 & 10.8 as well
OSX_VER=`sw_vers -productVersion`
if [ "$OSX_VER" = "10.9" -o "$OSX_VER" \> "10.9" ]
then
    if [ -z "$TOOLCHAIN_DIR" ]
    then
        # This is where gcc-4.x etc. will be found
        TOOLCHAIN_DIR=/Developer/usr/bin
    fi
fi

doCarbonBuild() {

    if [ -z "$SDKPATH" ]
    then
        SDKPATH=/Developer/SDKs/MacOSX10.4u.sdk
    fi

    if [ ! -d "$SDKPATH" ]
    then
        error "You must specify a valid SDK directory with Carbon headers/libs for building wxWidgets version $WX_VER on OS X $OSX_VER"
    fi

    CC=gcc-4.0
    CXX=g++-4.0
    LD=g++-4.0

    if [ -n "$TOOLCHAIN_DIR" ]
    then
        CC=$TOOLCHAIN_DIR/$CC
        CXX=$TOOLCHAIN_DIR/$CXX
        LD=$TOOLCHAIN_DIR/$LD
    fi

	# There is only one way to build wx2.8.  It doesn't require a "toolkit" flag
    # see http://wiki.wxwidgets.org/Development:_wxMac#Building_under_10.6_Snow_Leopard
    MIN_OSX_VER=`/usr/libexec/PlistBuddy -c "Print :DefaultProperties:MACOSX_DEPLOYMENT_TARGET" $SDKPATH/SDKSettings.plist`

    arch_flags="-arch i386 -arch ppc"
    sdk_flags="-mmacosx-version-min=$MIN_OSX_VER -isysroot $SDKPATH"

    CFLAGS="     $arch_flags $sdk_flags"
    CXXFLAGS="   $arch_flags $sdk_flags"
    CPPFLAGS="               $sdk_flags"
    OBJCFLAGS="  $arch_flags $sdk_flags"
    OBJCXXFLAGS="$arch_flags $sdk_flags"

	$DRY_RUN $CONF_SCRIPT --prefix=`pwd`                \
                            CC=$CC                      \
                            CXX=$CXX                    \
                            LD=$LD                      \
                            CFLAGS="$CFLAGS"            \
                            CXXFLAGS="$CXXFLAGS"        \
                            CPPFLAGS="$CPPFLAGS"        \
                            LDFLAGS="$arch_flags"       \
                            OBJCFLAGS="$OBJCFLAGS"      \
                            OBJCXXFLAGS="$OBJCXXFLAGS"  \
                            $DEBUG_FLAG                 \
                            --disable-shared            \
                            --disable-compat24          \
                            --enable-unicode            \
                            --enable-universal-binary   \
                            --with-osx_carbon           \
                            --with-macosx-sdk=$SDKPATH  \
                            --with-macosx-version-min=$MIN_OSX_VER
}

if [ "$WX_VER" \< "2.9.0" ]
then
	echo Building wxWidgets 2.8.X \(or earlier\): $WX_VER on OSX $OSX_VER
	if [ "$TOOLKIT" = "COCOA" ]
	then
		error wxWidgets $WX_VER does not support Cocoa
    else
        doCarbonBuild
	fi

else
	echo Building wxWidgets 2.9.X \(or later\): $WX_VER on OSX $OSX_VER

	if [ "$TOOLKIT" = "CARBON" ]
	then
        echo Please let PasswordSafe devs know why supporting carbon builds of wx2.9+ makes sense
        doCarbonBuild
    else

        # This is about the only cocoa build customization we support because everything
        # else is taken care of via "deployment target" in pwsafe-xcode5.xcodeproj
        SDKFLAG=
        if [ -n "$SDKPATH" ]
        then
            SDKFLAG="--with-macosx-sdk=$SDKPATH"
        fi

        # This is required for C++14
        export CC=clang
        export CPP="clang -E"
        export CXX=clang++
        export CXXCPP="clang++ -E"
        export CXXFLAGS="-std=c++1y -stdlib=libc++"
        export OBJCXXFLAGS="-std=c++1y -stdlib=libc++"
        export LDFLAGS=-stdlib=libc++

        $DRY_RUN $CONF_SCRIPT --prefix=`pwd` $DEBUG_FLAG        \
                              $SDKFLAG                          \
                              --with-osx_cocoa                  \
                              --disable-shared                  \
                              --disable-compat24                \
                              --enable-unicode                  \
                              --with-macosx-version-min=10.7    \
                              --without-liblzma
	fi
fi


