#!/bin/bash
#                       /usr/local/bin/find_images
#  http://crystalfaeries.net/posix/bin/find_images
# celeste:crystalfaery FIND_IMAGES 2019-08-30 00:55:53+00:00
# find images by decreasing size in specified directory list, or
# find images by decreasing size everywhere for /usr/local/bin/images or for cron job:
#	DAILY TASKS
#Minute	Hour	DOM	Month	DOW	Command
#40	02	 *	 *	*	/usr/local/bin/find_images		# update empty image files list

let help=10	# line number - 1
target="/"	# default to searching EVERYwhere

if	[ $# -ne 0 ]
then			# we have an argument
    case "${1}" in
	-v | --version )
		head -n 4 $0 | tail -n 1
		exit;;
	-h | --help )
		head -n ${help} $0
		exit;;
	* )	target="$@"	# search specified list
		;;
    esac
fi

# continue with existing non-zero, or create new list of images
if [ -s ~/.imgs.txt ]
then	# ONLY replace a missing or empty filelist (user is in middle of operations)
	exit 0	# No-Operation
else	# exclude removable media (e.g. USB flashdrives, SD cards), Trash, {website,rsnapshot,dar} backups
	nice find ${target}						  \
	\( \( -path	/media					-prune \) \
	-o \( -path	/home/rsnapshot				-prune \) \
	-o \( -path	/home/.Trash-${UID}/			-prune \) \
	-o \( -path	$HOME/.local/share/Trash		-prune \) \
	-o \( -path	$HOME/.cache				-prune \) \
	-o \( -path	$HOME/.thumbnails			-prune \) \
	-o \( -iname	'*.gif'		\! -size 0 -exec du {} \; \) \
	-o \( -iname	'*.jpeg'	\! -size 0 -exec du {} \; \) \
	-o \( -iname	'*.jpg'		\! -size 0 -exec du {} \; \) \
	-o \( -iname	'*.png'		\! -size 0 -exec du {} \; \) \
	-o \( -iname	'*.pnm'		\! -size 0 -exec du {} \; \) \
	-o \( -iname	'*.svg'		\! -size 0 -exec du {} \; \) \
	-o \( -iname	'*.tif'		\! -size 0 -exec du {} \; \) \
	\) 2> /dev/null \
	| sort -rn \
	| cut -f2- \
	>	~/.imgs.txt	# the size sorted list of image files
fi
exit $?
