#!/usr/bin/env sh

help() {
    printf "%s\n" \
    "Installs local dependencies for lambdas in this directory's subdirectories" \
    "Usage: RUN_TYPE is one of 'env', 'dev', or 'lock'" \
    "    install_local_dependencies.sh -r RUN_TYPE"
}

# Run types:
#     - env: creates a fresh virtual environment
#     - dev: installs dependencies necessary to build and develop locally
#     - lock: freezes dependencies for packages declared in "requirements-to-freeze.txt"

# Examples:
#     First, navigate to the directory of a lambda
#         cd index_product_metadata
#     Set up the virtual env inside of the lambda
#         ../install_local_dependencies.sh -r env
#     Install tools to run, lint, and test the lambda
#         ../install_local_dependencies.sh -r dev
#     Update dependencies based on requirements-to-freeze.txt
#         ../install_local_dependencies.sh -r lock

# Identify the system as linux, mac_intel, or mac_arm
getSystemArch() {
    CURRENT_UNAME=$(uname -s)
    case "$CURRENT_UNAME" in
        Linux*)     MACHINE_TYPE=linux;;
        Darwin*)    MACHINE_TYPE=mac;;
        *)          MACHINE_TYPE="UNKNOWN:${CURRENT_UNAME}"
    esac
    if [ "$MACHINE_TYPE" != "mac" ]; then
        echo "$MACHINE_TYPE"
        return
    fi
    case "$(uname -p)" in
        i386*) PROCESSOR_TYPE=mac_intel;;
        arm*) PROCESSOR_TYPE=mac_arm;;
        *) PROCESSOR_TYPE=mac_unknown;;
    esac
    echo ${PROCESSOR_TYPE}
}

# Identify whether there are kafka dependencies in the lambda
requiresKafka() {
    KAFKA_DEPS=$(cat requirements* | grep -i kafka)
    if [ -n "$KAFKA_DEPS" ]; then
        echo 'true'
    fi
}

# Install requirements, handling issues specific to each architecture
installRequirements() {
    REQUIREMENTS_FILE=$1
    MACHINE=$(getSystemArch)
    printf "Running on %s\n" "$MACHINE"
    REQUIRES_KAFKA=$(requiresKafka)
    if [ "$MACHINE" != "mac_arm" ] || [ -z "$REQUIRES_KAFKA" ]; then
        env/bin/python -m pip install -q -r "$REQUIREMENTS_FILE"
        return
    fi
    # ensure brew exists
    if ! command -v brew > /dev/null; then
        printf "Please install 'brew' and use it to install the package 'librdkafka'\n"
        exit 1
    fi
    (brew list librdkafka > /dev/null) || (brew install librdkafka > /dev/null)
    CFLAGS="-I/opt/homebrew/include -L/opt/homebrew/lib" env/bin/pip install -q -r "$REQUIREMENTS_FILE"
}

RUN_TYPE=

# Parse cli args
while getopts "r:h" flag
do
    case "${flag}" in
        r)
            if [ "$OPTARG" = 'env' ] || [ "$OPTARG" = 'dev' ] || [ "$OPTARG" = 'lock' ]; then
                RUN_TYPE=$OPTARG
            else
                printf "ERROR: Unknown run type '%s'\n" "$OPTARG"
                help && exit 1
            fi
            ;;
        h) help && exit;;
        \?) help && exit 1;;
    esac
done

if [ -z "$RUN_TYPE" ]; then
    help
    exit 1
fi

printf "Running '%s' routine in %s\n" "$RUN_TYPE" "$(pwd)"

# install base env
if [ "$RUN_TYPE" = "env" ]; then
    if [ -d env ]; then
        printf "ERROR: env already exists\n"
        exit 1
    fi
    python -m venv env
    env/bin/python -m pip install -q -U pip setuptools wheel
    printf "virtual env created in 'env/'\n"
fi

# install dev requirements
if [ "$RUN_TYPE" = "dev" ]; then
    if [ -f env/bin/py.test ]; then
        printf "skipping install, dev dependencies already installed\n"
        exit
    fi
    installRequirements requirements-test.txt
    printf "development dependencies installed\n"
fi

# update requirements
if [ "$RUN_TYPE" = "lock" ]; then
    installRequirements requirements-to-freeze.txt
    echo "-i https://pypi.theorchard.io/pypi/" > requirements.txt
    env/bin/python -m pip freeze >> requirements.txt
    printf "dependencies locked in 'requirements.txt'\n"
fi
