#!/bin/sh
# cPanel's ensure_deps, run as the account in the app's folder; pip gets a virtualenv because Debian and Ubuntu refuse user installs (PEP 668), and gems go in vendor/bundle
set -eu
case "$1" in
    npm)
        exec npm install --no-audit --no-fund
        ;;
    pip)
        [ -x .venv/bin/python ] || python3 -m venv --system-site-packages .venv
        .venv/bin/pip install --disable-pip-version-check -r requirements.txt
        .venv/bin/python -c 'import gunicorn' 2>/dev/null || .venv/bin/pip install --disable-pip-version-check gunicorn
        ;;
    gem)
        mkdir -p .bakkbone
        cp /opt/bakkbone/agent/share/bakkbone-Gemfile.rb .bakkbone/Gemfile
        # Starting from the app's own lock keeps its versions; Bundler only adds Puma when it isn't there
        if [ -f Gemfile.lock ]; then cp Gemfile.lock .bakkbone/Gemfile.lock; else rm -f .bakkbone/Gemfile.lock; fi
        export BUNDLE_GEMFILE="$PWD/.bakkbone/Gemfile" BUNDLE_PATH="$PWD/vendor/bundle"
        exec bundle install
        ;;
    *)
        echo "Unknown dependency type $1" >&2
        exit 2
        ;;
esac
