#!/usr/bin/env bash
#
# Copyright (c) 2024 YunoHost Contributors
#
# This file is part of YunoHost (see https://yunohost.org)
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#

readonly N_PREFIX="/opt/node_n"
export N_PREFIX

# [internal]
_ynh_load_nodejs_in_path_and_other_tweaks() {

    # Get the absolute path of this version of node
    nodejs_dir="$N_PREFIX/n/versions/node/$nodejs_version/bin"

    if [ ! -e "$nodejs_dir" ]; then
        echo "Skipping loading nodejs, because it doesn't seem to be provisioned yet. This is likely to happen during restore and other specific contexts."
        return
    fi

    # Load the path of this version of node in $PATH
    if [[ :$PATH: != *":$nodejs_dir"* ]]; then
        PATH="$nodejs_dir:$PATH"
    fi

    # Export PATH such that it's available through sudo -E / ynh_exec_as $app
    export PATH

    # This is in full lowercase such that it gets replaced in templates
    # shellcheck disable=SC2034
    path_with_nodejs="$PATH"
    # shellcheck disable=SC2034
    PATH_with_nodejs="$PATH"

    # Prevent yet another Node and Corepack madness, with Corepack wanting the user to confirm download of Yarn
    export COREPACK_ENABLE_DOWNLOAD_PROMPT=0
    export NPM_CONFIG_UPDATE_NOTIFIER=false
}

# Auto-load Nodejs path tweaks if this app uses the nodejs resource in the manifest
if [ -n "${nodejs_version:-}" ] && (cat "$YNH_APP_BASEDIR/manifest.toml" | toml_to_json | jq -e ".resources.nodejs" > /dev/null); then
    _ynh_load_nodejs_in_path_and_other_tweaks
fi

# Install a specific version of nodejs, using 'n'
#
# The installed version is defined by `$nodejs_version` which should be defined as global prior to calling this helper
#
# usage: ynh_nodejs_install
#
# `n` (Node version management) uses the `PATH` variable to store the path of the version of node it is going to use.
# That's how it changes the version
#
# The helper adds the appropriate, specific version of nodejs to the `$PATH` variable (which
# is preserved when calling ynh_exec_as_app). Also defines:
#
# - `$path_with_nodejs` to be used in the systemd config (`Environment="PATH=__PATH_WITH_NODEJS__"`)
# - `$nodejs_dir`, the directory containing the specific version of nodejs, which may be used in the systemd config too (e.g. `ExecStart=__NODEJS_DIR__/node foo bar`)
ynh_nodejs_install() {
    # Use n, https://github.com/tj/n to manage the nodejs versions

    [[ -n "${nodejs_version:-}" ]] || ynh_die "\$nodejs_version should be defined prior to calling ynh_nodejs_install"

    # Create $N_PREFIX
    mkdir --parents "$N_PREFIX"

    # Install the requested version of nodejs
    if [[ $YNH_ARCH == "arm64" ]]; then
        "$YNH_HELPERS_DIR/vendor/n/n" install "$nodejs_version" --arch=arm64
    else
        "$YNH_HELPERS_DIR/vendor/n/n" install "$nodejs_version"
    fi

    # Find the last "real" version for this major version of node.
    final_nodejs_version=$(find "$N_PREFIX/n/versions/node/$nodejs_version"* -maxdepth 0 | sort --version-sort | tail --lines=1)
    final_nodejs_version=$(basename "$final_nodejs_version")

    # Store nodejs_version into the config of this app
    nodejs_version="$final_nodejs_version"
    ynh_app_setting_set --key=nodejs_version --value="$final_nodejs_version"

    _ynh_load_nodejs_in_path_and_other_tweaks
}

# Remove the version of node used by the app.
#
# usage: ynh_nodejs_remove
#
# This helper will check if another app uses the same version of node.
#
# - If not, this version of node will be removed.
# - If no other app uses node, n will be also removed.
ynh_nodejs_remove() {

    [[ -n "${nodejs_version:-}" ]] || ynh_die "\$nodejs_version should be defined prior to calling ynh_nodejs_remove"

    ynh_app_setting_delete --key=nodejs_version

    # Garbage-collect unused versions
    local installed_versions="$(N_PREFIX=/opt/node_n "$YNH_HELPERS_DIR/vendor/n/n" ls | awk -F/ '{print $2}')"
    for version in $installed_versions; do
        if ! grep -qE "^nodejs_version: '?$version'?" /etc/yunohost/apps/*/settings.yml; then
            "$YNH_HELPERS_DIR/vendor/n/n" rm "$version"
        fi
    done

    # If no other app uses n, remove n
    if ! grep -q "^nodejs_version:" /etc/yunohost/apps/*/settings.yml; then
        ynh_safe_rm "$N_PREFIX"
        sed --in-place "/N_PREFIX/d" /root/.bashrc
    fi
}
