# Copyright (C) 2019 The Raphielscape Company LLC.
#
# Licensed under the Raphielscape Public License, Version 1.c (the "License");
# you may not use this file except in compliance with the License.
#
""" Userbot module for getting information about the userbot's version.
cmd is .ver"""

from asyncio import create_subprocess_shell as asyncrunapp
from asyncio.subprocess import PIPE as asyncPIPE
from shutil import which

from uniborg.util import admin_cmd


@borg.on(admin_cmd(pattern="ver(.*)"))
async def bot_ver(event):
    """For .ver command, get the bot version."""
    if which("git") is not None:
        invokever = "git describe --all --long"
        ver = await asyncrunapp(
            invokever,
            stdout=asyncPIPE,
            stderr=asyncPIPE,
        )
        stdout, stderr = await ver.communicate()
        verout = str(stdout.decode().strip()) + str(stderr.decode().strip())

        invokerev = "git rev-list --all --count"
        rev = await asyncrunapp(
            invokerev,
            stdout=asyncPIPE,
            stderr=asyncPIPE,
        )
        stdout, stderr = await rev.communicate()
        revout = str(stdout.decode().strip()) + str(stderr.decode().strip())

        await event.edit(
            "`Userbot Version: " f"{verout}" "` \n" "`Revision: " f"{revout}" "`"
        )
    else:
        await event.edit(
            "Shame that you don't have git, You're running 69.0 - 'Master' anyway"
        )
