#!/bin/sh
#
# Copyright (C) 2022, 2023  Etersoft
# Copyright (C) 2022, 2023  Vitaly Lipatov <lav@etersoft.ru>
#
# 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/>.
#

epm_tool_help()
{
    message "Tools embedded in epm:"
    get_help HELPCMD $SHAREDIR/epm-tool

    message '
  Examples:
    epm tool eget -U http://ya.ru
    epm tool estrlist union a b a c
    epm tool erc archive.zip
'
}

epm_tool()
{
    local WHAT="$1"
    shift

    case "$WHAT" in
        "")
            fatal "Use epm tool --help to get help."
            ;;
        "-h"|"--help"|"help")
            epm_tool_help
            ;;
        "eget")                      # HELPCMD: downloading tool (simular to wget or curl)
            showcmd eget "$@"
            eget "$@"
            ;;
        "erc")                       # HELPCMD: universal archive manager
            showcmd erc "$@"
            erc "$@"
            ;;
        "ercat")                     # HELPCMD: universal file uncompressor
            showcmd ercat "$@"
            ercat "$@"
            ;;
        "estrlist")                  # HELPCMD: string operations
            showcmd estrlist "$@"
            estrlist "$@"
            ;;
        "json")                      # HELPCMD: json operations
            if [ "$1" = "--help" ] || [ "$1" = "-h" ] ; then
                echo "epm tool json - JSON parsing tool"
                echo
                echo "Usage:"
                echo "  epm tool json [options]              - parse JSON from stdin"
                echo "  epm tool json --get-json-value URL field  - get field value from JSON URL"
                echo
                echo "Options:"
                echo "  -b       Brief output (leaf only + prune empty)"
                echo "  -l       Leaf only"
                echo "  -p       Prune empty"
                echo
                echo "Examples:"
                echo "  echo '{\"version\":\"1.0\"}' | epm tool json -b"
                echo "  epm tool json --get-json-value https://api.example.com/info.json version"
                return
            fi
            # epm tool json --get-json-value URL field
            if [ "$1" = "--get-json-value" ] ; then
                local url="$2"
                local field="$3"
                [ -n "$url" ] || fatal "Use: epm tool json --get-json-value URL field"
                [ -n "$field" ] || fatal "Use: epm tool json --get-json-value URL field"
                get_json_value "$url" "$field"
                return
            fi
            showcmd json "$@"
            $CMDSHELL $SHAREDIR/tools_json "$@"
            ;;
        "yaml")                      # HELPCMD: parse yaml operations
            showcmd yaml "$@"
            $CMDSHELL $SHAREDIR/tools_yaml "$@"
            ;;
        "which")                    # HELPCMD: which like command (no output to stderr, can works without which package)
            print_command_path "$@"
            ;;
        *)
            fatal 'Unknown command $ epm tool $WHAT. Use epm print help for get help.'
            ;;
    esac
}
