#! /bin/bash

# Check if CK_ROOT is defined and used it, otherwise use auto-detected path
if [ -z "$CK_ROOT" ]; then 
  CK_path1=`dirname $0`
  pushd $CK_path1/.. > /dev/null
  CK_path=`pwd`
  popd > /dev/null
  export CK_ROOT=$CK_path
fi

# Load kernel module (either GIT/local installation or as package)
if [ -f "$CK_ROOT/ck/kernel.py" ]; then 
   if [ "$CK_PYTHON" == "" ]; then
      CK_PYTHON=$PYTHON
      if [ "$CK_PYTHON" == "" ]; then
         CK_PYTHON=python
      fi
   fi

   $CK_PYTHON -W ignore::DeprecationWarning $CK_ROOT/ck/kernel.py "$@"

else
  if [ "$CK_PYTHON" == "" ]; then
     CK_PYTHON=python
     $CK_PYTHON -m ck.kernel test_install &> /dev/null

     if [ $? -ne 0 ]; then
        CK_PYTHON=python2
        $CK_PYTHON -m ck.kernel test_install &> /dev/null

        if [ $? -ne 0 ]; then
           CK_PYTHON=python3
           $CK_PYTHON -m ck.kernel test_install &> /dev/null
        fi
     fi
  fi

  if [ $? -ne 0 ]; then
     echo "Can't find CK kernel - you may need to set CK_ROOT environment variable manually!"
     exit 1
  fi

  $CK_PYTHON -W ignore::RuntimeWarning -m ck.kernel "$@"
fi
