# -*-eselect-*-  vim: ft=eselect
# Copyright 2019-2020 Orson Teodoro
# Copyright 2005-2012 Gentoo Foundation
# Distributed under the terms of the GNU GPL version 2 or later

DESCRIPTION="Manages the nodejs include symlink"
MAINTAINER="orsonteodoro@hotmail.com"
VERSION="20250106"
PN="nodejs"

# find a list of Node.js symlink targets
find_targets() {
    local f
    for f in /usr/include/node[0-9]*; do
        [[ -d ${f} ]] && echo $(basename "${f}")
    done
}

# remove the Node.js symlinks
remove_symlink() {
    rm "/usr/include/node" 2>/dev/null 1>/dev/null || true
}

# set the Node.js symlinks
set_symlink() {
    local target=$1

    if is_number "${target}"; then
        local targets=( $(find_targets) )
        target=${targets[target-1]}
        ln -s "/usr/include/${target}" "/usr/include/node"

	corepack disable 2>/dev/null || true
	mkdir -p "/usr/$(get_libdir)/node_modules"
	rm "/usr/$(get_libdir)/node_modules/corepack"
	rm "/usr/bin/corepack"
	ln -sf "/usr/$(get_libdir)/corepack/${target}" "/usr/$(get_libdir)/node_modules/corepack"
	ln -sf "/usr/$(get_libdir)/node_modules/corepack/dist/corepack.js" "/usr/bin/corepack"
	corepack enable
    elif [[ -d "/usr/include/${target}" && "${target}" =~ ^node[0-9]* ]] ; then
        ln -s "/usr/include/${target}" "/usr/include/node"

	corepack disable 2>/dev/null || true
	mkdir -p "/usr/$(get_libdir)/node_modules"
	rm "/usr/$(get_libdir)/node_modules/corepack"
	rm "/usr/bin/corepack"
	ln -sf "/usr/$(get_libdir)/corepack/${target}" "/usr/$(get_libdir)/node_modules/corepack"
	ln -sf "/usr/$(get_libdir)/node_modules/corepack/dist/corepack.js" "/usr/bin/corepack"
	corepack enable
    else
        echo "Failed to set symlink and is now gone."
        return
    fi

}

### show action ###

describe_show() {
    echo "Show the current Node.js symlink"
}

get_libdir() {
	realpath /usr/lib*/corepack | cut -f 3 -d "/"
}

do_show() {
    write_list_start "Current Node.js symlinks:"
    if [[ -L /usr/include/node ]]; then
        local k_node_src="/usr/include/node"
        local k_node_dest=$(canonicalise "/usr/include/node")
        write_kv_list_entry "${k_node_src%/} ->" "${k_node_dest%/}"

	local k_corepack_src="/usr/bin/corepack"
	local k_corepack_dest=$(canonicalise "/usr/bin/corepack")
        write_kv_list_entry "${k_corepack_src%/} ->" "${k_corepack_dest%/}"
    else
        write_kv_list_entry "(unset)" ""
    fi
}

### list action ###

describe_list() {
    echo "List available Node.js symlink targets"
}

do_list() {
    local i targets=( $(find_targets) )

    write_list_start "Available Node.js symlink targets:"
    for (( i = 0; i < ${#targets[@]}; i++ )); do
        # highlight the target where the symlink is pointing to
        [[ ${targets[i]} = \
            $(basename "$(canonicalise "/usr/include/node")") ]] \
            && targets[i]=$(highlight_marker "${targets[i]}")
    done
    write_numbered_list -m "(none found)" "${targets[@]}"
}

### set action ###

describe_set() {
    echo "Set a new Node.js symlink target"
}

describe_set_parameters() {
    echo "<target>"
}

describe_set_options() {
    echo "target : Target name or number (from 'list' action)"
}

do_set() {
    [[ -z $1 ]] && die -q "You didn't tell me what to set the symlink to"
    [[ $# -gt 1 ]] && die -q "Too many parameters"

    if [[ -L /usr/include/node ]]; then
        # existing symlink
        remove_symlink || die -q "Couldn't remove existing symlink"
        set_symlink "$1" || die -q "Couldn't set a new symlink"
    elif [[ -e /usr/include/node ]]; then
        # we have something strange
        die -q "/usr/include/node exists but is not a symlink"
    else
        set_symlink "$1" || die -q "Couldn't set a new symlink"
    fi
}
