#! /usr/bin/env bash
#- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# the script (build_macOS_dmg.sh.in) to be configured

# unhash to enable tracing
# set -x

# accomodate for fileicon being in different places
FILEICON="$(which fileicon)"

if  [ "${FILEICON}".  == "". ] ; then
  if    test -f "/usr/local/bin/fileicon" ; then
    FILEICON="/usr/local/bin/fileicon"
  elif  test -f "/opt/homebrew/bin/fileicon" ; then
    FILEICON="/opt/homebrew/bin/fileicon"
  else
    echo "unable to attach icons to files/directories"
  fi
fi

# the data passed from CMAKE
cpack_package_name="@CPACK_PACKAGE_NAME@"

cpack_package_file_name="@CPACK_PACKAGE_FILE_NAME@"

orx_major="@ORX_MAJOR@"

cmake_binary_dir="@CMAKE_BINARY_DIR@"

cmake_install_prefix="@CMAKE_INSTALL_PREFIX@"

cmake_source_dir="@CMAKE_SOURCE_DIR@"

# the rest of the script using the preset variables

# Rebuild everything from scratch everytime, erase build dir or write errors will occur
rm -dfr $cmake_binary_dir/temp_dmg_dir
mkdir $cmake_binary_dir/temp_dmg_dir

cp -R $cmake_install_prefix $cmake_binary_dir/temp_dmg_dir/$cpack_package_name$orx_major

cp $cmake_source_dir/platform/unix/macos/install/Readme_ooRexx.txt $cmake_binary_dir/temp_dmg_dir/Readme_ooRexx.txt

cp $cmake_source_dir/platform/unix/macos/install/Readme_ooRexx.txt $cmake_binary_dir/temp_dmg_dir/$cpack_package_name$orx_major/Readme_ooRexx.txt

cp $cmake_source_dir/platform/unix/macos/install/Readme_ooRexx_USB.txt $cmake_binary_dir/temp_dmg_dir/Readme_ooRexx_USB.txt

cp $cmake_source_dir/platform/unix/macos/install/Readme_ooRexx_USB.txt $cmake_binary_dir/temp_dmg_dir/$cpack_package_name$orx_major/Readme_ooRexx_USB.txt

cp $cmake_source_dir/CPLv1.0.txt $cmake_binary_dir/temp_dmg_dir/CPLv1.0.txt

cp $cmake_source_dir/CPLv1.0.txt $cmake_binary_dir/temp_dmg_dir/$cpack_package_name$orx_major/CPLv1.0.txt

ln -sfvh /Applications $cmake_binary_dir/temp_dmg_dir

# fileicon is open source, install with brew install fileicon if missing
if  [ "${FILEICON}".  != "". ] ; then
  ${FILEICON} set $cmake_binary_dir/temp_dmg_dir/$cpack_package_name$orx_major $cmake_source_dir/platform/unix/macos/install/oorexx.png
fi

# Documentation is taken care of by CMake

# Create the dmg locally, will look something like ooRexx-5.0.0-12160.macOS.x86_64.dmg
rm -f $cmake_binary_dir/$cpack_package_name$orx_major.dmg

hdiutil create -fs HFS+ -volname $cpack_package_name$orx_major -srcfolder $cmake_binary_dir/temp_dmg_dir $cmake_binary_dir/$cpack_package_name$orx_major.dmg

if  [ "${FILEICON}".  != "". ] ; then
  ${FILEICON} set $cmake_binary_dir/$cpack_package_name$orx_major.dmg $cmake_source_dir/platform/unix/macos/install/oorexx.png
fi

cp $cmake_binary_dir/$cpack_package_name$orx_major.dmg $cmake_binary_dir/$cpack_package_file_name.dmg

rm -f $cmake_binary_dir/$cpack_package_name$orx_major.dmg
