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

# Parse command line options
clean=1
install=0
CONFIG=
UNKNOWN_OPTIONS=
BUILD_RTAUDIO=0
RTAUDIO=0
NO_SYSTEM_RTAUDIO=0
PRO_FILE="../jacktrip.pro"
HELP_STR="usage:\n
./build [noclean nojack rtaudio nogui 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
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 [[ -z "$UNKNOWN_OPTIONS" ]]; 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" 
      ;;
      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
      ;;
      -h|--help) 
      echo -e $HELP_STR; exit 
      ;;
      *) UNKNOWN_OPTIONS="$UNKNOWN_OPTIONS $1" ;;
    esac
    shift
  else
    case $1 in
      *) UNKNOWN_OPTIONS="$UNKNOWN_OPTIONS $1" ;;
    esac
    shift
  fi
done

# 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
QSPEC=linux-g++
MCMD=make
elif [[ $platform == 'macosx' ]]; then
  QCMD=qmake
  QSPEC=macx-clang
  # if qmake is not in path, try homebrew qt5
  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
  QSPEC=win32-g++
  MCMD=mingw32-make
elif [[ $platform == 'unknown' ]]; then
  echo "Unregonized platform, exiting"
  exit
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

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