#!/bin/sh
PATH=/usr/sbin:/usr/bin:/sbin:/bin

# in case of `dpkg -r' leaving conffile.
if ! [ -x /usr/sbin/tmpreaper ]; then
    exit 0
fi

# Remove `/tmp/...' files not accessed in 7 days, protecting the .X, .ICE,
# .iroha and .ki2 files; but removing symlinks. For directories not the access
# time, but the modification time is used (--mtime-dir), as reading a directory
# to check the contents will update the access time!
#
# /tmp/. is given, not the plain /tmp you might expect, as this accomodates the
# situation where /tmp is a symlink to some other place.
#
# Note that the sockets are safe even without the `--protect', unless `--all'
# is given, and the `.X*-lock' files would be safe also, as long as they have
# no write permissions, so this particular protect is mainly illustrative, and
# redundant.  For best results, don't try to get fancy with the moustache
# expansions.  KISS.  Always --test your protect patterns.
#
# Immutable files (such as ext3fs' .journal) are not (cannot be) removed;
# when such a file is encountered when trying to remove it, no error is given
# unless you use the --verbose option in which case a message is given.
#
# In case you're wondering: .iroha is for cannaserver and .ki2 is for kinput2
# (japanese software, lock files).
# journal.dat is for (older) ext3 filesystems
# quota.user, quota.group is for (duh) quotas.

# ! Important !  The "set -f" below prevents the shell from expanding
#                file paths, which is vital for the configuration below to work.

set -f

if [ -s /etc/tmpreaper.conf ]; then
    . /etc/tmpreaper.conf
else
    TMPREAPER_TIME=7d
    TMPREAPER_PROTECT_EXTRA=''
    TMPREAPER_DIRS='/tmp/.'
fi

nice -n10 tmpreaper --mtime-dir --symlinks $TMPREAPER_TIME  \
  --protect '/tmp/.X*-{lock,unix,unix/*}' \
  --protect '/tmp/.ICE-{unix,unix/*}' \
  --protect '/tmp/.iroha_{unix,unix/*}' \
  --protect '/tmp/.ki2-{unix,unix/*}' \
  --protect '/tmp/lost+found' \
  --protect '/tmp/journal.dat' \
  --protect '/tmp/quota.{user,group}' \
  `for i in $TMPREAPER_PROTECT_EXTRA; do echo --protect $i; done` \
  $TMPREAPER_DIRS
