#!/bin/bash
# The Alpha (sometimes seen as "AlphaCocoa") application is a color-syntax
# highlighting macOS text editor that dates back to the 1980s. 
#	https://alphacocoa.sourceforge.io/
#	https://sourceforge.net/projects/alphacocoa/
# Although it was updated to Intel-64-bit, there has never been an
# Apple Silicon (arm64)-native version (6 years into the arm64 era,
# at this writing).
# I endeavored to see whether it was possible to bring this forward
# combining my own experience with ChatGSFC/Gemini AI.
#
# After quite some wrangling, the answer is yes!
#
#		David Friedlander, ADNET Systems (NASA/GSFC), 14 October 2026

# Start out with getting the source code:
# Code comes from:
#	svn checkout https://svn.code.sf.net/p/alphacocoa/code/Current alphacocoa-code-clean

# Please note that four Tcl-related modules are *not* built with this 
# build procedure: tdom, tnc, vfs, and tclx.  These should not matter for
# the vast majority of users of Alpha.

# Build time on an M4 Max Mac Studio: 82 seconds.


set -e

echo "=== 1. Patching Xcode SDK versions ==="
# Change hardcoded reference to Mac OS X 10.11 to the generic (modern) SDKROOT 
# (SDK= Software Development Kit, related to Apple Xcode in this context.)
find . -name "project.pbxproj" -exec sed -i '' 's/SDKROOT = macosx10.11/SDKROOT = macosx/g' {} +
find . -name "project.pbxproj" -exec sed -i '' 's/macosx10.11/macosx/g' {} +

echo "=== 2. Patching Libraries/Makefile to skip broken extensions ==="
# This is where the troublesome (to build) Tcl extensions are skipped "filter-out".
sed -i '' 's/EXTENSION_TARGETS := $(shell echo ${EXTENSION_NAMES} | tr A-Z a-z)/EXTENSION_TARGETS := $(filter-out tclx tdom vfs tcladdressbook, $(shell echo ${EXTENSION_NAMES} | tr A-Z a-z))/g' Libraries/Makefile
sed -i '' 's/ACTIVE_EXTENSION_TARGETS := $(shell echo ${ACTIVE_EXTENSION_NAMES} | tr A-Z a-z)/ACTIVE_EXTENSION_TARGETS := $(filter-out tclx tdom vfs tcladdressbook, $(shell echo ${ACTIVE_EXTENSION_NAMES} | tr A-Z a-z))/g' Libraries/Makefile

echo "=== 3. Pre-creating missing directories & dummy files ==="
mkdir -p Utils/Lexers/build
mkdir -p Help/Extensions
mkdir -p Help/Tcllib
mkdir -p tools/alphacocoa/current/tools
touch tools/alphacocoa/current/tools/alphacocoa.cfg
touch Tools/makeAlphacocoarc && chmod +x Tools/makeAlphacocoarc
mkdir -p build/Debug/Alpha.app/Contents
touch build/Debug/Alpha.app/Contents/Info.plist

echo "=== 4. Neutering the broken Xcode Install Scripts ==="
# Tools/runPhases/InstallTclFramework is a 26-line script that 
# we don't really need, so this script simply zeroes it out.
# Same for InstallTclExtensions (15 lines).
cp -p Tools/runPhases/InstallTclFramework Tools/runPhases/InstallTclFramework.orig
echo "#!/bin/bash" > Tools/runPhases/InstallTclFramework
echo "exit 0" >> Tools/runPhases/InstallTclFramework
chmod +x Tools/runPhases/InstallTclFramework

# InstallTclExtensions	runs "Tools/installExtension" (which we do by hand in Step 8)
cp -p Tools/runPhases/InstallTclExtensions Tools/runPhases/InstallTclExtensions.orig
echo "#!/bin/bash" > Tools/runPhases/InstallTclExtensions
echo "exit 0" >> Tools/runPhases/InstallTclExtensions
chmod +x Tools/runPhases/InstallTclExtensions

echo "=== 5. Exporting build environment ==="
export ROOT_DIR="$PWD"
export ALPHACOCOA_DIR="$ROOT_DIR"
export ALPHA_CURRENT_DIR="$ROOT_DIR"
export ALPHA_LIBS_DIR="$ROOT_DIR/Libraries"
export ALPHA_LIBRARIES_DIR="$ROOT_DIR/Libraries"
export ALPHA_TOOLS_DIR="$ROOT_DIR/Tools"
export ALPHA_HELP_DIR="$ROOT_DIR/Help"
export ALFC_TRGT_APP_DIR="$ROOT_DIR/build/Debug/Alpha.app"
export TCL_TRGT_DIR="$ROOT_DIR/Libraries/Frameworks/Tcl/8.6.10"
export CC="clang -fblocks -Wno-error=implicit-function-declaration"

echo "=== 6. Building Lexers and Libraries ==="
make lexers
make libs-all

echo "=== 7. Building Xcode Core App ==="
# We need to undo the extra CC definitions from Step 5 because Alpha's own code
# does not need it (and work with it so defined).
unset CC
make xc-all || true
make xc-all

echo "=== 8. Injecting Tcl Framework, Extensions, and Fixing Linkage ==="
mkdir -p "$ROOT_DIR/build/Debug/Alpha.app/Contents/Frameworks"
cp -R "$ROOT_DIR/Libraries/Frameworks/Tcl/8.6.10/Tcl.framework" "$ROOT_DIR/build/Debug/Alpha.app/Contents/Frameworks/"

env ALPHACOCOA_DIR="$ROOT_DIR" \
    ALPHA_CURRENT_DIR="$ROOT_DIR" \
    ALPHA_LIBRARIES_DIR="$ROOT_DIR/Libraries" \
    ALPHA_TOOLS_DIR="$ROOT_DIR/Tools" \
    ALFC_TRGT_APP_DIR="$ROOT_DIR/build/Debug/Alpha.app" \
    "$ROOT_DIR/Tools/installExtension" -a || true

install_name_tool -id @executable_path/../Frameworks/Tcl.framework/Tcl "$ROOT_DIR/build/Debug/Alpha.app/Contents/Frameworks/Tcl.framework/Tcl"

OLD_TCL=$(otool -L "$ROOT_DIR/build/Debug/Alpha.app/Contents/MacOS/Alpha" | grep "Tcl.framework" | awk '{print $1}')
if [ ! -z "$OLD_TCL" ]; then
    install_name_tool -change "$OLD_TCL" @executable_path/../Frameworks/Tcl.framework/Tcl "$ROOT_DIR/build/Debug/Alpha.app/Contents/MacOS/Alpha"
fi

echo "=== 9. Signing App to bypass Gatekeeper ==="
/usr/bin/xattr -cr "$ROOT_DIR/build/Debug/Alpha.app"
codesign --force --deep --sign - "$ROOT_DIR/build/Debug/Alpha.app"

echo "=== DONE! Native Apple Silicon app is ready at build/Debug/Alpha.app ==="
