#!/usr/bin/perl -w
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
#NAME
#  delrepdups - Delete files that are duplicates (linked to) repertoire files
#
#SYNOPSIS
#  delrepdups [file]..
#
#REQUIRES
#
#DESCRIPTION
#  This is a special-purpose file for adjusting the directories in  which  my
#  *.abc  files  are  stored.   At present, it is used to unlink "repertoire"
#  files from my .../abc/*/ "global" files, to trim their sizes somewhat.  By
#  "repertoire file" is meant files in the [a-z]*[0-9] "rep" directories.
#
#  This program check to see if any of the listed files are in any of the rep
#  directories, and deletes the ones that are.
#
#  If no files are named on the command line, the default is to check all the
#  *.abc files in the [a-z]*[a-z] directories.  This program is basically run
#  to clean out these directories, which have been used as "global" pools  of
#  various types of tunes.
#
#OPTIONS
#
#EXAMPLES
#
#FILES
#
#BUGS
#
#SEE ALSO
#
#AUTHOR
#  John Chambers <jc@trillian.mit.edu>
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #

$| = 1;
$exitstat = 0;
($P = $0) =~ s".*/"";
$V = $ENV{"V_$P"} || $ENV{"D_$P"} || 2;	# Verbose level.

if ($ENV{"Lim_$P"} =~ /^(\d+)/) {
	$limit = int($1);
	print STDERR "$P: Limited to $limit files.\n" if $V>0;
}

$home = $ENV{HOME} || ".";
if (-f ($bkup = "$home/sh/Backup.pm")) {
	require $bkup;
} else {
	print STDERR "$P: Can't find Backup.pm module.\n" if $V>0;
	exit 1;
}
print "$P: bkup program is '$bkup'\n" if $V>0;

$deletes = 0;	# Cound of files deleted/renamed
@files = ();	# List of files to check

@repfiles = `ls [a-z]*[0-9]/*.abc`;		# List the repertoire files
for $repfile (@repfiles) {				# Get the ID info for each file
	$repfile =~ s/[\r\s]$//;			# The file names end with a newline
	($dev,$ino) = stat($repfile);		# The info that we use about the file
	$repinfo{"$dev:$ino"} = $repfile;	# Record the ID info for one file
}

for $a (@ARGV) {
	if (-f $a) {
		push @files, $a;
	}
}
$files = int(@files);
unless ($files) {			# Default: list all the mass-collection directories
	for $l ('a' .. 'z') {	# Kludge to handle "Argument list too long" errors
		@tmp = `ls $l*[a-z]/*.abc`;	# Directories starting with each letter
		$n = int(@tmp);
		print "$P: '$l' directories have $n .abc files.\n" if $V>1 && $n>0;
		for $f (@tmp) {
			$f =~ s/[\r\s]+$//;		# Trim away newlines
			push @files, $f;		# List of deletable files
		}
	}
}
print STDERR "$P: We have $files files to check.\n" if $V>0;

file:
for $file (@files) {
	$file =~ s/[\r\s]+$//;
	if (-d $file) {
		print "$P: $file is a directory; get its .abc files ...\n" if $V>0;
		@tmp = `ls $file/*.abc`;	# ABC files in $file/
		$n = int(@tmp);	
		if ($n > 0) {
			push @files, @tmp;
		} else {
			print STDERR "$P: Directory $a contains no .abc files.\n" if $V>0;
		}
		next file;
	}
	($dev,$ino) = stat($file);
	if ($repfile = $repinfo{"$dev:$ino"}) {
		print "$P: DEL '$file'	-> '$repfile'\n";
		&Backup($file);
		++$deletes;
		if ($limit && ($deletes >= $limit)) {
			print STDERR "$P: Deletes reached limit of $limit; exiting.\n";
			exit 0;
		}
	}
}

print "$P: $deletes files deleted.\n" if $V>0;
print "$P: Exit with status $exitstat.\n" if $V>1;
exit $exitstat;

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
