#!/bin/bash
## Created by Juan-Pablo Caceres

# Parse command line options
clean=1
install=0
jobs=1
CONFIG="DEFINES+=JACKTRIP_BUILD_INFO=\\\"$(git describe --tags)-$(git rev-parse --short HEAD)\\\""
UNKNOWN_OPTIONS=()
BUILD_RTAUDIO=0
RTAUDIO=0
NO_SYSTEM_RTAUDIO=0
PRO_FILE="../jacktrip.pro"
HELP_STR="usage:\n
./build [noclean nojack rtaudio nogui novs vsftux static install [-config static]]\n\n
options:\n
noclean - do not run \"make clean\" first\n
nojack - build without jack\n
rtaudio - build with RtAudio\n
no-system-rtaudio - use bundled RtAudio library even if it's available in the system\n
nogui - build without the gui\n
novs - build without Virtual Studio support\n
vsftux - build with Virtual Studio first launch experience\n
noupdater - build without auto-update support\n
static - build with static libraries\n
weakjack - build with weak linking of jack libraries\n
install - install jacktrip in system location (uses sudo)\n
"
while [[ "$#" -gt 0 ]]; do
  if [[ ${#UNKNOWN_OPTIONS[@]} -eq 0 ]]; then
    case $1 in
      noclean) clean=0 ;;
      nojack)
      echo "Building without jack"
      CONFIG="-config nojack $CONFIG" 
      ;;
      rtaudio) 
      RTAUDIO=1
      ;;
      no-system-rtaudio) 
      NO_SYSTEM_RTAUDIO=1
      ;;
      nogui)
      echo "Building without the gui"
      CONFIG="-config nogui $CONFIG" 
      ;;
      novs)
      echo "Building without Virtual Studio support"
      CONFIG="-config novs $CONFIG" 
      ;;
      vsftux)
      echo "Building with Virtual Studio first launch experience"
      CONFIG="-config vsftux $CONFIG" 
      ;;
      noupdater)
      echo "Building without auto-update support"
      CONFIG="-config noupdater $CONFIG"
      ;;
      static)
      echo "Building with static libraries"
      CONFIG="-config static $CONFIG" 
      ;;
      weakjack)
      echo "Building with weak linking of jack"
      CONFIG="-config weakjack $CONFIG"
      ;;
      install)
      echo "Will install JackTrip in system location"
      install=1
      ;;
      -j*)
      jobs=$(echo $1 |sed 's,-j0*\([0-9]*\),\1,')
      if [[ $jobs -le 1 ]]; then
        jobs=1
      fi
      echo "Will build using $jobs make jobs"
      ;;
      -h|--help) 
      echo -e $HELP_STR; exit 
      ;;
      *) UNKNOWN_OPTIONS+=("$1") ;;
    esac
    shift
  else
    case $1 in
      *) UNKNOWN_OPTIONS+=("$1") ;;
    esac
    shift
  fi
done

echo "All build options:"
echo "$CONFIG"

# Check for Platform
platform='unknown'
unamestr=`uname`
if [[ "$unamestr" == 'Linux' ]]; then
  echo "Building on Linux"
  platform='linux'
elif [[ "$unamestr" == 'Darwin' ]]; then
  echo "Building on macOS"
  platform='macosx'
elif [[ "$unamestr" == 'MINGW'* ]]; then
  echo "Building on Windows (MinGW)"
  platform='mingw'
fi

# Set qmake command name
if [[ $platform == 'linux' ]]; then
  if hash qmake-qt5 2>/dev/null; then
    echo "Using qmake-qt5"
    QCMD=qmake-qt5
  elif hash qmake 2>/dev/null; then #in case qt was compiled by user
    echo "Using qmake"
    QCMD=qmake
  fi
  MCMD=make
elif [[ $platform == 'macosx' ]]; then
  QCMD=qmake
  # if qmake is not in path, try homebrew qt5
  echo "path to qmake"
  echo "$(which qmake)"
  if ! command -v $QCMD &> /dev/null; then
    # echo "qmake not found in \$PATH, searching for homebrew qt5"
    QT_PREFIX=`brew --prefix qt5`
    if [[ -n $QT_PREFIX ]]; then
      QCMD="$QT_PREFIX/bin/$QCMD"
      echo "Using qmake at $QCMD"
    else
      echo "Homebrew installation of qt5 not found"
      exit
    fi
  fi
  MCMD=make
elif [[ $platform == 'mingw' ]]; then
  QCMD=qmake
  MCMD=mingw32-make
elif [[ $platform == 'unknown' ]]; then
  echo "Unregonized platform, exiting"
  exit
fi

# detect spec
QSPEC=`$QCMD -query | grep QMAKE_SPEC`
QSPEC=${QSPEC##QMAKE_SPEC:}

# check spec for windows to update make command
if [[ $QSPEC == "win32-msvc" ]]; then
  MCMD=nmake
  JOM=$(which jom.exe)
  if [[ "x$JOM" != "x" ]]; then
    MCMD=$JOM
  fi
fi

# check for RtAudio
if [[ $RTAUDIO == 1 ]]; then
  pkg-config --exists rtaudio 2> /dev/null
  if [[ $? -eq 0 ]]; then
    if [[ $NO_SYSTEM_RTAUDIO == 1 ]]; then
      echo "RtAudio library found, but using the bundled library anyway"
      BUILD_RTAUDIO=1
    else
      echo "Using system-provided RtAudio library"
    fi
  else
    echo "Using bundled RtAudio library"
    BUILD_RTAUDIO=1
  fi
fi

if [[ $BUILD_RTAUDIO == 1 ]]; then
  PRO_FILE="../jacktrip_and_rtaudio.pro";
  CONFIG="-config bundled_rtaudio $CONFIG"
elif [[ $RTAUDIO == 1 ]]; then
  echo "Building with RtAudio"
  CONFIG="-config rtaudio $CONFIG"
fi

# Create our build directory
mkdir -p builddir
cd builddir

# exit if build commands fail
set -e

# Build
echo "qmake command:"
echo $QCMD -spec $QSPEC $CONFIG "${UNKNOWN_OPTIONS[@]}" $PRO_FILE
$QCMD -spec $QSPEC $CONFIG "${UNKNOWN_OPTIONS[@]}" $PRO_FILE
if [[ $clean == 1 ]]; then
  $MCMD clean
fi
if [[ "$MCMD" == "nmake" ]]; then
  $MCMD release
else
  $MCMD -j$jobs release
fi
if [[ "$install" == 1 ]]; then
  echo "*** Installing JackTrip ***"
  echo "We need elevated privileges to install JackTrip in the system location"
  sudo $MCMD release-install
fi
