#!/usr/bin/env bash

# omarchy:summary=Check free disk space required for an update
# omarchy:hidden=true

set -e

if [[ ${OMARCHY_UPDATE_FORCE:-0} == "1" ]]; then
  exit 0
fi

# Based: macOS df has no --output; the POSIX form reads the same column.
if [[ $(omarchy-os) == darwin ]]; then
  available_bytes=$(df -Pk / 2>/dev/null | awk 'NR == 2 { printf "%.0f\n", $4 * 1024 }')
else
  available_bytes=$(df --output=avail --block-size=1 / 2>/dev/null | tail -n 1)
fi

if [[ $available_bytes =~ ^[0-9]+$ ]] && (( available_bytes < 10 * 1024 * 1024 * 1024 )); then
  echo "You need at least 10 GiB free to safely update Omarchy."
  exit 1
fi

exit 0
