#!/bin/bash

# Written by:      fennek182
# Version:         2
# Last change:     Sun 2026-06-14
# Git repo:        https://git.fennek.xyz/fennek182/dotfiles
# Latest version:  https://git.fennek.xyz/fennek182/dotfiles/src/branch/main/.local/bin/stats
# Description:     For my personal use currently. Outputs useful disk usage and package stats.

# TODO: move repeating string manipluations to functions
# TODO: clean up formatting, use colors (and maybe glow)

# total size of installed pacman packages
sum=$(expac %m | awk '{sum+=$1} END {printf "%.2fGiB\n", sum/2^30}' | cut -c 1-4)
sum=$(awk "BEGIN{print $sum*1073741824/1000/1000/1000}" | cut -c-3 | tr . ,) #limit to 2 decimals, replace "." with ","

df -h -H -x tmpfs -x efivarfs -x devtmpfs -T -x vfat | head -n3
echo "----------"
echo "Pacman packages installed: $(pacman -Qq | wc -l)"
echo "Foreign packages (aur): $(pacman -Qm | wc -l)"
echo "explicitly installed packages: $(pacman -Qe | wc -l)"
echo "explicitly installed packages (without dependencies): $(pacman -Qet | wc -l)"
echo "Flatpak packages installed: $(flatpak list --app | wc -l)"
echo "==> Sum disk usage of installed packages: $sum GB"
echo "Size of pacman cache /var/cache/pacman/pkg/: $(dus /var/cache/pacman/pkg/ | sed 's/M/ MB/' | sed 's/G/ GB/' | tr . ,)"
echo "Size of home cache /home/tobias/.cache: $(dus ~/.cache/ / 2> >(grep -v 'Did not') | sed 's/M/ MB/' | sed 's/G/ GB/' | tr . ,)"
echo "   -> Size of yay cache /home/tobias/.cache/yay: $(dus ~/.cache/yay/ / 2> >(grep -v 'Did not') | sed 's/M/ MB/' | sed 's/G/ GB/')"
# echo "Size of systemd journals /var/log/journal: $(dus /var/log/journal | sed -r 's/([0-9])([a-zA-Z])/\1 \2/g; s/([a-zA-Z])([0-9])/\1 \2/g')B"
echo "Size of ~/.local/share/: $(dus ~/.local/share/ | sed -r 's/([0-9])([a-zA-Z])/\1 \2/g; s/([a-zA-Z])([0-9])/\1 \2/g' | tr . ,)B"
echo "Size of ~/.local/share/flatpak/: $(dus ~/.local/share/flatpak/ | sed -r 's/([0-9])([a-zA-Z])/\1 \2/g; s/([a-zA-Z])([0-9])/\1 \2/g')B"
echo "Size of ~/.local/share/cargo/: $(dus ~/.local/share/cargo/ | sed -r 's/([0-9])([a-zA-Z])/\1 \2/g; s/([a-zA-Z])([0-9])/\1 \2/g' | tr . ,)B"
echo "Size of ~/.local/share/go/: $(dus ~/.local/share/go/ | sed -r 's/([0-9])([a-zA-Z])/\1 \2/g; s/([a-zA-Z])([0-9])/\1 \2/g' | tr . ,)B"
echo "Size of ~/.local/share/npm/: $(dus ~/.local/share/npm/ | sed -r 's/([0-9])([a-zA-Z])/\1 \2/g; s/([a-zA-Z])([0-9])/\1 \2/g' | tr . ,)B"
echo "Size of ~/.local/share/nvim/: $(dus ~/.local/share/nvim/ | sed -r 's/([0-9])([a-zA-Z])/\1 \2/g; s/([a-zA-Z])([0-9])/\1 \2/g' | tr . ,)B"
echo ".var size: $(dus ~/.var/ | grep --color=never -oE '^\S*')B"
echo ".var size without Steam: $(dust -b -c -o si -n0 -v "com.valvesoftware.Steam" .var | grep --color=never -oE '^\S*' | sed -r 's/([0-9])([a-zA-Z])/\1 \2/g; s/([a-zA-Z])([0-9])/\1 \2/g'| tr . ,)B"
echo ".var size only Steam: $(dust -b -c -o si -n0 ~/.var/app/com.valvesoftware.Steam/ | grep --color=never -oE '^\S*' | sed -r 's/([0-9])([a-zA-Z])/\1 \2/g; s/([a-zA-Z])([0-9])/\1 \2/g' | tr . ,)B"
echo "----------"
echo "Top 10 pacman packages by disk usage:"
expac -H M '%m\t%n' | sort -h | tail -n10

echo "Top 10 flatpak apps by disk usage:"
flatpak list --app --columns=size,name | sort -g | tail -n10

GREEN='\033[0;32m'
#printf "I ${GREEN}love \033[0mStack Overflow\n"

echo "enter sudo password for / disk usage (system):"
echo 'sudo /bin/du -xcsh /etc /opt /root /run /usr /var && echo " " && sudo /bin/du -sh /home'
sudo /bin/du -xcsh /etc /opt /root /run /usr /var && echo " " && sudo /bin/du -sh /home
