#!/bin/sh

# 1. Load any PATHs that were configured by the customer
PYTHONEXEC=""
SCLPYTHONVER=""
SCLPYTHONEXEC="python3"

[ -e "/etc/corelight-client-python3-env" ] && . "/etc/corelight-client-python3-env"

if [ "$PYTHONEXEC" != "" ]; then
   exec "$PYTHONEXEC" /usr/share/corelight-fleet/corelight-client/bin/corelight-client "$@"
fi

# 2. Find python
CHKPYTHON_EXEC="python3 python36 python3.6 python37 python3.7 python35 python3.5 python34 python34 python32 python3.2 python38 python3.8 python39 python3.9 python"
for i in ${CHKPYTHON_EXEC}; do
   NEWEXEC="`which $i 2>/dev/null`"
   if [ "$NEWEXEC" != "" ] && [ -x "$NEWEXEC" ]; then
      PYTHONEXEC="$NEWEXEC"
      break
   fi
done

# 3. See if we are done (found python3) and can execute
if [ -x "$PYTHONEXEC" ]; then
   PYTHON_VERSION="`"${PYTHONEXEC}" --version 2>&1 |sed -n "s/^Python\ \([0-9]\+\)\..*/\1/p"`"

   if [ "$PYTHON_VERSION" -ge "3" ]; then
      exec "$PYTHONEXEC" "$@"
   fi
fi

# 4. on redhat and centos (which will contain /etc/redhat-release too) we can try software collections like we suggested
#    this will setup the local environment on the system to allow us to use python3

# e.g. scl enable rh-python35 -- /usr/share/corelight-fleet/corelight-client/bin/corelight-client "$@"
if [ -e "/etc/redhat-release" ]; then
   SCL_TOOL="`which scl 2>/dev/null`"
   if [ "$SCL_TOOL" != "" ] && [ -x "$SCL_TOOL" ]; then
      if [ "$SCLPYTHONVER" = "" ]; then
         PYTHON_SCLVERS="`scl --list |sed -n 's/^rh-\(python[3-9][0-9]*\)$/\1/p'`"
         if [ "`echo "$PYTHON_SCLVERS" |wc -w`" = "1" ]; then
            SCLPYTHONVER="rh-$PYTHON_SCLVERS"
         else
            ALL_SYSTEM_PACKAGES="`rpm -q --all`"
            for i in $PYTHON_SCLVERS; do
               # python35-python-requests
               if echo "$ALL_SYSTEM_PACKAGES" |grep -q "${i}-python-requests"; then
                  SCLPYTHONVER="rh-${i}"
                  break
               fi
            done
         fi      
      fi

      if [ "$SCLPYTHONVER" != "" ]; then
         exec "$SCL_TOOL" enable "$SCLPYTHONVER" -- "$SCLPYTHONEXEC" "$@"
      fi
   fi
fi

# Nothing worked. Tell the user what to do to fix this.
echo "" >&2
echo "Please ensure that python3 is installed on your system" >&2
echo "" >&2
echo "On Ubuntu 18.04 and higher, you can install 'python3' from your os repository" >&2
echo "   1. sudo apt install python3" >&2
echo "" >&2
echo "On CentOS 7, you can get this from the software collections:" >&2
echo "   1. sudo yum install centos-release-scl yum-utils" >&2
echo "   2. sudo yum-config-manager --enable centos-sclo-rh-testing" >&2
echo "   3. sudo yum install rh-python35 sclo-python35-python-requests" >&2
echo "" >&2
echo "On RedHat EL 7, you can get this from the software collections:" >&2
echo "   1. sudo yum install yum-utils" >&2
echo "   2. sudo yum-config-manager --enable rhel-server-rhscl-7-rpms" >&2
echo "   3. sudo yum-config-manager --enable rhel-server-rhscl-beta-7-rpms" >&2
echo "   4. sudo yum install rh-python35 sclo-python35-python-requests" >&2
echo "" >&2
exit 1

