#!/usr/bin/env bash

set -euo pipefail

main() {
    # fpm does not preserve symlinks from /usr/local/node/bin/npm to
    # /usr/local/node/lib/node_modules/npm correctly, so we need to re-create
    # them on install.
    # This script runs from /var/lib/dpkg/info on running machines and we can't
    # pass any arguments to it so we need to hardcode the prefix to use.
    pushd /usr/local/node/bin
        rm -f npm npx
        if [[ ! -f ../lib/node_modules/npm/bin/npx-cli.js ]]; then
            echo "error creating npm symlink: /usr/local/node/bin/../lib/node_modules/npm/bin/npx-cli.js does not exist"
        fi
        if [[ ! -f ../lib/node_modules/npm/bin/npm-cli.js ]]; then
            echo "error creating npm symlink: /usr/local/node/bin/../lib/node_modules/npm/bin/npx-cli.js does not exist"
        fi
        ln -s ../lib/node_modules/npm/bin/npx-cli.js npx
        ln -s ../lib/node_modules/npm/bin/npm-cli.js npm
    popd
}

main "$@"
