#!/bin/bash
#                       /usr/local/bin/rip_tidy
#  http://crystalfaeries.net/posix/bin/rip_tidy
# celeste:crystalfaery RIP_TIDY 2016-09-07 19:00:45+00:00
# NOTE:	Not yet reviewed for XDG compliance, and likely depends on other crystalfaeries.net scripts.

# Presuming there have been one or more streamrippers running,
# dumping into /home/audio/streams/
# then by invoking rip_tidy, we terminate all the streamrippers
# then tidy the names of the files and directories generated, and
# fill-in holes in the archive from any incomplete downloads.

pkill streamripper	# which generates non POSIX filenames
sleep 60		# we could test for running processes instead
cd /home/audio/streams||exit 1	# default output of streamripper jobs.
/usr/local/bin/name_tidy		# first tidy the top level of directories names.
			# secondly as a side effect that will merge new un-tidied files into the preexisting directory of the tidied name.
for directory in *	# then for each stream/directory
do			# tidy it else complain of failure.
	if [ -d					 $directory/incomplete ]
	then	# we have an incomplete directory to handle
		pushd	$directory/incomplete || continue
		find . -iname '*([0-9]+).*' -exec rm {} \;	# remove duplicates that got numbered for multiple copies
		/usr/local/bin/name_tidy			# tidy the incompletes (partial files)
		touch -t 197001012359	*	# partials get backdated to make them obsolete compared to any whole copies
		rsync -auvzH		* ../	# save the fragments
		popd
		rm -rf	$directory/incomplete	# in(tegration)complete
	fi
	/usr/local/bin/name_tidy -r	$directory		# tidy the integrated directory
done
exit $?	# exit status code is from the loop


syntax highlighted by Code2HTML, v. 0.9.1