# shellcheck shell=dash

___x_cmd_telegram_bot_service(){
    param:void

    [ $# -gt 0 ]    ||      set -- status

    local op="$1";          shift
    case "$op" in
        -h|--help)          ___x_cmd help -m telegram bot service "$@" ; return 0 ;;
        start|stop|restart|status|ensure|log|run)
                            ___x_cmd_telegram_bot_service_"$op" "$@"      ;;
        *)                  N=telegram M="Unknown op -> $op" log:ret:64   ;;
    esac
}

___x_cmd_telegram_bot_service_start(){
    ___x_cmd_telegram_ensureinit || return "$?"
    ___x_cmd service start      x-telegram-listener   --   x-cmd telegram bot service run "$@"
}

___x_cmd_telegram_bot_service_ensure(){
    ___x_cmd_telegram_ensureinit || return "$?"
    ___x_cmd service ensure      x-telegram-listener   --   x-cmd telegram bot service run "$@"
}

___x_cmd_telegram_bot_service_restart(){
    ___x_cmd service restart    x-telegram-listener
}

___x_cmd_telegram_bot_service_stop(){
    ___x_cmd service stop      x-telegram-listener
}

___x_cmd_telegram_bot_service_status(){
    ___x_cmd service status     x-telegram-listener
}

___x_cmd_telegram_bot_service_log(){
    local chatid="$1"
    [ -n "$chatid" ] || N=telegram M="Not Found chat id" log:ret:1

    local fp
    local x_=; ___x_cmd_telegram_bot_data_which_ "$chatid"
    fp="$x_"
    [ -n "$fp" ] || N=telegram M="Message data file not found for [chatid=$chatid]" log:ret:1

    local ___X_CMD_TAIL_MAIN_OPTS=

    while [ $# -gt 0 ]; do
        case "$1" in
            -n)     ___x_cmd cmdstr ___X_CMD_TAIL_MAIN_OPTS -n "$1";  arg:2:shift   ;;
            -f)     ___x_cmd cmdstr ___X_CMD_TAIL_MAIN_OPTS "$1";     shift         ;;
            *)      break       ;;
        esac
    done

    eval set -- "$___X_CMD_TAIL_MAIN_OPTS" "\"\$@\""

    ___x_cmd_cmds tail "$@" "$fp"
}


___x_cmd_telegram_bot_service_run(){
    ___x_cmd telegram listen --fp
}
