#!/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.
# 
# It 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
#
# As of October 2026, this script (and the release build) have been updated to
# mainly support macOS 10.14 and above, Xcode 16.4 and wxWidgets 3.2.10 and
# above.  As a result, references and procedures for older platforms have
# been removed.  If an older version is needed, it might be best
# to pull it from an older GitHub release tag.
#
# For more information on using this script, see: docs/README.MAC.DEVELOPERS.md
#

set -eu

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

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

# Only run configure, not make
CONFIG_ONLY=

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

# Minimum macOS version, default comes from the workflow file
MACOS_VER_MIN=

# The path, if not in the default location
SDKPATH=

# "make install" base directory.  For safety, we set it to pwd and don't recommend using "make install".
# You might want to use /usr/local if you intend to install the resulting wxWidgwts build.
WX_PREFIX=`pwd`

# Architectures to build, default comes from the workflow file but can be overriden
# here or with the -a option.
WX_ARCH=

# Build options for the ./configure command.  The defaults are taken from the workflow file
# but can be overriden here.  Setting this overrides all other options, specify all of the configure
# options here.
CONF_OPTS=

# Workflow file for the GitHub release build.  We will fetch some parameters from it.
# Can be overriden with the -w option.
WORKFLOW=`dirname $0`/../.github/workflows/macos-latest.yml



usage() {
    cat <<__END_USAGE__

`basename $0`: Runs the wxWidgets configure script appropriately for building pwsafe, the runs make.

Usage (all flags are optional):
	-a (arm64|x86_64)
	    Build for a single, specified architecture.  The default is taken from the workflow file.

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

        -C
            Configure only, do not run make

	-d
	    Create Debug configuration.  Absence of this flag creates the Release configuration

        -h
            This usage message

	-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

        -m <version>
            Minimum macOS version. The defaut is taken from the workflow file.

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

	-w <Path to macos-latest.yml>
	    Path to the GitHub workflow file from which to retrieve build parameters.  The default
	    is relative to the location of this script.

__END_USAGE__
}

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



while getopts "a:c:Cdhk:m:nw:" opt; do
    case $opt in
        a)
            case "$OPTARG" in
                arm64|x86_64) WX_ARCH=$OPTARG;;
                *) error "Unexpected architecture: \"$OPTARG\" is unrecognized. Supported architectures are \"arm64\", \"x86_64\" or \"arm64,x86_64\" for both.)"
                ;;
            esac
            ;;
        c)
            if [ -x $OPTARG ]
            then
                CONF_SCRIPT=$OPTARG
            else
                error "Missing or non-executable configure script: $OPTARG"
            fi
            ;;
        C)
            CONFIG_ONLY=Y
            ;;
        d)
            WX_DEBUG_FLAG="--enable-debug --enable-debug_info"
            ;;
        k)
            if [ -d $OPTARG ]
            then
                SDKPATH=$OPTARG
            else
                error "Invalid directory passed for SDK path: $OPTARG"
            fi
            ;;
        m)
            MACOS_VER_MIN=$OPTARG
            ;;
        n)
            DRY_RUN="echo "
            ;;
        w)
            WORKFLOW=$OPTARG
            ;;
        ?|h)
            usage
            exit 1
            ;;
    esac
done

# Get the version of wxWidgets source we are building
if [ ! -x $CONF_SCRIPT ]
then
    error "Configure script not found: $CONF_SCRIPT"
fi
WX_VER=`$CONF_SCRIPT -V | grep 'wxWidgets configure' | cut -d' ' -f3`

#
# This is to build the wxOSX/Cocoa version of wxWidgets, which is what is supported on macOS 10.10 and up.
# This code tries to build wxWidgets the same way the Github workflow builds the distribution. 
#
doCocoaBuild() {

    # If the user edited the CONF_OPTS variable, they are responsible for all of it.
    # Othewise, we build it from the GitHub workflow file and any command line options.
    if [ -z $CONF_OPTS ]
    then
        if [ ! -f $WORKFLOW ]
        then
            error "Cannot locate the workflow file: $WORKFLOW"
        fi
        # This is actually where 'make install' will put things.
        WX_BUILD_DIR=$WX_PREFIX

        # Fetch some parameters from the macos-latest.yaml workflow
        if [ -z $WX_ARCH ]
        then
            eval `grep WX_ARCH: $WORKFLOW | sed -E -H -e 's/^\s*(.*):\s*(".*")$/\1=\2/'`
        fi
        if [ -z $MACOS_VER_MIN ]
        then
            eval `grep MACOS_VER_MIN: $WORKFLOW | sed -E -H -e 's/^\s*(.*):\s*(\d.*)$/\1=\2/'`
        fi

        # The configure command in the workflow file uses the above variables.
        # This trick will expand them into the CONF_OPTS variable
        CONF_OPTS=`grep "./configure --enable-universal_binary=" $WORKFLOW | cut -w -f 3-`
        CONF_OPTS=`eval echo $CONF_OPTS`

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

        echo "=============================="
        echo "osx-build-wx options:"
        echo "WX_VER        =" $WX_VER
        echo "WX_ARCH       =" $WX_ARCH
        echo "WX_BUILD_DIR  =" $WX_BUILD_DIR
        echo "WX_DEBUG_FLAG =" $WX_DEBUG_FLAG
        echo "NCPU          =" $NCPU
        echo "SDKFLAG       =" $SDKFLAG
        echo "MACOS_VER_MIN =" $MACOS_VER_MIN
        echo "CONF_OPTS     =" $CONF_OPTS
        echo "=============================="
    else
        echo "=============================="
        echo "CONF_OPTS =" $CONF_OPTS
        echo "=============================="
    fi

	$DRY_RUN $CONF_SCRIPT $CONF_OPTS $SDKFLAG

        if [ -z $CONFIG_ONLY ]
        then
            echo "Running make, this could take a while..."
            $DRY_RUN make -s -j $NCPU
        fi
}

echo Building wxWidgets: $WX_VER for OSX/Cocoa
doCocoaBuild
