#!/bin/bash
#                       /usr/local/bin/thumbnail_expire
#  http://crystalfaeries.net/posix/bin/thumbnail_expire
# celeste:crystalfaery THUMBNAIL_EXPIRE 2017-11-18 02:19:33+00:00
# $0 cleans trash cans and old thumbnails and temporary space to free disk space
# we run weekly for maintenance and more often when we actually run out of space
# $0 is executed as root by /etc/cron.d/rsnapshot:
# 05 02 * * 1 root /usr/local/bin/thumbnail_expire > /tmp/thumbnail_expire.out 2>&1 || /bin/cat /tmp/thumbnail_expire.out | /usr/bin/mail -s "expire errs: `hostname`" root
# The argument is an integer number of days of retention (see also /usr/local/bin/{dar,rsnapshot}limit)
# Notice some desktop software has its own built-in version of handling trash expiry automagically,
# and WE are PAYing absolutely NO ATTENTION TO the USER CONFIGURATION thereof, which MAY SURPRISE THE USER.
# (sadly, I have met a "luser" who actually used their trashcan to store files they wanted to keep!)
# With some extra coding we might be able to discover such configurations and honor them #################
# As the disk fills up we keep progressively less trash and thumbnails by iteratively calling this
# with fewer days of retention specified, a job handled by /usr/local/bin/{dar,rsnapshot}limit
# which implies that days of retention will be the minimum of 32 or
# 100 - (the max percentage fullness of disks specified in {dar,rsnapshot}limit)
# so you may have up to a month worth, or as little as a day of trash, thumbnails, and temporary files.

if [ "X$1" == "X" ]
then
	expiry=32					# default to one month of retention (primarily for cron)
else
	expiry=`echo "$1" | tr -d -c '[:digit:]'`	# sanitize input argument integer number of days retention
fi

# expire .xvpics thumbnails
# not coded yet:
# we should build a system wide list of .xvpics folders, then
# for each directory in the list, cd into that directory, then
# for each file in that directory, delete it if its parent file
# does not exist in the parent directory of the .xvpics directory
# to expunge orphanned thumbnails, but then also, while in the dir,
# we can expire thumbnails older than our expiry date of this execution.

# expire empty .hardlinks.txt files
find / -iname '.hardlinks.txt' -ctime +"$expiry" -mtime +"$expiry" -size 0 -exec rm {} \; 2>/dev/null &

# expire editor swap files:
find / -iname '.*.sw?$' -ctime +"$expiry" -mtime +"$expiry" -exec rm {} \; 2>/dev/null &

# expire temporary files:
/usr/bin/find /var/tmp /tmp -type f -ctime +"$expiry" -mtime +"$expiry" \! -name CACHEDIR.TAG -exec rm {} \; 2>/dev/null &
/usr/bin/find /var/tmp/kdecache-* /tmp/kde-* -type f -ctime +"$expiry" -mtime +"$expiry" \! -name CACHEDIR.TAG -exec rm {} \; 2>/dev/null &

# expire user Trash globally
for directory in $HOME/.local/share/Trash $( find / \( -name '.Trash-*' -o -name Trash \) -type d -print 2>/dev/null )
do
	/usr/bin/find "$directory" -type f -ctime +"$expiry" -mtime +"$expiry" \! -name CACHEDIR.TAG -exec rm {} \; 2>/dev/null &
done

# iterate users (edit appropriately for your installation)
for domicile in /home/celeste 
do

	# expire ~/.cache/media-art
	/usr/bin/find ${domicile}/.cache/media-art -iname '*.jpeg' -ctime +"$expiry" -mtime +"$expiry" -exec rm {} \; 2>/dev/null &

	# expire thumbnails (yeah, finally what the script is named for and originally written to do)
	if [ -d			"$domicile"/.thumbnails ]
	then
		cd		"$domicile"/.thumbnails || break
		if [ -f		CACHEDIR.TAG ]		# make sure we do not backup thumbnails
		then
			touch	CACHEDIR.TAG	2>/dev/null	# kilroy was here :-)
		else
			echo	"CACHEDIR.TAG missing... attempting to create in `pwd`."
			if	[ -f /var/cache/CACHEDIR.TAG ]
			then	# attempt to hard link to well known CACHEDIR.TAG (copy if different filesystem)
				ln /var/cache/CACHEDIR.TAG CACHEDIR.TAG || cp /var/cache/CACHEDIR.TAG CACHEDIR.TAG
			else	# or search for one in /home filesystem (presuming users don't each have their own filesystems)
				for tag in $(find /home -name CACHEDIR.TAG -type f 2>/dev/null); do
					ln $tag	CACHEDIR.TAG || cp $tag CACHEDIR.TAG
				done
			fi
		fi
		# iterate .thumbnails subdirectories
		for dir in $(/usr/bin/find	"$domicile"/.thumbnails -type d 2>/dev/null);do
			if [ "$dir" =	"$domicile"/.thumbnails ]
			then
				continue	# don't process .thumbnails itself
			fi
			/usr/bin/find "$dir" -type f -ctime +"$expiry" -mtime +"$expiry" \! -name CACHEDIR.TAG -exec rm {} \; 2>/dev/null &
		done
	fi

	if [ -d			"$domicile"/.miro/crashes ]
	then
		rm -rf		"$domicile"/.miro/crashes
	fi
	# expire thumbnails from Miro's icon cache
	if [ -d			"$domicile"/.miro/icon-cache ]
	then
		cd		"$domicile"/.miro/icon-cache || break
		if [ -f		CACHEDIR.TAG ]		# make sure we do not backup thumbnails
		then
			touch	CACHEDIR.TAG	2>/dev/null	# kilroy was here :-)
		else
			echo	"CACHEDIR.TAG missing... attempting to create in `pwd`."
			if	[ -f /var/cache/CACHEDIR.TAG ]
			then	# attempt to hard link to well known CACHEDIR.TAG (copy if different filesystem)
				ln /var/cache/CACHEDIR.TAG CACHEDIR.TAG || cp /var/cache/CACHEDIR.TAG CACHEDIR.TAG
			else	# or search for one in /home filesystem (presuming users don't each have their own filesystems)
				for tag in $(find /home -name CACHEDIR.TAG -type f 2>/dev/null); do
					ln $tag	CACHEDIR.TAG || cp $tag CACHEDIR.TAG
				done
			fi
		fi

		# expire .miro icon-cache
		/usr/bin/find "$domicile"/.miro/icon-cache -type f -ctime +"$expiry" -mtime +"$expiry" \! -name CACHEDIR.TAG -exec rm {} \; 2>/dev/null &
	fi

done
# expire .miro crash reports
/usr/bin/find /home/*/.miro/crashes \( -ctime +"$expiry" -o -mtime +"$expiry" \) -type f -exec rm {} \; 2>/dev/null &
exit 0		# failure is not an option when run by CRON 


syntax highlighted by Code2HTML, v. 0.9.1