#!/bin/sh
set -x

echo "uname_a=$(uname -a)"
echo "uname_m=$(uname -m)" # x86_64 arm64

xcodebuild -showsdks

# GHA ~202410
# x86_64-apple-darwin21.6.0   lacks needed -I/usr/X11/include
# aarch64-apple-darwin23.6.0  has needed -I/usr/X11/include

for d in /usr/X11/include /opt/X11/include /opt/local/include /usr/local/include
do
    if [ -d "$d" ]
    then
        echo "Directory Exists: $d"
        if [ -z "$x11_include_dir" ] && [ -f "${d}/X11/Xlib.h" ]
        then
            echo "Found Xlib.h: ${d}/X11/Xlib.h"
            x11_include_dir="${d}"
        fi
    fi
done

for d in /usr/X11/lib /opt/X11/lib /opt/local/lib /usr/local/lib
do
    if [ -d "$d" ]
    then
        echo "Directory Exists: $d"
        if [ -z "$x11_library_dir" ] && [ -e "${d}/libX11.dylib" ]
        then
            echo "Found libX11.dylib: ${d}/libX11.dylib"
            x11_library_dir="${d}"
        fi
    fi
done

# This appeared to have the "@8" suffix add to the installed path
brew_prefix_tcl_tk=$(brew --prefix tcl-tk)
echo "brew_prefix_tcl_tk=$(brew --prefix tcl-tk)"
brew_prefix_tcl_tk_at8=$(brew --prefix tcl-tk@8)
echo "brew_prefix_tcl_tk_at8=$(brew --prefix tcl-tk@8)"
# Changed the order to reflect current state to use TCL8 in preference to default,
#  but you can swap/edit these to modify the first-found-is-selected for building.
for d in "${brew_prefix_tcl_tk_at8}" "${brew_prefix_tcl_tk}"
do
    if [ -d "$d" ]
    then
        echo "Directory Exists: $d"
        ls -l "${d}/"
        if [ -z "$tcl_tk_prefix_dir" ]
        then
            if [ -f "${d}/include/tcl.h" ]
            then
                echo "Found tcl-tk: ${d}"
                tcl_tk_prefix_dir="${d}"
            else
                echo "WARNING: Not Found tcl.h in ${d}/include/"
                ls -l "${d}/include/"
                if [ -d "${d}/lib" ]
                then
                    # A subdir exists
                    ls -l "${d}/lib"
                    # TEST does it build if we set
                    echo "Found tcl-tk (lib): ${d}/lib"
                    tcl_tk_prefix_dir="${d}/lib"
                elif [ -d "${d}/include/tcl-tk" ]
                then
                    # A subdir exists
                    ls -l "${d}/include/tcl-tk"
                    # TEST does it build if we set
                    if [ -f "${d}/include/tcl-tk/tcl.h" ]
                    then
                        echo "Found tcl-tk: ${d}/include/tcl-tk"
                        tcl_tk_prefix_dir="${d}/include/tcl-tk"
                    fi
                fi
            fi
        else
            echo "WARNING: Multiple tcl-tk packages found installed (side-by-side) please manually edit this script or fixup configure command line" 1>&2
            echo "WARNING: The version being used for this build is: $tcl_tk_prefix_dir" 1>&2
        fi
    fi
done
# Restores the original script default when the above detection fails
tcl_tk_prefix_dir=${tcl_tk_prefix_dir:-$brew_prefix_tcl_tk}

if [ -n "$x11_include_dir" ]
then
    # On some versions of MacOSX (example macosx12 / XCode 14.2) the tcl-tk
    #  also installs a set of X11 headers that seem incomplete.  At a location
    #  like /usr/local/Cellar/tcl-tk/8.6.15/include/X11/X.h
    #
    # When XQuartz is installed it has a full set of correct X11 headers but
    #  it doesn't provide any explicit -I directory itself, and tcl-tk does
    #  provide -I value, so the incomplete headers end up having priority.
    #CFLAGS="-I/opt/local/include"
    #CFLAGS="-I/opt/X11/include"
    CONFARGS="$CONFARGS --x-includes=$x11_include_dir"
fi

if [ -n "$x11_library_dir" ]
then
    # Example system view from xquartz 2.8.5  and  tcl-tk 8.6.15
    # /opt/X11/lib/libX11.6.dylib (3496912 bytes 2023-01-26)
    # /opt/homebrew/Cellar/libx11/1.8.10/lib/libX11.6.dylib (1025536 bytes 2024-10-07)
    # Note the size difference, the question is ... if the tcl-tk expects to run against
    #  their copy of libX11, is the ABI equivalent and substitutable without crashing ?
    CONFARGS="$CONFARGS --x-libraries=$x11_library_dir"
fi

./configure\
    --with-tcl="${tcl_tk_prefix_dir}"\
    --with-tk="${tcl_tk_prefix_dir}"\
    --with-cairo=$(brew --prefix cairo)/include\
    $CONFARGS\
    "LDFLAGS=-L$(brew --prefix cairo)/lib"
