#!/bin/sh

usage() {
    echo "Usage: python_package_analyze [options] <environment-name> <python-command-string>"
    echo "where options are:"
    echo -e " -h, --help\tShow this help screen"
    exit $1
}

# Parse command line arguments
if [ "$1" == "-h" ] || [ "$1" == "--help" ]; then
    usage 0
fi
if [ $# -ne 2 ]; then
    usage 1
fi
ENVIRONMENT_NAME=$1
PYTHON_COMMAND_STRING=$2

# Unpack the packed environment
if [ ! -f "${ENVIRONMENT_NAME}.tar.gz" ]; then
    echo "Environment tarball does not exist, exiting"
    exit 2
fi    
tar xzf ${ENVIRONMENT_NAME}.tar.gz
if [ $? -ne 0 ]; then
    echo "Unable to successfully unpack tarball, exiting"
    exit 3
fi

# Activate conda environment, run the task, deactivate
source bin/activate &> /dev/null
if [ $? -ne 0 ]; then
    echo "Unable to activate Conda environment, exiting"
    exit 4
fi
conda-unpack &> /dev/null
if [ $? -ne 0 ]; then
    echo "Unable to activate Conda environment, exiting"
    exit 4
fi
${PYTHON_COMMAND_STRING}
EXITVALUE=$?
source bin/deactivate &> /dev/null
exit $EXITVALUE 
