#!/bin/bash
#                       /usr/local/bin/used
# https://crystalfaeries.net/posix/bin/used
# celeste:crystalfaery USED 2017-02-06 00:13:50+00:00
# Related to: /usr/local/bin/{find_,}images /usr/local/bin/seen
# The purpose of this script is to determine if an image file
# IS or IS NOT {yet,already} "used" in our website
# either as a displayed image (<img src="">) or
# linked to within the website(<a  href="">).
# Because we may have come upon the image in many ways,
# we wish to accept references to it in many formats, (e.g.):
# http{,s}://crystalfaeries.net/{clipart,imgs}/{,[subdir]/}FILENAME.{gif,jpg,png,svg}
#      /home/crystalfaeries.net/{clipart,imgs}/{,[subdir]/}FILENAME.{gif,jpg,png,svg}
#                                                          FILENAME.{gif,jpg,png,svg}
#
# we should validate that the file exists in some such path (else complain),
# we should search for such references as mentioned, to such file, and display them.
# we should find all equivalently named files (multiple (e.g. hardlinked) copies), and
# we should find all equivalent         files of different names but same content.
#
# invocation shall be of the nature: "used [{URL,PATH,FILENAME},...]

if [ $# -eq 0 ]
then	# invoked without argument is dysfunctional request for help
	head -n 21 $0
	exit	0
fi
case "${1}" in
-h | --help)
	head -n 21 $0
	exit	0
	;;
-v | --version)
	head -n 4 $0 | tail -n 1
	exit	0
	;;
esac

# we must loop on all provided command line arguments (sigh... iterate, iterate, dance to the music...)
while [ $# -ne 0 ]
do
	this="${1}"	#  accept the command-line argument
	shift		# consume the command-line argument
	that="$(echo ${this} | sed 's/^.*https:\/\/www.crystalfaeries.net//;s/^.*http:\/\/www.crystalfaeries.net//;s/^.*https:\/\/crystalfaeries.net//;s/^.*http:\/\/crystalfaeries.net//;s/^.*file:\/\///;s/^.*\/home\/crystalfaeries.net//')"	# strip off any leading URL syntax
# we "should be" left with a path of the nature of /clipart/... or /imgs/...
# NOTE: WE ARE TOTALLY IGNORING ALL IMAGES BURIED WITHIN THE HIERARCHIES OF /audio/ OR /video/
	if [ -f /home/crystalfaeries.net/"${that}" ]
	then	# referenced file exists
		echo "EXISTENT: ${that}"
	else	# referenced file missing
		echo "MISSING_: ${that}"
	fi
done
exit

################################################################################
# NOTE: in searching audio files, be aware that we have relocated
# out of the hierarchy of /home/raid/audio = /home/audio
# the directories of INCOMING audio-only versions of downloaded videos (e.g. youtube.com):
# lrwxrwxrwx 1 celeste staff 24 Oct 31 12:17 /home/audio/m4a -> /home/raid/downloads/m4a/
# lrwxrwxrwx 1 celeste staff 25 Oct 31 12:17 /home/audio/opus -> /home/raid/downloads/opus/
# lrwxrwxrwx 1 celeste staff 25 Oct 31 12:17 /home/audio/webm -> /home/raid/downloads/webm/
# lrwxrwxrwx 1 celeste staff 24 Oct 31 12:17 /home/audio/ogg -> /home/raid/downloads/ogg/
# lrwxrwxrwx 1 celeste staff 24 Oct 31 12:17 /home/audio/wav -> /home/raid/downloads/wav/
# lrwxrwxrwx 1 celeste staff 25 Oct 31 12:17 /home/audio/flac -> /home/raid/downloads/flac/
# lrwxrwxrwx 1 celeste staff 24 Oct 31 12:17 /home/audio/mp3 -> /home/raid/downloads/mp3/
# lrwxrwxrwx 1 celeste staff 19 Nov  4 07:00 /home/audio/spx -> /home/downloads/spx
# lrwxrwxrwx 1 celeste staff 24 Oct 31 07:07 /home/audio/mid -> /home/raid/downloads/mid/
# 


syntax highlighted by Code2HTML, v. 0.9.1