#!/bin/bash

exit 1	#DEBUGGING EXIT: CODE BELOW HERE IS NOT YET DEBUGGED!

# Scan for duplicate files in directories most likely to accumulate them.
# Released under GNU LGPL 3 by celeste:crystalfaery 2012-01-10 19:21:28+00:00
# the list of directories should have home directory relative ones listed first
# for d in /home/audio /home/galleries/divas ~/documents ~/Documents ~/Downloads "${HOME}"/downloads /usr/local/src /home/video /home/www /usr/local/bin /usr/share/icons /usr/share/openclipart /usr/share/icons
for d in /tmp/dupes/
do
	cd "$d" || continue	# we must have permission to enter directory
# DANGER, DANGER, WILL ROBINSON... WE MUST verify we're playing on a linkable filesystem
# DANGER, DANGER, WILL ROBINSON... There may be mountpoints within or below this directory,
# where there could be mounted non-linkable filesystems, therefore, recurse into subdirectories
# we will only process files in our immediate directory, handling directories by recursion.
# which of my scripts already recurses? why name_tidy does, therefore shall we
# converge name_tidy and filedupes into one new script named file_tidy? yes we shall.
	if [ -f .fdupes.txt ] 
	then	# we already have generated the file
		touch /tmp/"$0"."$$"	#kilroy was here
	else	# we don't yet have a listing of duplicates
		fdupes -1 -r . > .fdupes.txt 2> /dev/null
	fi
	if [ -s .fdupes.txt ]
	then
		mail -s "`hostname`:`pwd`/.fdupes.txt" kahealani < /dev/null
	else
		rm .fdupes.txt	# no need to keep an empty file
	fi
done
#!/bin/bash
# This script (hereby licensed under LGPL-3.0) by  SL: Celeste Python  /  RL: celeste:crystalfaery
# 2012-01-20 23:16:54+00:00 angela@crystalfaeries.net http://www.kahealani.com/kahealani/angela_kahealani.html
# This is not exactly genric, but oriented to cleaning our ~/Music directory
# It supports cleaning up the streamrips of http://sl.magnatune.com/New%20Age
# It supports cleaning up Jazz downloads from usenet
# we tidy the names of our arguments, their contents, or current directory
# we should accept files as arguments versus demanding  whole directory
# we should accept a list of arguments
# we presume to do the current directory or the one named as argument (after optional recursion flag -r)
# Wed Aug 18 11:49:31 HST 2010 replaced audio diff with automagic rsyncing of file to file and dir to dir
# 2011-10-08 19:14:20+00:00 had to hack the file renaming logic to accomodate the NTFS handling of case
# 2011-11-14 00:39:48+00:00 fixing bug with rsyncing a empty non cannonically named dir over its empty cannonically named empty dir.
# 2011-12-18 18:47:46+00:00 fixing rsync failure within a dir where two copies of a file exist, one with a leading "-" in name; interpreted as erroneous rsync option
# 2012-02-10 23:05:54+00:00 yet another hack around website stupidity which gives us .mp4 video files with butchered names
#	Audio Player and Alert Sound:
	player="mplayer"
	alert="/usr/share/sounds/gnome/default/alerts/bark.ogg"
let recurse=1 # false
if [ -n "$1" ]	# any arguments on command line?
then
	if [ "$1" = "-r" ] # yes, "-r" tells us to recurse subdirectories
	then
		let recurse=0 # true
		shift
	fi
fi
if [ -n "$1" ]	# any target directory arguments on command line?
then
	if [ -d "$1" ] # other arguments should be directories to process
	then
		cd "$1"	|| exit -1 # make next directory to process be current working directory
	else
		if [ -h "$1" ] # other arguments could be links to directories to process
		then
			cd "$1"	|| exit -1 # make next directory to process be current working directory
		else
			echo time to edit "$0" to handle non directories like "$1" as arguments
			exit -1
		fi
	fi
fi

directory="`pwd`"
# here we protect a directory from tidying (total kludge)
if [ "astro" = "$directory" ]
then
	exit 0	# oh, yeah, this is very nonstandard hackish
else
	echo tidying $directory	# normal execution continues
fi

let loop=1		# start the loop
while [[ $loop > 0 ]] ; do
    let loop=0		# end the loop when we make no changes in a pass
    for i in *; do	# iterate through each item in current working directory
			# Files are renamed, though .* files are untouched
	if   [ "$i" == "CACHEDIR.TAG" ]
		then continue	# leave CACHEDIR.TAG files alone
	elif [ "$i" == "HEADER.html" ]
		then continue	# leave HEADER.html files alone
	elif [ "$i" == "README.html" ]
		then continue	# leave README.html files alone
	elif [ "$i" == "Incomplete Downloads" ]
		then continue	# leave README.html files alone
	fi
	if [ -a "$i" ] 
	then		# Accesible
		OLDNAME="$i"
		NEWNAME=`echo "$i"				| \
		sed 's/PREVIEW, buy at http---magnatune.com//g'	| \
		sed 's/PREVIEW- buy it at www.magnatune.com//g'	| \
		sed 's/PREVIEW- buy it at magnatune.com//g'	| \
		tr ' ' '_' | tr A-Z a-z				| \
		sed 's/^_//g'					| \
		sed 's/^-//g'					| \
		sed 's/^~//g'					| \
		sed 's/_$//g'					| \
		sed 's/-$//g'					| \
		sed 's/~$//g'					| \
		sed 's/_\././g'					| \
		sed 's/-\././g'					| \
		sed 's/~\././g'					| \
		sed 's/preview//g'				| \
		sed 's/buy_it_at//g'				| \
		sed 's/buy-it-at//g'				| \
		sed 's/buy_at//g'				| \
		sed 's/buy-at//g'				| \
		sed 's/http//g'					| \
		sed 's/www\.magnatune\.com//g'			| \
		sed 's/magnatune\.com//g'			| \
		sed 's/magnatune_compilation-//g'		| \
		sed "s/#/./g"					| \
		sed "s/:/./g"					| \
		sed "s/;/./g"					| \
		sed "s/'/_/g"					| \
		sed 's/\"/_/g'					| \
		sed 's/(/_/g'					| \
		sed 's/)/_/g'					| \
		sed 's/,/_/g'					| \
		sed 's/\&/+/g'					| \
		sed 's/_+/+/g'					| \
		sed 's/+_/+/g'					| \
		sed 's/-+/+/g'					| \
		sed 's/+-/+/g'					| \
		sed 's/+\./+/g'					| \
		sed 's/\.+/+/g'					| \
		sed 's/++/+/g'					| \
		sed 's/--/-/g'					| \
		sed 's/-_/-/g'					| \
		sed 's/_-/-/g'					| \
		sed 's/__/_/g'					| \
		sed 's/_\./\./g'				| \
		sed 's/\._/\./g'				| \
		sed 's/-\./\./g'				| \
		sed 's/\.-/\./g'				| \
		sed 's/\.\./\./g'				| \
		sed 's/format=[0-9]*kb+mpeg4/video.mp4/'	| \
		cat`
		if [ "$NEWNAME" != "$OLDNAME" ]
		then
			let loop=1+$loop	# we had to rename something and should loop because further tidy may be necessary
#			echo ""			# a blank line between files renamed for easy reading
			if [ -d "$NEWNAME" ]
			then 								# we have at least one directory involved
				if [ -d "$OLDNAME" ]
				then							# rsync the directories
					rmdir "$OLDNAME"
					if [ $? != 0 ]
					then
						rsync -auvzH ./"$OLDNAME"/.??*	"$NEWNAME" && rm -rf ./"$OLDNAME"/.??* 2>/dev/null
						rsync -auvzH ./"$OLDNAME"/*	"$NEWNAME" && rm -rf ./"$OLDNAME"
					fi
				else							# file vs directory
					"$player" "$alert"				# alert the human
					echo I want to rename the file "$OLDNAME" over the "$NEWNAME" directory, which would be bad, so you fix it manually...
					echo   it could be a collision of a directory and a symbolic link... in which case edit $0
					exit -2
				fi
			else								# good, no directory in the way
				if [ -f "$NEWNAME" ]					# do we already have a file by this name?
				then							# filename collision, let's update
					if [ "$NEWNAME" == "$(echo "$OLDNAME" | tr '[:upper:]' '[:lower:]')" ]
					then						# EEEK, name match requires special handling on NTFS
						mv		./"$OLDNAME"	./"$OLDNAME"-
						rsync -auvzH	./"$OLDNAME"-	"$NEWNAME" && rm ./"$OLDNAME"-
					else						# Phew, no need to dance around Bill Gates steaming pile of excrement
						rsync -auvzH	./"$OLDNAME"	"$NEWNAME" && rm ./"$OLDNAME"
					fi
				else
					mv -v -- ./"$OLDNAME" "$NEWNAME"		# no filename collision, just rename
				fi
			fi
		fi

		# Directories are recursed if we were invoked with option -r
		if [ $recurse = 0 ]
			then	# we were commanded to recurse
				if [ -d "$NEWNAME" ] 
				then	# directory
					"$0" -r "$NEWNAME"	# Follow the White Rabbit, Neo.
				fi
		fi
	fi
    done	# i
done	# loop
exit 0

# Released under GNU General Public License 3 by celeste:crystalfaery 2011-03-31 19:09:49+00:00
# http://www.crystalfaeries.net/linux/bin/name_tidy

# alternate space removal algorithm:
for s in *\ * ; do
	rename 's/ /_/g' "$s"
done
