#!/usr/bin/env bash

# {{ ansible_managed }}

# Clean up DokuWiki installation

function cleanup()
{
    local data_path="$1"        # full path to data directory of wiki
    local retention_days="$2"   # number of days after which old files are to be removed

    # purge files older than ${retention_days} days from attic and media_attic (old revisions)
    find "${data_path}"/{media_,}attic/ -type f -mtime +"${retention_days}" -delete

    # remove stale lock files (files which are 1-2 days old)
    find "${data_path}"/locks/ -name '*.lock' -type f -mtime +1 -delete

    # remove empty directories
    find "${data_path}"/{attic,cache,index,locks,media,media_attic,media_meta,meta,pages,tmp}/ \
        -mindepth 1 -type d -empty -delete

    # remove files older than ${retention_days} days from the cache
    if [ -e "${data_path}/cache/?/" ]
    then
        find "${data_path}"/cache/?/ -type f -mtime +"${retention_days}" -delete
    fi
}

cleanup_path="{{ dokuwiki__git_checkout + '/data' }}"

dokuwiki_farm="{{ dokuwiki__farm | bool | lower }}"
dokuwiki_farm_path="{{ dokuwiki__farm_path }}"
read -r -a dokuwiki_farm_animals <<< "{{ dokuwiki__farm_animals | join(' ') }}"

# Cleanup DokuWiki installations (path to datadir, number of days)
cleanup "${cleanup_path}" 180
if [ "${dokuwiki_farm}" = "true" ] ; then
    for animal in "${dokuwiki_farm_animals[@]}" ; do
        cleanup "${dokuwiki_farm_path}/${animal}/data" 180
    done
fi
