# shellcheck shell=dash

___x_cmd_last___exec(){
    [ $# -gt 0  ]   ||  set -- --app

    local fmt=app;
    local filtermode=""

    while [ $# -gt 0 ]; do
        case "$1" in
            -j|--json)      fmt=json        ;;
            -c|--csv)       fmt=csv         ;;
            --auto|--app)   fmt="${1#--}"   ;;
            -r|--reboot)    filtermode=reboot ;;
            *)              break           ;;
        esac
        shift
    done

    "___x_cmd_last___exec___${fmt}" "$@"
}

___x_cmd_last___exec___auto(){
    if      ___x_cmd_is_stdout2tty; then        ___x_cmd_last___exec___app "$@"
    else                                        ___x_cmd_last___exec___csv "$@"
    fi
}

___x_cmd_last___exec___csv(){
    ___x_cmd_last___exec_raw "$@" | ___x_cmd_cmds awk -f "$___X_CMD_ROOT_MOD/last/lib/awk/parse.awk"
}

___x_cmd_last___exec___json(){
    ___x_cmd_last___exec___csv "$@" | ___x_cmd csv tojson
}

___x_cmd_last___exec_raw(){
    case "$filtermode" in
        reboot)     ___x_cmd_last___exec___raw "$@" | ___x_cmd_cmds grep -i "reboot" ;;
        *)          ___x_cmd_last___exec___raw "$@" ;;
    esac
}

___x_cmd_last___exec___raw(){
    ___x_cmd_hascmd last || N=last M="last command not found"  log:ret:1

    LANG="$___X_CMD_LOCALE_DEF_C" ___x_cmd_cmds last "$@"
}

___x_cmd_last___exec___app(){
    ___x_cmd_last___exec___csv "$@" | ___x_cmd csv app --clear
}

