#!/bin/bash

PM_PACKMAN_VERSION=5.7.2

# Specify where packman command exists
export PM_INSTALL_PATH=$(dirname ${BASH_SOURCE})

add_packages_root_to_file()
{
	FILE_PATH=$1
	if [ -f "$FILE_PATH" ]; then
		if ! grep -Fq "PM_PACKAGES_ROOT" $FILE_PATH ; then
			echo "Adjusting $FILE_PATH"
			echo -e "export PM_PACKAGES_ROOT=\$HOME/packman-repo\n" >> $FILE_PATH
		fi
	fi
}

# The packages root may already be configured by the user
if [ -z "$PM_PACKAGES_ROOT" ]; then
	# Set variable permanently using .profile for this user (if exists)
	add_packages_root_to_file ~/.profile
	add_packages_root_to_file ~/.bashrc
	# Set variable temporarily in this process so that the following execution will work 
	export PM_PACKAGES_ROOT="${HOME}/packman-repo"
fi

# Ensure the packages root path exists:
if [ ! -d "$PM_PACKAGES_ROOT" ]; then
	echo "Creating packman packages repository at $PM_PACKAGES_ROOT"
	mkdir -p "$PM_PACKAGES_ROOT"
fi

# The packman module may be externally configured
if [ -z "$PM_MODULE_DIR_EXT" ]; then
	PM_MODULE_DIR="$PM_PACKAGES_ROOT/packman-common/$PM_PACKMAN_VERSION"
else
    PM_MODULE_DIR="$PM_MODULE_DIR_EXT"
fi
export PM_MODULE="$PM_MODULE_DIR/packman.py"

fetch_file_from_s3() 
{
	SOURCE=$1
	SOURCE_URL=http://packman-bootstrap.s3.amazonaws.com/$SOURCE
	TARGET=$2
	echo "Fetching $SOURCE from S3 ..."
	if command -v wget >/dev/null 2>&1; then
		wget --quiet -O$TARGET $SOURCE_URL
	else
		curl -o $TARGET $SOURCE_URL -s -S
	fi		
}

# Ensure the packman package exists:
if [ ! -f "$PM_MODULE" ]; then
	PM_MODULE_PACKAGE="packman-common@$PM_PACKMAN_VERSION.zip"
	TARGET="/tmp/$PM_MODULE_PACKAGE"
	# We always fetch packman from S3:
	fetch_file_from_s3 $PM_MODULE_PACKAGE $TARGET
	if [ "$?" -eq "0" ]; then
		echo "Unpacking ..."
		mkdir -p "$PM_MODULE_DIR"
		unzip -q $TARGET -d "$PM_MODULE_DIR"
		rm $TARGET
	else
		echo "Failure while fetching packman module from S3!"
		exit 1
	fi
fi

# For now assume python is installed on the box and we just need to find it
if command -v python2.7 >/dev/null 2>&1; then
	export PM_PYTHON=python2.7
elif command -v python2 >/dev/null 2>&1; then
    export PM_PYTHON=python2
else
	export PM_PYTHON=python
fi

# Ensure 7za package exists:
PM_7za_VERSION=16.02.4
export PM_7za_PATH="$PM_PACKAGES_ROOT/7za/$PM_7za_VERSION"
if [ ! -d "$PM_7za_PATH" ]; then
    export PM_7za_PATH="$PM_PACKAGES_ROOT/chk/7za/$PM_7za_VERSION"
    if [ ! -d "$PM_7za_PATH" ]; then
        $PM_PYTHON -s -u -E "$PM_MODULE" pull "$PM_MODULE_DIR/deps.packman.xml"
        if [ "$?" -ne 0 ]; then
           echo "Failure while installing required 7za package"
           exit 1
        fi
    fi
fi

# Generate temporary file name for environment variables:
PM_VAR_PATH=`mktemp -u -t tmp.$$.pmvars.XXXXXX`

$PM_PYTHON -s -u -E "$PM_MODULE" "$@" --var-path="$PM_VAR_PATH"
exit_code=$?
# Export the variables if the file was used and remove the file:
if [ -f "$PM_VAR_PATH" ]; then
	while read -r line
	do
        if [ ${#line} -gt 0 ]; then
    		export "$line"
        fi
	done < "$PM_VAR_PATH"
    rm -f "$PM_VAR_PATH"
fi

# Return the exit code from python
if [ "$exit_code" != 0 ]; then
    exit "$exit_code"
fi
