#!/bin/bash
#
# This file is part of MorphoGraphX - https://www.MorphoGraphX.org  (@RichardSmithLab)
#
# MorphoGraphX development is led by the Richard S. Smith lab at the John Innes Centre, Norwich, UK
#
# If you use MorphoGraphX in your work, please cite:
#   https://doi.org/10.7554/eLife.72601
#
# For support please see the image.sc forum:
#   https://forum.image.sc/tag/MorphoGraphX
#
# MorphoGraphX is copyright by its authors, contributors, and/or their employers.
#
# MorphoGraphX is free software, and is licensed under the terms of the 
# GNU General Public License https://www.gnu.org/licenses/.
#
# Set the paths in case we are having a local install
MY_DIR=$(dirname $(readlink -f $0))
LIB_DIR=$(readlink -f ${MY_DIR}/../lib)
BIN_DIR=$(readlink -f ${MY_DIR}/../bin)

LD_LIBRARY_PATH=${LIB_DIR}/MorphoGraphX:${LIB_DIR}:${LD_LIBRARY_PATH}
PATH=${PATH}:${BIN_DIR}
export PATH
export LD_LIBRARY_PATH
export SCIFIO_PATH=/usr/lib/jars # reguired for XPIWIT

# Process information commands and exit, does not require terminal window
if [ $# -gt 0 ]; then 
  if [ $1 = "--help" ]; then 
    MorphoGraphX --help
    exit 0
  elif [ $1 = "--dir" ]; then 
    MorphoGraphX --dir
    exit 0
  elif [ $1 = "--process" ]; then 
    MorphoGraphX --process
    exit 0
  elif [ $1 = "--user-process" ]; then 
    MorphoGraphX --user-process
    exit 0
  elif [ $1 = "--all-process" ]; then 
    MorphoGraphX --all-process
    exit 0
  elif [ $1 = "--include" ]; then 
    MorphoGraphX --include
    exit 0
  elif [ $1 = "--lib" ]; then 
    MorphoGraphX --lib
    exit 0
  elif [ $1 = "--version" ]; then 
    MorphoGraphX --version
    exit 0
  elif [ $1 = "--resource" ]; then 
    MorphoGraphX --resource
    exit 0
  fi
fi

# Hack to pick up correct style on Mint
#export QT_STYLE_OVERRIDE=gtk2

# Give the option for the user to specify whatever OPTIRUN is
test_nvidia_context() {
  if [ $($1 glxinfo 2>/dev/null | grep -c -e 'direct rendering: Yes' -e 'OpenGL vendor string.*NVIDIA') -eq 2 ]; then
    echo $1
    return 0
  fi
  # This probably only works for virtualgl with xrdp
  if [ "$XRDP_SESSION" == 1 ]; then
    if [ $($1 -d /dev/dri/card0 glxinfo 2>/dev/null | grep -c -e 'direct rendering: Yes' -e 'OpenGL vendor string.*NVIDIA') -eq 2 ]; then
      echo $1 -d /dev/dri/card0
      return 0
    fi
    if [ $($1 -d /dev/dri/card1 glxinfo 2>/dev/null | grep -c -e 'direct rendering: Yes' -e 'OpenGL vendor string.*NVIDIA') -eq 2 ]; then
      echo $1 -d /dev/dri/card1
      return 0
    fi
  fi
  return 1
}

test_context() {
  if [ $($1 glxinfo 2>/dev/null | grep -c -e 'direct rendering: Yes' -e 'OpenGL vendor string') -ge 2 ]; then
    echo $1
    return 0
  fi
  return 1
}

test_nvidia_command() {
  CMD=$1
  if REAL_CMD=$(which $CMD); then
    test_nvidia_context $REAL_CMD
    return 0
  fi
  return 1
}

test_command() {
  CMD=$1
  if REAL_CMD=$(which $CMD); then
    test_context $REAL_CMD
    return 0
  fi
  return 1
}

test_commands() {
  # Testing if the current context is simply sufficient
  if test_nvidia_context; then
    echo
    return 0
  fi
  # Try to find utilities to find an nvidia context
  for cmd in vglrun primusrun optirun; do
    if OPTIRUN=$(test_nvidia_command $cmd); then
      echo $OPTIRUN
      return 0
    fi
  done
  # Test for non nvidia context, in case
  if test_context; then
    echo
    return 0
  fi
  echo
  return 1
}

# For RDP check if groups set properly and vglrun installed
if [ "$XRDP_SESSION" == 1 ]; then
  if [ $(groups | grep -c -e 'video') -ne 1 ]; then
    echo "For RDP to work you need to be in the groups: video render"
  fi
  if [ $(groups | grep -c -e 'render') -ne 1 ]; then
    echo "For RDP to work you need to be in the groups: video render"
  fi
  if [ $(which vglrun | grep -c -e 'vglrun') -lt 1 ]; then
    echo "For RDP to work you need to have vglrun (virtualgl) installed"
  fi
fi

if [ -z "$OPTIRUN" ]; then
  if ! OPTIRUN=$(test_commands); then
    echo "Warning, cannot find a good OpenGL display. Trying default display."
  fi
fi

mgxargs=
for i; do mgxargs="$mgxargs '$i'"; done
echo "MorphoGraphX command line: \"${OPTIRUN} MorphoGraphX $mgxargs\""
x-terminal-emulator -e "bash -ic \"${OPTIRUN} MorphoGraphX $mgxargs; sleep 30\""
