#!/bin/bash -ex
#
# this script is run on travis for the script stage of mac builds
#
 
function version_ge() { test "$(printf "%s\n%s" $1 $2 | sort -rV | head -n 1)" == "$1"; }

QMAKE=${QMAKE:-qmake}
LUPDATE=${LUPDATE:-lupdate}
LRELEASE=${LRELEASE:-lrelease}
MACDEPLOYQT=${MACDEPLOYQT:-macdeployqt}
# we assume we are on macOS, so date is not gnu date.
VERSIONID=${VERSIONID:-$(date -ju -f %Y-%m-%dT%H:%M:%S%z $(git show -s --format="%aI" HEAD | sed 's/:\(..\)$/\1/') +%Y%m%dT%H%MZ)-$(git rev-parse --short=7 HEAD)}

# debug tokens
"$(cd "$(dirname "${BASH_SOURCE[0]}" )" && pwd)"/ci_tokens

# build and test the CLI
if version_ge $($QMAKE -query QT_VERSION) 6.0.0; then
  $QMAKE GPSBabel.pro QMAKE_APPLE_DEVICE_ARCHS="x86_64 arm64"
else
  $QMAKE GPSBabel.pro
fi
make -j 3
make check

# build the GUI
pushd gui
if version_ge $($QMAKE -query QT_VERSION) 6.0.0; then
  $QMAKE app.pro QMAKE_APPLE_DEVICE_ARCHS="x86_64 arm64"
else
  $QMAKE app.pro
fi
make -j 3
make package
popd

# what is in there?
hdiutil attach -noverify gui/GPSBabelFE.dmg
find /Volumes/GPSBabelFE -ls
hdiutil detach /Volumes/GPSBabelFE

mv gui/GPSBabelFE.dmg gui/GPSBabel-${VERSIONID}.dmg

