#!/bin/sh
# @param $1 mimimum free disc space
# @param $2 --force to for execution indepentend on how much disc space is free

minimum_percent_cleanup_disc_space="$1"
force_freeing=false
echo "Checking free disc space..."
df
if [ $# -gt 0 ] && [ "$2" = "--force" ]; then
  echo "Forcing disc space freeing."
  force_freeing=true 
fi 
for disc_use_percent in $(df --output=pcent | sed 1d)
do
    disc_use_percent_number=$(echo "$disc_use_percent" | sed "s/%//")
    if [ "$disc_use_percent_number" -gt "$minimum_percent_cleanup_disc_space" ]; then
      echo "WARNING: ${disc_use_percent_number}% exceeds the limit of ${minimum_percent_cleanup_disc_space}%."
      force_freeing=true 
    fi
done
if [ "$force_freeing" = true ]; then
  echo "cleaning up /tmp" &&
  find /tmp -type f -atime +10 -delete || exit 1
  
  {% if BACKUPS_FOLDER_PATH is defined and SIZE_PERCENT_MAXIMUM_BACKUP is defined %}
  echo "cleaning up backups" &&
  systemctl start {{ SYS_SERVICE_CLEANUP_BACKUPS }} || exit 2
  {% endif %}

  if command -v docker >/dev/null 2>&1 ; then
    echo "cleaning up docker (prune + anonymous volumes) via systemd service" &&
    systemctl start {{ SYS_SERVICE_CLEANUP_DOCKER }} || exit 3

    nextcloud_application_container="{{ applications | get_app_conf('web-app-nextcloud', 'docker.services.nextcloud.name') }}"
    if [ -n "$nextcloud_application_container" ] && [ "$(docker ps -a -q -f name=$nextcloud_application_container)" ] ; then
      echo "cleaning up docker nextcloud" &&
      docker exec -u www-data $nextcloud_application_container /var/www/html/occ files:cleanup || exit 4
      docker exec -u www-data $nextcloud_application_container /var/www/html/occ trashbin:cleanup --all-users || exit 5
      docker exec -u www-data $nextcloud_application_container /var/www/html/occ versions:cleanup || exit 6
    fi

    # Mastodon cleanup (remote media cache)
    mastodon_application_container="{{ applications | get_app_conf('web-app-mastodon', 'docker.services.mastodon.name') }}"
    mastodon_cleanup_days="1"

    if [ -n "$mastodon_application_container" ] && docker ps -a --format '{% raw %}{{.Names}}{% endraw %}' | grep -qw "$mastodon_application_container"; then
      echo "Cleaning up Mastodon media cache (older than ${mastodon_cleanup_days} days)" &&
      docker exec -u root "$mastodon_application_container" bash -lc "bin/tootctl media remove --days=${mastodon_cleanup_days}" || exit 8

      # Optional: additionally remove local thumbnail/cache files older than X days
      # Warning: these will be regenerated when accessed, which may cause extra CPU/I/O load
      # docker exec -u root "$mastodon_application_container" bash -lc "find /mastodon/public/system/cache -type f -mtime +${mastodon_cleanup_days} -delete" || exit 9
    fi
  fi

  if command -v pacman >/dev/null 2>&1 ; then
    echo "cleaning pacman cache" &&
    yes | pacman -Sc || exit 7
  fi

  echo "cleanup finished."
else
  echo "Sufficient disc space available."
  echo "To force the freeing of disc space pass the parameter --force."
fi
exit 0