#!/usr/bin/env bash

# Exit on error
set -e

# Number of parallel jobs
jobs=$(sysctl -n hw.activecpu)

# Build libcdio
cd tools

./build-codecs libcdio libcdio-paranoia

if [[ ${MACOSX_DEPLOYMENT_TARGET:0:2} = 11 ]]; then
  export BUILD_ARM64=True

  mkdir -p arm64
  cd arm64
  ../build-codecs libcdio libcdio-paranoia
  cd ..

  lipo -create libcdio-2.3.0/lib/driver/.libs/libcdio.19.dylib arm64/libcdio-2.3.0/lib/driver/.libs/libcdio.19.dylib -output libcdio.19.dylib
  lipo -create libcdio-paranoia-10.2+2.0.2/lib/cdda_interface/.libs/libcdio_cdda.2.dylib arm64/libcdio-paranoia-10.2+2.0.2/lib/cdda_interface/.libs/libcdio_cdda.2.dylib -output libcdio_cdda.2.dylib
  lipo -create libcdio-paranoia-10.2+2.0.2/lib/paranoia/.libs/libcdio_paranoia.2.dylib arm64/libcdio-paranoia-10.2+2.0.2/lib/paranoia/.libs/libcdio_paranoia.2.dylib -output libcdio_paranoia.2.dylib

  sudo cp libcdio*.dylib /usr/local/lib

  unset BUILD_ARM64
fi

cd ..

# Build smooth
cd smooth

CONFIG="release"

make config=$CONFIG -j$jobs
sudo make config=$CONFIG -j$jobs install

cd ..

# Build BoCA
cd boca

CONFIG="release"

make config=$CONFIG -j$jobs
sudo make config=$CONFIG -j$jobs install

cd ..

# Build fre:ac
CONFIG="release"

make config=$CONFIG -j$jobs
sudo make config=$CONFIG -j$jobs install

# Build codecs and DSP libraries
cd tools

./build-codecs faad2 mp4v2
./build-codecs lame mpg123
./build-codecs libogg opus speex vorbis
./build-codecs flac mac musepack wavpack
./build-codecs ffmpeg libsndfile
./build-codecs libsamplerate rnnoise rubberband

if [[ ${MACOSX_DEPLOYMENT_TARGET:0:2} = 11 ]]; then
  export BUILD_ARM64=True

  mkdir -p arm64
  cd arm64
  ../build-codecs faad2 mp4v2
  ../build-codecs lame mpg123
  ../build-codecs libogg opus speex vorbis
  ../build-codecs flac mac musepack wavpack
  ../build-codecs ffmpeg libsndfile
  ../build-codecs libsamplerate rnnoise rubberband
  cd ..

  lipo -create faad2-2.11.2/.build/libfaad.2.dylib arm64/faad2-2.11.2/.build/libfaad.2.dylib -output libfaad.2.dylib
  lipo -create mp4v2-2.1.3/.libs/libmp4v2.2.dylib arm64/mp4v2-2.1.3/.libs/libmp4v2.2.dylib -output libmp4v2.2.dylib
  lipo -create lame-3.100/libmp3lame/.libs/libmp3lame.0.dylib arm64/lame-3.100/libmp3lame/.libs/libmp3lame.0.dylib -output libmp3lame.0.dylib
  lipo -create mpg123-1.33.4/src/libmpg123/.libs/libmpg123.0.dylib arm64/mpg123-1.33.4/src/libmpg123/.libs/libmpg123.0.dylib -output libmpg123.0.dylib
  lipo -create libogg-1.3.6/src/.libs/libogg.0.dylib arm64/libogg-1.3.6/src/.libs/libogg.0.dylib -output libogg.0.dylib
  lipo -create opus-1.6.1/.libs/libopus.0.dylib arm64/opus-1.6.1/.libs/libopus.0.dylib -output libopus.0.dylib
  lipo -create speex-1.2.1/libspeex/.libs/libspeex.1.dylib arm64/speex-1.2.1/libspeex/.libs/libspeex.1.dylib -output libspeex.1.dylib
  lipo -create libvorbis-1.3.7/lib/.libs/libvorbis.0.dylib arm64/libvorbis-1.3.7/lib/.libs/libvorbis.0.dylib -output libvorbis.0.dylib
  lipo -create libvorbis-1.3.7/lib/.libs/libvorbisenc.2.dylib arm64/libvorbis-1.3.7/lib/.libs/libvorbisenc.2.dylib -output libvorbisenc.2.dylib
  lipo -create flac-1.5.0/src/libFLAC/.libs/libFLAC.14.dylib arm64/flac-1.5.0/src/libFLAC/.libs/libFLAC.14.dylib -output libFLAC.14.dylib
  lipo -create MAC_1260_SDK/.build/libMAC.15.dylib arm64/MAC_1260_SDK/.build/libMAC.15.dylib -output libMAC.15.dylib
  lipo -create libsndfile-1.2.2/src/.libs/libsndfile.1.dylib arm64/libsndfile-1.2.2/src/.libs/libsndfile.1.dylib -output libsndfile.1.dylib
  lipo -create libsamplerate-0.2.2/src/.libs/libsamplerate.0.dylib arm64/libsamplerate-0.2.2/src/.libs/libsamplerate.0.dylib -output libsamplerate.0.dylib
  lipo -create rnnoise-9acc1e5/.libs/librnnoise.0.dylib arm64/rnnoise-9acc1e5/.libs/librnnoise.0.dylib -output librnnoise.0.dylib
  lipo -create rubberband-4.0.0/.build/librubberband.so.3 arm64/rubberband-4.0.0/.build/librubberband.so.3 -output librubberband.3.dylib

  lipo -create musepack_src_r475/mpcdec/mpcdec arm64/musepack_src_r475/mpcdec/mpcdec -output mpcdec
  lipo -create musepack_src_r475/mpcenc/mpcenc arm64/musepack_src_r475/mpcenc/mpcenc -output mpcenc
  lipo -create wavpack-5.8.1/cli/wavpack arm64/wavpack-5.8.1/cli/wavpack -output wavpack
  lipo -create wavpack-5.8.1/cli/wvunpack arm64/wavpack-5.8.1/cli/wvunpack -output wvunpack
  lipo -create ffmpeg-8.1/ffmpeg arm64/ffmpeg-8.1/ffmpeg -output ffmpeg

  sudo cp lib*.dylib /usr/local/lib
  sudo cp ffmpeg mpc* w*pack /usr/local/bin

  unset BUILD_ARM64
fi

cd ..

# Prepare package
cd packaging/macosx

bash ./prepare.sh
bash ./package.sh "Robert Kausch" $NOTARIZATION_PASSWORD

MACOSX_TARGET=$MACOSX_DEPLOYMENT_TARGET
if [[ -z $MACOSX_TARGET ]]; then
  MACOSX_TARGET=`sw_vers | awk '$1 == "ProductVersion:" { print $2 }'`
fi

mv freac-*-macos${MACOSX_TARGET%%.*}.dmg ../../freac-continuous-macos${MACOSX_TARGET%%.*}.dmg

cd ../..

# Upload DMG to GitHub
bash .github/workflows/tools/upload-build freac*.dmg
