#!/usr/app/TurboShell/bin/bash
# Free space of the volume that holds a folder
set -euo pipefail
folder=${1:-.}
limit=${2:-90}
if [[ ! $limit =~ ^(0|[1-9][0-9]?|100)$ ]]; then
  echo "The limit is a number from 0 to 100"
  exit 2
fi

{
  read -r _
  read -r _ total _ free percent _
} < <(df -P -k "$folder")

used=${percent%\%}
echo "$((free / 1024 / 1024)) GiB of $((total / 1024 / 1024)) GiB are free, $used percent are used"
if ((used >= limit)); then
  echo "The limit is $limit percent"
  exit 1
fi
