#!/bin/bash
#
# This file is part of MorphoGraphX - http://www.MorphoGraphX.org
# Copyright (C) 2012-2016 Richard S. Smith and collaborators.
#
# If you use MorphoGraphX in your work, please cite:
#   http://dx.doi.org/10.7554/eLife.05864
#
# MorphoGraphX is free software, and is licensed under under the terms of the 
# GNU General (GPL) Public License version 2.0, http://www.gnu.org/licenses.
#
# Shell script to start MorphoGraphX with a shell window underneath
# For nVidia optimus cards on laptops, MorphoGraphX needs to called with optirun
#

# 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

# 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
  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 primusrun optirun vglrun; 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
}

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\""
