#!/bin/bash

TOPDIR=/mnt/koji
TIMEARG="-mtime +14"

# for tasks, try to remove as a unit
for x in $(find "$TOPDIR"/work/tasks/ -mindepth 2 -maxdepth 2 -type d $TIMEARG); do
    find "$x" -xdev '!' -type d -print0 | xargs -0 -r rm -f
    find "$x" -xdev -depth -type d -print0 | xargs -0 -r rmdir
done

# for anything else, just remove old stuff
# but don't remove the top level dirs (e.g. cli-build)
for x in $(find "$TOPDIR"/work -maxdepth 1 -mindepth 1 \! -name tasks); do
    find "$x" -xdev '!' -type d $TIMEARG -print0 | xargs -0 -r rm -f
    find "$x" -xdev -depth -mindepth 1 -type d -empty -print0 | xargs -0 -r rmdir
done
