# shellcheck shell=dash
___x_cmd log init luarocks
xrc:mod:lib luarocks     __xmirror  __xproxy  src  lsremote  open  install

# luarocks bin path added into the current PATH

# If we install, we will set this: luarocks config local_by_default true

___x_cmd_luarocks___main(){
    # Special for lua installation
    local ___X_CMD_LUAROCKS_PREFIX=""
    local ___X_CMD_LUAROCKS_LUAPATH=""

    # -- luarocks default behavior: Run 'luarocks' without any arguments to see the configuration.
    [ $# -gt 0 ]    ||      set - lsremote

    # TODO: How to better handle --lua-dir,--tree and other options
    # e.g: `x luarocks --tree=<path> install <rock>` and `x luarocks install <rock> --tree=<path>`
    local op="$1";  shift
    case "$op" in
        admin)              ___x_cmd_luarocks_admin                 "$@" ;;

        # src)                ___x_cmd_luarocks_src               "$@"     ;;
        open)               ___x_cmd_luarocks_open                  "$@" ;;
        # mirror|--xmirror)   ___x_cmd_luarocks___xmirror             "$@" ;;
        proxy|--xproxy)     ___x_cmd_luarocks___xproxy              "$@" ;;

        lsremote)           ___x_cmd_luarocks_lsremote              "$@" ;;

        i|install)          ___x_cmd_luarocks_install               "$@" ;;



        -h|--help)          ___x_cmd help -m luarocks;                 return 1 ;;
        *)                  ___x_cmd_luarocks_exec      "$op"       "$@" ;;
    esac
}

___x_cmd_luarocks___init(){
    ___X_CMD_LUAROCKS_MANIFEST_LIST_FP="$___X_CMD_PKG_ROOT_PATH/metadata/$___X_CMD_PKG_VERSION/luarocks/manifest.list"
}
___x_cmd_luarocks___init


___x_cmd_luarocks_exec(){
    if ! ___x_cmd_hascmd luarocks; then
        ___x_cmd env try luarocks || return 1
    fi

    ___x_cmd_luarocks___xproxy run ___x_cmd_cmds luarocks        "$@"
}

___x_cmd_luarocks_admin(){
    if ! ___x_cmd_hascmd luarocks-admin; then
        ___x_cmd env try luarocks || return 1
    fi

    ___x_cmd_luarocks___xproxy run ___x_cmd_cmds luarocks-admin  "$@"
}


