#!/bin/sh

build_version="10.1"

# Run scripts with Python (and modpy.sh if needed) if possible, rather
# than the modXXX script (which only works correctly if Python 2.3 is installed)
modpy="../../bin/modpy.sh"
# If modpy.sh doesn't exist, assume we have Python set up correctly already
# (e.g. Mac or RPM install, or PYTHONPATH and LD_LIBRARY_PATH set):
if test "${PYTHON}x" != "x"; then
  pyints="${PYTHON}"
else
  pyints="python3 python2 python"
fi

for pyint in ${pyints}; do
  if test ! -f ${modpy}; then
    runpy="${pyint}"
  else
    runpy="${modpy} ${pyint}"
  fi
  # Only use Modeller through Python if 1) an import succeeds, 2) the 'modeller'
  # module imported is really Modeller (not a spurious modeller.py in the path
  # somewhere) and 3) the version matches that these examples were built with
  run_version=`${runpy} - 2>/dev/null <<END
import sys
from modeller import *
v = info.version_info
if isinstance(v, tuple):
    v = ".".join([str(i) for i in v])
sys.stdout.write(v + '\n')
END
`
  if test "${run_version}" = "${build_version}"; then
    break
  fi
done

run_script() {
  pyscript="$1.py"
  if test "${run_version}" = "${build_version}"; then
    log="$1.log"
    cmd="${runpy} ${pyscript} > ${log}"
  else
    cmd="mod10.1 ${pyscript}"
  fi
  echo "$cmd"
  eval "$cmd"
}

run_script search
run_script compare
run_script get-model
