#!/bin/bash set -e test_if_ancient() { local d="$1" local ttl="$2" mtime="$(stat --format "%Y" "$d")" tod="$(($mtime + 3600*24*$ttl))" now="$(date +%s)" [ "$optVerbose" -ge "2" ] && echo " Considering $d: ttl=$ttl, mod time=$mtime, time of death=$tod, now=$now" [ "$tod" -lt "$now" ] && return 0 return 1 } cleanit() { local d="$1" local ttl="$2" if [ -e "$d/.publish.ttl" ]; then ttl="$(cat "$d/.publish.ttl")" fi if test_if_ancient "$d" "$ttl"; then [ "$optVerbose" -ge "1" ] && echo "Purge $d" [ "$optDonot" = "1" ] || rm -rf "$d" fi return 0 } usage() { cat << EOF usage: $0 [OPTIONS] OPTIONS: -h Show this message. -n Do not actually purge directories, but show what would be done. -v Give verbose output. Be more verbose with -vv. EOF } optVerbose=0 optDonot=0 while getopts "hnv" OPTION; do case $OPTION in h) usage exit 0 ;; n) optDonot=1 ;; v) optVerbose=$(($optVerbose + 1)) ;; esac done ttl=128 base="" [ -e ~/.publish-purge.cfg ] && . ~/.publish-purge.cfg if [ -z "$base" ]; then echo >&2 "$0: Please setup \$base in ~/.publish-purge.cfg" exit 1 fi if ! [ -e "$base" ]; then echo >&2 "$0: $base does not exist or is not a directory" exit 1 fi cd "$base" # We would not like to accidentily delete files in the wrong directory, hence # the paranoia check if ! [ -e .publish-purge.token ]; then echo >&2 "$0: Place a .publish-purge.token in $base" exit 1 fi for d in *; do cleanit "$d" "$ttl" done # vim:set et: # vim:set ts=4: # vim:set shiftwidth=4: