#!/usr/bin/env bash


# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
BLUE='\033[0;34m'
PLAIN='\033[0m'

getHardwareInfo() {
get_opsy() {
    [ -f /etc/redhat-release ] && awk '{print ($1,$3~/^[0-9]/?$3:$4)}' /etc/redhat-release && return
    [ -f /etc/os-release ] && awk -F'[= "]' '/PRETTY_NAME/{print $3,$4,$5}' /etc/os-release && return
    [ -f /etc/lsb-release ] && awk -F'[="]+' '/DESCRIPTION/{print $2}' /etc/lsb-release && return
}


dashLine() {
    printf "%-70s\n" "-" | sed 's/\s/-/g'
}

machineID=$(cat /etc/machine-id)
cname=$( awk -F: '/model name/ {name=$2} END {print name}' /proc/cpuinfo | sed 's/^[ \t]*//;s/[ \t]*$//' )
cores=$( awk -F: '/model name/ {core++} END {print core}' /proc/cpuinfo )

tram=$( free -g | awk '/Mem/ {print $2}' )

opsy=$( get_opsy )
arch=$( uname -m )
lbit=$( getconf LONG_BIT )
kern=$( uname -r )

dashLine
echo -e "CPU\t$cname x $cpu_number"
echo -e "Cores\t$cores"
if [ "$HYPERTHREADING" -eq 1 ]; then
echo -e ${text_color}Hyperthread${remove_color} enabled
else
echo -e ${text_color}Hyperthread${remove_color} disabled
fi

echo -e "Memory\t$tram GB, $ramInfo x $ram_number"
# echo -e ${text_color}RAM Info${remove_color} $ramInfo x $ram_number

echo -e "OS\t$opsy"
echo -e "Arch\t$arch ($lbit Bit)"
echo -e "Kernel\t$kern"
echo -e ${text_color}GPU${remove_color} $gpu

# echo -e "ID\t${machineID}"
dashLine
echo ""
echo ""
echo -e "Disk Info:"
dashLine

if ! command -v lsblk &> /dev/null
then
    if [[ -f /etc/redhat-release ]]; then
        echo "lsblk not found. Installing on RHEL..."
        yum install util-linux -y
    elif [[ -f /etc/lsb-release ]]; then
        echo "lsblk not found. Installing on Ubuntu..."
        apt-get update
        apt-get install util-linux -y
    else
        echo "Unknown distribution. Cannot install lsblk."
        echo "Please install lsblk manually and try again."
        exit 1
    fi
fi

for disk in `lsblk | grep ^sd | awk '{print $1}'`; do echo -e "DiskID:\t`lsblk --nodeps -no serial /dev/$disk`"; done | sort | uniq
for disk in `lsblk | grep ^nvme | awk '{print $1}'`; do echo -e "DiskID:\t`lsblk --nodeps -no serial /dev/$disk`"; done | sort | uniq
dashLine
echo ""
echo ""
echo -e "Block Info:"
dashLine
lsblk -f
dashLine
}

source /etc/os-release
fetch="no"

# parse options
while getopts :n flag
do
  case "${flag}" in
    n) fetch="nofetch" && text_color="\033[1;37m";;
  esac
done

# ascii art
if [ $fetch == "yes" ]; then
  case $NAME in
    "Arch Linux")
      echo -e "$(<fetches/arch)"
      text_color='\033[1;34m'
      ;;
    "EndeavourOS")
      echo -e "$(<fetches/eos)"
      text_color='\033[1;35m'
      ;;
    "Gentoo")
      echo -e "$(<fetches/gentoo)"
      text_color='\033[1;35m'
      ;;
    "Debian GNU/Linux")
      echo -e "$(<fetches/debian)"
      text_color='\033[1;31m'  
      ;;
    "Ubuntu")
      echo -e "$(<fetches/ubuntu)"
      text_color='\033[1;33m'
      ;;
    "Manjaro Linux")
      echo -e "$(<fetches/manjaro)"
      text_color='\033[1;32m'
      ;;
    "Alpine Linux")
      echo -e "$(<fetches/alpine)"
      text_color='\033[1;34m'
      ;;
  esac
fi

# check for package managers in here and exit if none
if [[ -x "$(command -v apk)" ]]; then
  ((packages = packages + $(apk list --installed | wc -l)))
elif [[ -x "$(command -v apt)" ]]; then
  ((packages = packages + $(apt list --installed | wc -l)))
elif [[ -x "$(command -v dnf)" ]]; then
  ((packages = packages + $(dnf list --installed | grep -c -v '^Installed Packages$')))
elif [[ -x "$(command -v pacman)" ]]; then
  ((packages = packages + $(pacman -Q | wc -l)))
elif [[ -x "$(command -v zypper)" ]]; then
  ((packages = packages + $(zypper search -i | wc -l)))
elif [[ -x "$(command -v emerge)" ]]; then
  ((packages = packages + $(q qlist -I | wc -l)))
elif [[ -x "$(command -v nix-store)" ]]; then
  ((packages = packages + $(nix-store -q --references /var/run/current-system/sw | cut -d'-' -f2- | wc -l)))
elif [[ -x "$(command -v xbps-query)" ]]; then
  ((packages = packages + $(xbps-query -l | awk '{ print $2 }' | xargs -n1 xbps-uhelper getpkgname | wc -l)))
else
  echo "Unknown"
  exit 1
fi

# Setting variables
user_hostname="$(whoami)@$(cat /etc/hostname)"
user_shell=$(echo $SHELL | cut -c 6-)
disk_usage=$(df -h | awk '$NF=="/"{printf "%d/%dGB (%s)\n", $3,$2,$5}')
ram_usage=$(free -h | awk 'NR==2{printf "%s/%s\n", $3,$2}')
session_type=$XDG_SESSION_TYPE
#text_color='\033[1;34m' #if you want to use a fixed color use this
remove_color='\033[0m'

# get the host hardware
read -r version 2>/dev/null </sys/devices/virtual/dmi/id/product_version
read -r name 2>/dev/null </sys/devices/virtual/dmi/id/product_name
read -r mobovendor 2>/dev/null </sys/devices/virtual/dmi/id/board_vendor
read -r mobo 2>/dev/null </sys/devices/virtual/dmi/id/board_name
host="$mobovendor $name $version $mobo"
host=${host//To be filled by O.E.M./}
host=${host//To Be Filled*/}
host=${host//OEM*/}
host=${host//Not Applicable/}
host=${host//System Product Name/}
host=${host//System Version/}
host=${host//Undefined/}
host=${host//Default string/}
host=${host//Not Specified/}
host=${host//Type1ProductConfigId/}
host=${host//INVALID/}
host=${host//All Series/}

# get gpu
if [[ $(lspci) ]]; then
  gpu=$(lspci | grep "VGA\|3D" | cut -d ":" -f3)
else
  gpu="Unknown"
fi

#get cpu
cpu_model=$(cat /proc/cpuinfo | grep 'model name' | uniq | cut -c 14-)
if [ "$cpu_model" == "" ]; then
  cpu_model="Unknown"
fi

# cpu number
cpu_number=`cat /proc/cpuinfo | grep 'physical id' | sort | uniq | wc -l`

#if there's no session, should be a tty
if [[ "${XDG_SESSION_TYPE}" == "" ]] ; then #When the session cannot be detected.
  session_type="tty"
fi

# get RAM info 
ramInfo=`dmidecode | grep -P -A5 "Memory\s+Device" | grep Size  | grep -v Range | sort -u | cut -d : -f 2`
ramType_number=`dmidecode | grep -P -A5 "Memory\s+Device" | grep Size  | grep -v Range  | sort | uniq | wc -l`


ram_number=`dmidecode | grep -P -A5 "Memory\s+Device" | grep Size  | grep -v Range | wc -l`



# Hyperthreading
# Check if hyperthreading is enabled
if [ -f "/sys/devices/system/cpu/smt/active" ]; then
  HYPERTHREADING=$(cat /sys/devices/system/cpu/smt/active)
else
  HYPERTHREADING=1
fi

#You can remove the features you don't like by putting the # sign.
#echo -e ${text_color}Distro${remove_color}: $PRETTY_NAME
#echo -e ${text_color}Host${remove_color}: $host
#echo -e ${text_color}Kernel${remove_color}: $(uname -r)
#echo -e ${text_color}User${remove_color}: $user_hostname
#echo -e ${text_color}Packages${remove_color}: $packages
#echo -e ${text_color}Shell${remove_color}: $user_shell
#echo -e ${text_color}Session${remove_color}: $session_type
#echo -e ${text_color}Disk Usage${remove_color}: $disk_usage
#echo -e ${text_color}Archihecture${remove_color}: $(uname -m)
#echo -e ${text_color}CPU${remove_color}: $cpu_model x $cpu_number
#if [ "$HYPERTHREADING" -eq 1 ]; then
#echo -e ${text_color}Hyperthreading${remove_color}: enabled
#else 
#echo -e ${text_color}Hyperthreading${remove_color}: disabled
#fi
# echo -e ${text_color}GPU${remove_color}: $gpu
# echo -e ${text_color}RAM Usage${remove_color}: $ram_usage
# echo -e ${text_color}RAM Type${remove_color}: $ramType_number
# echo -e ${text_color}RAM Number${remove_color}: $ram_number
#echo -e ${text_color}RAM Info${remove_color}: $ramInfo x $ram_number
#echo -e ${text_color}GPU${remove_color}: $gpu
#echo 
getHardwareInfo
