#!/bin/bash

# Python launcher script for Linux

IFS="
"
PYTHON=""

for file in "${HOME}/.config/PRQA/installed_packages" \
  "${HOME}/.config/Perforce/installed_packages" \
  "/etc/prqa.d/installed_packages" \
  "/etc/Perforce.d/installed_packages"
do
  if [ -f "${file}" ]
  then
    for package in $(< "${file}" )
    do
      if [ -z "${PYTHON}" ]
      then
        # is it a framework or Helix package?
        if ! [[ "${package/qa-framework//}" == "${package}" ]] || \
           ! [[ "${package/Helix-QAC//}" == "${package}" ]] 
        then
          # assemble python path
          path="${package##*location\'}"
          path="${path#*\'}"
          path=$(find "${path%\'*}/components/python"* -name python )
          # if it is executable, we are done
          if [ -x "${path}" ]
          then
            PYTHON="${path}"
          fi
        fi
      fi
    done
  fi
done

# if no framework found, hope that there is a suitable python in the path
if [ -z "${PYTHON}" ]
then
  PYTHON=python
fi

# launch python script with same basename as the launcher script
"${PYTHON}" "${0%.sh}.py" $*

