# shellcheck shell=dash


# ___x_cmd_pkg_sphere_bootrc add --sphere X java 18.0.2-sem
___x_cmd_pkg_sphere_bootrc(){
    local op="$1";
    case "$op" in
        ls|load|unload|rm|add)
            shift; ___x_cmd_pkg_sphere_bootrc___"$op" "$@" ;;
        *)  N=pkg M="sphere bootrc no such option '$op'" log:ret:1 ;;
    esac
}

___x_cmd_pkg_sphere_bootrc___ls(){
    pkg:sphere:parse:option
    sphere_name="${sphere_name:-X}"
    local file="$___X_CMD_PKG_ROOT_SPHERE/$sphere_name/.x-cmd/link/X"
    [ -f "$file" ] || return 0
    < "$file" ___x_cmd_cmds_awk -v FS="=" '($0 != ""){ print $1 "=" $2; }'
}

___x_cmd_pkg_sphere_bootrc___rm(){
    pkg:sphere:parse:option
    sphere_name="${sphere_name:-X}"
    local file="$___X_CMD_PKG_ROOT_SPHERE/$sphere_name/.x-cmd/link/X"
    local name="$1";    [ -n "$name" ] || N=pkg M="Provide a package name" log:ret:1
    pkg:info "Remove bootrc of $name $version in [sphere=$sphere_name]"
    ___x_cmd fslock run "pkg_sphere_bootrc_${sphere_name}" ___x_cmd_pkg_sphere_bootrc___rm_inner "$file"
}

___x_cmd_pkg_sphere_bootrc___rm_inner(){
    local file="$1"
    [ -f "$file" ] || return 0
    local x_=;  ___x_cmd rat_ "$file"
    printf "%s\n" "$x_" | ___x_cmd_cmds_awk -v FS="=" -v name="$name" '
        (name == $1){ next; }
        ($0 != ""){ print $0; }
    ' > "${file}"
}

___x_cmd_pkg_sphere_bootrc___add(){
    local sphere_name=; local osarch=; local sociality=
    while [ "$#" -gt 0 ]; do
        case "$1" in
            --sphere)
                    sphere_name="$2";
                    [ -n "$sphere_name" ] || M="Provide a sphere name" N=pkg log:ret:1
                    shift 2
                    ;;
            --sphereroot)
                    local ___X_CMD_PKG_ROOT_SPHERE="$2"
                    [ -n "$___X_CMD_PKG_ROOT_SPHERE" ] || M="Provide sphere root path" N=pkg log:ret:1
                    shift 2
                    ;;
            --osarch)
                    osarch="$2";    shift 2 ;;
            --sociality)
                    sociality="$2"; shift 2 ;;
            *)      break ;;
        esac
    done
    [ -n "$osarch" ] || { ___x_cmd_pkg_osarch_ || return 1; osarch="$___X_CMD_PKG_OSARCH"; } || N=pkg M="Not found osarch" log:ret:1
    sphere_name="${sphere_name:-"X"}"
    pkg:sphere:name-version
    [ -n "$sociality" ] || {
        local x_="" >/dev/null 2>&1; ___x_cmd_pkg_sphere_sociality___get_       \
            --sphere "$sphere_name" --sphereroot "$___X_CMD_PKG_ROOT_SPHERE"    \
            --osarch "$osarch" "$name" "$version" || return $?
        sociality="$x_"
    }

    pkg:info "Recording bootrc of $name $version in [sphere=$sphere_name]"
    local file="$___X_CMD_PKG_ROOT_SPHERE/$sphere_name/.x-cmd/link/X"
    ___x_cmd fslock run "pkg_sphere_bootrc_${sphere_name}" ___x_cmd_pkg_sphere_bootrc___add_inner "$file"
}

___x_cmd_pkg_sphere_bootrc___add_inner(){
    local file="$1"
    local boot_file;    boot_file="$(___x_cmd_pkg___get_script_path "$name" "$version" "$osarch" boot)" || return $?
    local unboot_file;  unboot_file="$(___x_cmd_pkg___get_script_path "$name" "$version" "$osarch" unboot)" || return $?
    [ -n "${boot_file}${unboot_file}" ] || {
        pkg:debug "Not found boot/unboot file of '$osarch $name $version'"
        return 0
    }

    local x_treename=; ___x_cmd_pkg_treename_ "$name" "$version" "$osarch" || return $?

    if [ ! -f "$file" ]; then
        ___x_cmd ensurefp "$file"
        printf "%s\n" "${name}=${version}=${x_treename}=${sociality}" > "$file"
    else
        local x_=; ___x_cmd rat_ "$file"
        printf "%s\n" "$x_" | ___x_cmd_cmds_awk -v FS="=" \
            -v name="$name"             \
            -v version="$version"       \
            -v treename="$x_treename"   \
            -v sociality="$sociality"   '
            (name == $1){ next; }
            ($0 != ""){ print $0; }
            END{ print name "=" version "=" treename "=" sociality ; }
        ' > "${file}"
    fi

}

___x_cmd_pkg_sphere_bootrc___cache_(){
    pkg:sphere:parse:option
    sphere_name="${sphere_name:-X}"
    local file="$___X_CMD_PKG_ROOT_SPHERE/$sphere_name/.x-cmd/link/X"
    local cache_file="$___X_CMD_ROOT_TMP/pkg/bootrc$file.cache"
    [ -f "$file" ] || {
        pkg:debug "Not found bootrc $file"
        [ ! -f "$cache_file" ] || ___x_cmd rmrf "$cache_file"
        return 0
    }
    [ "$cache_file" -nt "$file" ] || {
        pkg:debug "Update bootrc cache file => $cache_file"
        ___x_cmd touch "${cache_file}.tmp"
        local l=
        while ___x_cmd_readr l; do
            [ -n "$l" ] || continue
            name="${l%%=*}"
            l="${l#"${name}="}"
            version="${l%%=*}"
            l="${l#"${version}="}"
            treename="${l%%=*}"
            l="${l#"${treename}="}"
            sociality="${l%%=*}"
            boot_file="$(___x_cmd_pkg___get_script_path "$name" "$version" "$osarch" boot)" || return $?
            unboot_file="$(___x_cmd_pkg___get_script_path "$name" "$version" "$osarch" unboot)" || return $?
            [ -n "${boot_file}${unboot_file}" ] || continue
            printf "%s\001%s\001%s\001%s\001%s\001%s\n" "$name" "$version" "$treename" "$sociality" "$boot_file" "$unboot_file"
        done  < "$file" > "$cache_file.tmp" || return $?
        ___x_cmd rmrf "$cache_file"
        ___x_cmd mv "${cache_file}.tmp" "$cache_file"
    }
    x_="$cache_file"
}

___x_cmd_pkg_sphere_bootrc___parse_cache_(){
    local l="$1"
    unboot_file="${l##*"${___X_CMD_UNSEENCHAR_001}"}"
    l="${l%"${___X_CMD_UNSEENCHAR_001}${unboot_file}"}"
    boot_file="${l##*"${___X_CMD_UNSEENCHAR_001}"}"
    [ -n "${boot_file}${unboot_file}" ] || return $?
    l="${l%"${___X_CMD_UNSEENCHAR_001}${boot_file}"}"
    sociality="${l##*"${___X_CMD_UNSEENCHAR_001}"}"
    l="${l%"${___X_CMD_UNSEENCHAR_001}${sociality}"}"
    treename="${l##*"${___X_CMD_UNSEENCHAR_001}"}"
    l="${l%"${___X_CMD_UNSEENCHAR_001}${treename}"}"
    version="${l##*"${___X_CMD_UNSEENCHAR_001}"}"
    name="${l%"${___X_CMD_UNSEENCHAR_001}${version}"}"
}

___x_cmd_pkg_sphere_bootrc___load(){
    pkg:sphere:parse:option
    sphere_name="${sphere_name:-X}"
    local l=; local name=; local version=; local treename=; local sociality=; local boot_file=; local unboot_file=
    local x_=; ___x_cmd_pkg_sphere_bootrc___cache_ --sphere "$sphere_name" --sphereroot "$___X_CMD_PKG_ROOT_SPHERE" || return $?
    [ -f "$x_" ] || return 0

    local ___X_CMD_PKG___META_OS="${osarch%%/*}"
    local ___X_CMD_PKG___META_ARCH="${osarch#*/}"
    local ___X_CMD_PKG___META_NAME=; local ___X_CMD_PKG___META_VERSION=; local ___X_CMD_PKG___META_TGT=
    while ___x_cmd_readr l; do
        [ -n "$l" ] || continue
        ___x_cmd_pkg_sphere_bootrc___parse_cache_ "$l" || continue

        pkg:debug --name "$name" --version "$version" --treename "$treename" \
            --sociality "$sociality" --boot_file "$boot_file" --unboot_file "$unboot_file" "bootrc load"

        [ "$sociality" = intrusive ] || [ "$sociality" = dev ] || {
            pkg:debug "The sociality[$sociality] of $name $version will not load the boot script file"
            continue
        }

        [ -f "$boot_file" ] || {
            pkg:debug "Not found $name $version boot script file"
            continue
        }

        ___X_CMD_PKG___META_NAME="$name"
        ___X_CMD_PKG___META_VERSION="$version"
        ___X_CMD_PKG___META_TGT="$___X_CMD_PKG_ROOT_SPHERE/$sphere_name/$treename/$name/$version"
        pkg:debug "Loading $name $version boot script file => $boot_file"
        . "$boot_file" || N=pkg M="Source boot script file failed" log:ret:1
    done < "$x_"
}

___x_cmd_pkg_sphere_bootrc___unload(){
    pkg:sphere:parse:option
    sphere_name="${sphere_name:-X}"
    local l=; local name=; local version=; local treename=; local sociality=; local boot_file=; local unboot_file=
    local x_=; ___x_cmd_pkg_sphere_bootrc___cache_ --sphere "$sphere_name" --sphereroot "$___X_CMD_PKG_ROOT_SPHERE" || return $?
    [ -f "$x_" ] || return 0

    local ___X_CMD_PKG___META_OS="${osarch%%/*}"
    local ___X_CMD_PKG___META_ARCH="${osarch#*/}"
    local ___X_CMD_PKG___META_NAME=; local ___X_CMD_PKG___META_VERSION=; local ___X_CMD_PKG___META_TGT=
    while ___x_cmd_readr l; do
        [ -n "$l" ] || continue
        ___x_cmd_pkg_sphere_bootrc___parse_cache_ "$l" || continue

        pkg:debug --name "$name" --version "$version" --treename "$treename" \
            --sociality "$sociality" --boot_file "$boot_file" "bootrc unload"

        [ "$sociality" = intrusive ] || [ "$sociality" = dev ] || {
            pkg:debug "The sociality[$sociality] of $name $version will not unload the boot script file"
            continue
        }

        [ -f "$unboot_file" ] || {
            pkg:debug "Not found $name $version unboot script file"
            continue
        }

        ___X_CMD_PKG___META_NAME="$name"
        ___X_CMD_PKG___META_VERSION="$version"
        ___X_CMD_PKG___META_TGT="$___X_CMD_PKG_ROOT_SPHERE/$sphere_name/$treename/$name/$version"
        pkg:debug "Loading $name $version unboot script file => $unboot_file"
        . "$unboot_file" || N=pkg M="Source unboot script file failed" log:ret:1
    done < "$x_"
}

