#!/usr/bin/env bash
# http://redsymbol.net/articles/unofficial-bash-strict-mode/

#########################################################################################
#  SCRIPT: clear-podman-cache.sh
#  Description: Used to cleanup unused podman containers and volumes
######################################################################################
IFS=$'\n\t'
set -euo pipefail

if ! [ -x "$(command -v podman)" ]; then
  echo -e "INFO: podman installation not found, skipping clear-podman-cache"
  exit 0
fi

podman_API_VERSION=$(podman version --format '{{.Client.APIVersion}}')
podman_CLIENT_VERSION=$(podman version --format '{{.Client.Version}}')
podman_CLIENT_VERSION="${podman_CLIENT_VERSION%%-*}" # strip -whatever, so 23.0.0-rd becomes 23.0.0
REQUIRED_podman_API_VERSION=1.25
FILTER_FLAG="${FILTER_FLAG:-label=com.gitlab.gitlab-runner.managed=true}"

usage() {
  echo -e "\nUsage: $0 prune-volumes|prune|space|help\n"
  echo -e "\tprune-volumes    Remove all unused containers (both dangling and unreferenced) and volumes"
  echo -e "\tprune            Remove all unused containers (both dangling and unreferenced)"
  echo -e "\tspace            Show podman disk usage"
  echo -e "\thelp             Show usage"
  exit 1
}

if awk "BEGIN {exit !(\"$podman_API_VERSION\" < \"$REQUIRED_podman_API_VERSION\")}"; then
  echo -e "ERROR: Your current API version is lower than ${REQUIRED_podman_API_VERSION}. The client and daemon API must both be at least ${REQUIRED_podman_API_VERSION}+ to run these commands. Kindly upgrade your podman version."
  exit 1
fi

COMMAND="${1:-prune-volumes}"

case "$COMMAND" in

  prune)

    echo -e "\nCheck and remove all unused containers (both dangling and unreferenced)"
    echo -e "-----------------------------------------------------------------------"

    if awk "BEGIN {exit !(\"$podman_CLIENT_VERSION\" < \"17.06.1\")}"; then
      # The podman system prune command without pruning volumes does not exist before 17.06.1, so we need to use podman rm
      CONTAINERS=$(podman ps -a -q \
                  --filter=status=exited \
                  --filter=status=dead \
                  --filter="$FILTER_FLAG")

      if [ -n "${CONTAINERS}" ]; then
        podman rm "${CONTAINERS}"
      fi
    else
      podman system prune -af --filter "$FILTER_FLAG"
    fi

    exit 0
    ;;

  space)

    echo -e "\nShow podman disk usage"
    echo -e "----------------------"
    podman system df

    exit 0
    ;;

  help)

    usage
    ;;

  prune-volumes)

    echo -e "\nCheck and remove all unused containers (both dangling and unreferenced) including volumes."
    echo -e "------------------------------------------------------------------------------------------"

    if awk "BEGIN {exit !(\"$podman_CLIENT_VERSION\" > \"5.6.1\")}"; then
      # Prior to 23.0, `podman volume prune` didn't support --all
      podman system prune -af --filter "$FILTER_FLAG"
      podman volume prune -f --filter "$FILTER_FLAG"
    else
      podman system prune -af --filter "$FILTER_FLAG"
      podman volume prune -af --filter "$FILTER_FLAG"
    fi

    exit 0
    ;;

esac
