#!/bin/sh

c++ -O3 -I/usr/include -I/usr/local/include -L/usr/lib -L/usr/local/lib -I/opt/homebrew/opt/ncurses/include -L/opt/homebrew/opt/ncurses/lib -o hexagon main.cc src/*.cc -std=c++17 -lncursesw
strip hexagon

mkdir -p Hexagon.app/Contents/MacOS
mkdir -p Hexagon.app/Contents/Resources
mkdir -p Hexagon.app/Contents/Frameworks
cp macos/Hexagon.icns Hexagon.app/Contents/Resources/
cp macos/Info.plist Hexagon.app/Contents/
mv hexagon Hexagon.app/Contents/MacOS/hexagon_real
cp macos/hexagon.sh Hexagon.app/Contents/MacOS/hexagon
chmod +x Hexagon.app/Contents/MacOS/hexagon
chmod +x Hexagon.app/Contents/MacOS/hexagon_real

cd Hexagon.app/Contents/MacOS
install_name_tool -change \
  /opt/homebrew/opt/ncurses/lib/libncursesw.6.dylib \
  @executable_path/../Frameworks/libncursesw.6.dylib \
  hexagon_real

cp /opt/homebrew/opt/ncurses/lib/libncursesw.6.dylib \
  ../Frameworks/
install_name_tool -id @rpath/libncursesw.6.dylib \
  ../Frameworks/libncursesw.6.dylib

cd ../../..
codesign --force --deep --sign - Hexagon.app/Contents/MacOS/hexagon_real
codesign --force --deep --sign - Hexagon.app/Contents/Frameworks/libncursesw.6.dylib

tar zcfv hexagon-macos-aarch64.tar.gz Hexagon.app
rm -rf Hexagon.app
