#!/usr/bin/env bash

set -euo pipefail

MEDIA="/home/marcus/media"
MOVIES="/home/marcus/media/movies"
SERIES="/home/marcus/media/series"


MEDIA_SIZE=$(du -sh "$MEDIA" 2>/dev/null | awk '{print $1}')
MOVIES_SIZE=$(du -sh "$MOVIES" 2>/dev/null | awk '{print $1}')
SERIES_SIZE=$(du -sh "$SERIES" 2>/dev/null | awk '{print $1}')

DISK_INFO=$(du -h "$MEDIA" | awk 'NR==2')

FILESYSTEM=$(echo "$DISK_INFO" | awk '{print $1}')
DISK_SIZE=$(echo "$DISK_INFO" | awk '{print $2}')
DISK_USED=$(echo "$DISK_INFO" | awk '{print $3}')
DISK_AVAIL=$(echo "$DISK_INFO" | awk '{print $4}')
DISK_USEP=$(echo "$DISK_INFO" | awk '{print $5}')

echo "Folder sizes"
echo "----------------"
echo "$MEDIA : $MEDIA_SIZE"
echo "$MOVIES : $MOVIES_SIZE"
echo "$SERIES : $SERIES_SIZE"


read FILESYSTEM DISK_SIZE DISK_USED DISK_AVAIL DISK_USEP MOUNT <<< \
$(df -h --output=source,size,used,avail,pcent,target "$MEDIA" | tail -1)

echo
echo "Disk usage ($FILESYSTEM)"
echo "----------------"
echo "Total : $DISK_SIZE"
echo "Used  : $DISK_USED"
echo "Free  : $DISK_AVAIL"
echo "Usage : $DISK_USEP"
echo "Mount : $MOUNT"

# ==========================
# OPTIONAL WARNING
# ==========================
USAGE_NUM=${DISK_USEP%\%}

if [ "$USAGE_NUM" -ge 90 ]; then
  echo
  echo "⚠️  WARNING: Disk usage above 90%"
fi
