# shellcheck shell=dash disable=SC2016

xrc:mod:lib     id      get

___x_cmd_id___main(){
    [ "$#" -gt 0 ]      ||  set -- cat

    local op="$1";      shift
    case "$op" in
        -h|--help)      ___x_cmd help -m id;  return ;;
        --exec)         ___x_cmd_id___exec          "$@" ;;
        -*)             ___x_cmd_id___exec  "$op"   "$@" ;;

        cat)            ___x_cmd_id___cat           "$@" ;;

        uid|uid_|gid|gid_|ugid|ugid_)
                        ___x_cmd_id_"$op"           "$@" ;;

        *)              ___x_cmd_id___cat   "$op"   "$@" ;;
    esac
}

___x_cmd_id___exec(){
    ___x_cmd_cmds id "$@"
}

___x_cmd_id___cat(){
    local IFS=" "
    local NO_COLOR="$NO_COLOR"
    { ___x_cmd_is_stdout2tty; } || NO_COLOR=1
    ___x_cmd_cmds id "$@" | ___x_cmd_cmds awk -v NO_COLOR="$NO_COLOR" '
BEGIN{
    if(NO_COLOR != 1){
        UI_KEY= "\033[36m"
        UI_NUM_VAR= "\033[35m"
        UI_STR_VAR= "\033[32m"
        UI_END= "\033[0m"
    }
}

function split_eq( str, _arr ){
    split( str, _arr, "=" )
    a = _arr[1]
    b = _arr[2]
}

function split_bracket( str, _arr ){
    split( str, _arr, "(" )
    a = _arr[1]
    b = _arr[2]
    b = substr(b, 1, length(b)-1)
}

{
    split_eq( $1 )
    split_bracket( b )
    printf( UI_KEY "uid:    " UI_END UI_NUM_VAR "%s" UI_END UI_STR_VAR "\t%s" UI_END "\n", a , b )

    split_eq( $2 )
    split_bracket( b )
    printf( UI_KEY "gid:    " UI_END UI_NUM_VAR "%s" UI_END UI_STR_VAR "\t%s" UI_END "\n", a , b )

    split_eq( $3 )
    l = split( b, arr, ",")
    printf(UI_KEY "groups:\n" UI_END)
    for (i=1; i<=l; ++i) {
        split_bracket( arr[i] )
        printf( "  -     "  UI_NUM_VAR "%s" UI_END UI_STR_VAR "\t%s" UI_END "\n",  a , b )
    }
}
'

}
