#!/bin/bash -e

dir=/var/lib/archbuilddest/srcdest
ret=0
age=${1:-90}

while IFS= read -r -d $'\0' file; do
  if rm -rf -- "$file"; then
    echo "<6>Deleted $file"
  else
    echo "<3>Error deleting $file"
    ret=1
  fi
done < <(
  find "$dir" -maxdepth 1 -type f -atime +$age -print0

  rage=$((age + 1))
  while IFS= read -r -d $'\0' folder; do
    if [[ -z $(find "$folder" -type f -atime -$rage -print -quit) ]]; then
      printf '%s\0' "$folder"
    fi
  done < <(find "$dir" -mindepth 1 -maxdepth 1 -type d -print0)
)

exit $ret

# vim:set sw=2 et:
