#!/bin/sh
# btfetch 0.18 -- LostGeek.NET == Mar 13, 2025
# bens tiny fetch
#
# BASED ON: ufetch-debian - tiny system info for debian

# shellcheck disable=SC2034,SC2250,SC2312

# Setup
host="$(hostname)"
#os="Debian $(cat /etc/debian_version)"
os=$(grep -o '^PRETTY_NAME=.*' /etc/os-release 2>/dev/null | cut -d= -f2 | tr -d '"')
[ -z "$os" ] && os=$(uname -o)
kernel="$(uname -sr)"
# shellcheck disable=SC2116
rawcpus="$(echo "$(nproc) x $(
  model=$(grep "model name" /proc/cpuinfo | head -n1 | cut -d ":" -f2 | sed 's/^[ \t]*//')
  if [ -n "$model" ]; then echo "$model"; else echo "$(lscpu | grep "Vendor ID" | cut -d ":" -f2 | sed 's/^[ \t]*//') $(lscpu | grep "Model name" | cut -d ":" -f2 | sed 's/^[ \t]*//')"; fi
)")"
uptime_output=$(uptime) # Capture the full uptime output once

# Package count, for the following dists
if command -v dpkg >/dev/null 2>&1; then
  packages=$(dpkg -l | grep -c '^i') # Debian/Ubuntu
elif command -v pacman >/dev/null 2>&1; then
  packages=$(pacman -Qq | wc -l) # Arch Linux
elif command -v apk >/dev/null 2>&1; then
  packages=$(apk list --installed 2>/dev/null | wc -l) # Alpine Linux
elif command -v rpm >/dev/null 2>&1; then
  packages=$(rpm -qa | wc -l) # Fedora/RHEL
elif command -v pkg >/dev/null 2>&1; then
  packages=$(pkg info | wc -l) # FreeBSD
else
  packages="Unknown"
fi

# Find disk size, and used
diskused=$(df -h / | awk '{print $3}' | grep -v Used)
disktotal=$(df -h / | awk '{print $2}' | grep -v Size)

# Extract the days from the uptime output
if echo "$uptime_output" | grep -q 'day'; then
  # There are days in the output; extract # of days
  daysup=$(echo "$uptime_output" | awk -F'up ' '{print $2}' | awk '{print $1}' | sed 's/[^0-9]*//g')
else
  daysup=0
fi
users=$(echo "$uptime_output" | awk -F'users,' '{print $1}' | awk '{print $NF}' | sed 's/[^0-9]*//g')
loadavg=$(echo "$uptime_output" | awk -F'load average: ' '{print $2}' | sed 's/,/ /g')
freeth=$(free -h | grep Mem)
usedmem=$(echo "$freeth" | awk '{print $3}')
totalmem=$(echo "$freeth" | awk '{print $2}')
# For CPUINFO we remove (R) (TM), extra spaces, etc... otherwise it is LONG
precpus=$(echo "$rawcpus" | sed -E 's/\(R\)|\(TM\)|CPU//g' \
  | sed 's/[[:space:]]+/ /g' \
  | sed 's/^ //;s/ $//' \
  | sed 's/ @.*//' \
  | sed 's/  / /g' \
  | sed 's/   /  /g')
# Get CPU Speed, make human-readable
cpu_freq=$(awk '{if ($1 < 1000000) printf "%.0f MHz", $1/1000; else printf "%.2f GHz", $1/1000000}' /sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq)
cpus="$precpus ($cpu_freq)"

## Define Colors -- Everything below needs these
if [ -x "$(command -v tput)" ]; then
  bold="$(tput bold 2>/dev/null)"
  black="$(tput setaf 0 2>/dev/null)"
  red="$(tput setaf 1 2>/dev/null)"
  green="$(tput setaf 2 2>/dev/null)"
  yellow="$(tput setaf 3 2>/dev/null)"
  blue="$(tput setaf 4 2>/dev/null)"
  magenta="$(tput setaf 5 2>/dev/null)"
  cyan="$(tput setaf 6 2>/dev/null)"
  white="$(tput setaf 7 2>/dev/null)"
  reset="$(tput sgr0 2>/dev/null)"
fi

# How the colors are used in the fetch
lc="${reset}${bold}${red}" # labels
nc="${reset}${bold}${red}" # user and hostname
ic="${reset}"              # info

# Extract the USER@HOST ps1 color code from .bashrc
# Check if .bashrc exists before extracting color code
if [ -f ~/.bashrc ]; then
  promptcolor=$(grep -oP '\\\[\\033\[[0-9;]+m\\\]' ~/.bashrc | head -n1 | sed 's/\\\[//g; s/\\\]//g' || grep -o '\033\[[0-9;]*m' ~/.bashrc | head -n1)
fi

# Map the escape codes to color names
if [ "$promptcolor" = "\033[01;32m" ]; then
  color_name="green"
elif [ "$promptcolor" = "\033[01;31m" ]; then
  color_name="red"
elif [ "$promptcolor" = "\033[01;33m" ]; then
  color_name="yellow"
elif [ "$promptcolor" = "\033[01;34m" ]; then
  color_name="blue"
elif [ "$promptcolor" = "\033[01;35m" ]; then
  color_name="magenta"
elif [ "$promptcolor" = "\033[01;36m" ]; then
  color_name="cyan"
elif [ "$promptcolor" = "\033[01;30m" ]; then
  color_name="black"
else
  color_name="unknown"
fi

# I'd be surprised if there isnt a much more elegant way of doing this
if [ "$color_name" = "green" ]; then
  c0="${reset}${green}"
elif [ "$color_name" = "red" ]; then
  c0="${reset}${red}"
elif [ "$color_name" = "yellow" ]; then
  c0="${reset}${yellow}"
elif [ "$color_name" = "blue" ]; then
  c0="${reset}${blue}"
elif [ "$color_name" = "magenta" ]; then
  c0="${reset}${magenta}"
elif [ "$color_name" = "cyan" ]; then
  c0="${reset}${cyan}"
elif [ "$color_name" = "black" ]; then
  c0="${reset}${black}"
else
  c0="${reset}" # Just reset if unknown. Logo will be white / colorless
fi

# Output -- # Font is 'Whimsy' #  -- patorjk.com #

cat <<EOF
${c0}   d8b                ${nc}${USER}${ic}@${nc}${host}${ic}  up for ${daysup} days${reset}${reset}
${c0}   ?88         d88    ${lc}OS:        ${ic}${os}${reset}
${c0}    88b     d888888P  ${lc}KERNEL:    ${ic}${kernel}${reset}
${c0}    888888b   ?88'    ${lc}PACKAGES:  ${ic}${packages}${reset}${lc}  DISK: ${ic}${diskused} of ${disktotal} Total${reset}
${c0}    88P \`?8b  88P     ${lc}MEMORY:    ${ic}${usedmem} Used of ${totalmem} Total${reset}
${c0}   d88,  d88  88b     ${lc}CPUs:      ${ic}${cpus}${reset}
${c0}  d88'\`?88P'  \`?8b    ${lc}LOAD:      ${ic}${loadavg}${reset}
EOF
