#!/bin/sh


# Copyright (c) 2020, XGQT
# Licensed under the ISC License

#     .vir.
#   ,d$$$$$$b.
#   &&&&( )&&&b
#   Q$$$$$$$$$$B
#     "$$$$$$$P
#   ,d$$$$$$P"
#   $$$$$$P
#   `Q$$P"

# gfetch - tiny system info for gentoo

# based on:
# https://github.com/jschx/ufetch/


# INFO

host="$(hostname 2>/dev/null)"
cpu="$(uname -p 2>/dev/null)"
kernel="$(uname -sr 2>/dev/null)"
uptime="$(uptime -p 2>/dev/null | sed 's/up //')"
shell="$(basename "${SHELL}" 2>/dev/null)"

if [ -f /etc/lsb-release ]
then
    os="$(cut -d \" -f 2 < /etc/lsb-release) $(uname -m)"
elif [ -f /etc/os-release ]
then
    os="$(cut -d = -f 2 < /etc/os-release | sed 1q) $(uname -m)"
else
    os="Gentoo $(uname -m)"
fi

if [ -d "${EPREFIX}"/var/db/pkg ]
then
    packages="All: $(find "${EPREFIX}"/var/db/pkg/*/* -type d | wc -l)"
    real="Real: $(find "${EPREFIX}"/var/db/pkg/*/* -type d | grep -c -v -E 'acct-group|acct-user|app-eselect|java-virtuals|media-fonts|virtual')"
else
    packages="n/a"
    real=""
fi

if [ -f "${EPREFIX}"/var/lib/portage/world ]
then
    world="World: $(wc -l < "${EPREFIX}"/var/lib/portage/world)"
else
    world=""
fi

if [ -n "${DE}" ]
then
    ui="${DE}"
    uitype='DE'
elif [ -n "${WM}" ]
then
    ui="${WM}"
    uitype='WM'
elif [ -n "${XDG_CURRENT_DESKTOP}" ]
then
    ui="${XDG_CURRENT_DESKTOP}"
    uitype='DE'
elif [ -n "${DESKTOP_SESSION}" ]
then
    ui="${DESKTOP_SESSION}"
    uitype='DE'
elif [ -f "${HOME}/.xinitrc" ]
then
    ui="$(tail -n 1 "${HOME}/.xinitrc" | cut -d ' ' -f 2)"
    uitype='WM'
elif [ -f "${HOME}/.xsession" ]
then
    ui="$(tail -n 1 "${HOME}/.xsession" | cut -d ' ' -f 2)"
    uitype='WM'
else
    ui='unknown'
    uitype='UI'
fi


# Color Definitions

if [ -x "$(command -v tput)" ]; then
    bold="$(tput bold)"
    # black="$(tput setaf 0)"
    # red="$(tput setaf 1)"
    # green="$(tput setaf 2)"
    # yellow="$(tput setaf 3)"
    # blue="$(tput setaf 4)"
    magenta="$(tput setaf 5)"
    # cyan="$(tput setaf 6)"
    white="$(tput setaf 7)"
    reset="$(tput sgr0)"
fi


# Output color

# labels
lc="${reset}${bold}${magenta}"

# user and hostname
nc="${reset}${bold}${magenta}"

# info
ic="${reset}${bold}${white}"

# first color
c0="${reset}${bold}${magenta}"

# second color
c1="${reset}${magenta}"


# OUTPUT

cat <<EOF

${c0}     .vir.       ${nc}${USER}${ic}@${nc}${host}${reset}
${c0}   ,d\$\$\$\$${c1}\$\$b.    ${lc}CPU:     ${ic}${cpu}${reset}
${c0}   &&&&${c1}( )&&&b   ${lc}OS:      ${ic}${os}${reset}
${c0}   Q\$\$\$\$\$\$\$\$${c1}\$\$B  ${lc}KERNEL:  ${ic}${kernel}${reset}
${c0}     "\$\$\$\$\$${c1}\$\$P   ${lc}UP:      ${ic}${uptime}${reset}
${c0}   ,d\$\$\$\$${c1}\$\$P"    ${lc}PKGS:    ${ic}${packages}  ${world}  ${real}${reset}
${c0}   \$\$\$\$${c1}\$\$P       ${lc}SHELL:   ${ic}${shell}${reset}
${c0}   \`Q\$\$${c1}P"        ${lc}${uitype}:      ${ic}${ui}${reset}

EOF