#!/usr/bin/env bash

# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License.  You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific


# Because Ambari rpm unpacks modules here on all systems
PARAM="$@"
export PYTHONPATH=/usr/lib/python2.6/site-packages:$PYTHONPATH
SHELL_SCRIPT=/usr/lib/python2.6/site-packages/ambari_shell/ambari_shell.py
OK=1
NOTOK=0
if [ -a /usr/bin/python2.7 ] && [ -z "$PYTHON" ]; then
  PYTHON=/usr/bin/python2.7
fi

if [ -a /usr/bin/python2.6 ] && [ -z "$PYTHON" ]; then
  PYTHON=/usr/bin/python2.6
fi

if [ "x$PYTHON" == "x" ]; then
  PYTHON=/usr/bin/python
fi

export PYTHON=$PYTHON



# check for version
check_python_version ()
{
  echo "Verifying Python version compatibility..."
  majversion=`$PYTHON -V 2>&1 | awk '{print $2}' | cut -d'.' -f1`
  minversion=`$PYTHON -V 2>&1 | awk '{print $2}' | cut -d'.' -f2`
  numversion=$(( 10 * $majversion + $minversion))
  if (( $numversion < 26 )); then
    echo "ERROR: Found Python version $majversion.$minversion. Ambari Agent requires Python version > 2.6"
    return $NOTOK
  fi
  echo "Using python " $PYTHON
  return $OK
}

retcode=0
check_python_version
if [ "$?" -eq "$NOTOK" ]; then
	 exit -1
fi
$PYTHON $SHELL_SCRIPT $PARAM

exit $retcode