#!/bin/sh
APP="/Applications/Install macOS Big Sur.app"
PKG=~/Downloads/InstallAssistant.pkg
FREE_KB=$(df -k / | awk 'NR==2{print $4}')
FREE_GB=$((FREE_KB/1024/1024))

echo "== Disk =="
df -h / | awk 'NR==2{print "Free: "$4" of "$2}'
[ -f "$PKG" ] && ls -lh "$PKG" | awk '{print "pkg: "$5}'
[ -d "$APP" ] && echo "Installer app exists: $(du -sh "$APP" 2>/dev/null | cut -f1)"
if [ "$FREE_GB" -lt 15 ]; then
  echo ">> Only ${FREE_GB} GB free: too little to unpack the pkg (needs ~15 GB)."
elif [ "$FREE_GB" -lt 45 ]; then
  echo ">> ${FREE_GB} GB free: enough to unpack, not enough for the upgrade (~45 GB)."
else
  echo ">> ${FREE_GB} GB free: space is fine."
fi

echo "== install.log, latest errors =="
L=/var/log/install.log
[ -r "$L" ] || L="sudo"
if [ "$L" = "sudo" ]; then
  echo "(log not readable, rerun with: curl -s maclasse.plumppu.nu/felsok | sudo sh)"
else
  grep -iE "error|fail|space|denied|abort" "$L" | tail -12 \
    | sed -E 's/^[A-Z][a-z]{2} +[0-9]+ [0-9:]+ [^ ]+ //' | cut -c1-150
fi
