#!/usr/bin/perl -w
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
#NAME
#  Cleanup -
#
#SYNOPSIS
#  Cleanup [file]..
#
#REQUIRES
	$HOME = $ENV{HOME} || '.';
	push @INC, "$HOME/sh";
	require "Backup.pm";	# We usually rename to backup rather than delete
	require "namesubs.pm";	# Filename-munging routines shared with others
#
#DESCRIPTION
#  This is a program to  run  around  looking  for  files  that  fit  certain
#  descriptions,  and  (usually)  delete  them.   Mostly  this  is  a sort of
#  prototype program, and I change it nearly  every  time  I  use  it.   This
#  explains  why  its  exact behavior isn't described here.  If you copy this
#  program, you should rewrite at least the pattern  matching  stuff  to  fit
#  your current needs.
#
#  The basic structure of this program walks the trees of  directories  named
#  on the command line, and does some "cleanup" operation on any files found.
#  What is done depends on a file's name and  possibly  the  names  of  other
#  files nearby.  You can also put file pathnames on the command line, though
#  this would typically be done via a "find ... | xargs Cleanup" command.
#
#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"} || 1;	# Verbose level.

for $arg (@ARGV) {
	print "$P: ARG \"$arg\"\n" if $V>3;
	if (-f $arg) {
		&onefile($arg);
	} elsif (-d $arg) {
		&onedir($arg);
	} else {
	}
}

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

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

sub onedir {my $F='onedir';
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
	local($dir) = @_;
	local($files,$x);
	$files = 0;
	print "$P: DIR: '$dir'\n" if $V>1;
	if (!opendir(DIR,$dir)) {
		print STDERR "$P: Can't read directory \"$dir\" [$!]\n";
		return $files;
	}
	local(@files) = readdir(DIR);
	closedir DIR;
	foreach $x (sort @files) {
		print STDERR "$P: nam \"$x\"\n" if $V>1;
		++$files;
		next if $x eq '.' || $x eq '..';
		if (-f "$dir/$x") {
			&onefile("$dir/$x");
		} elsif (-d "$dir/$x") {
			&onedir("$dir/$x");
		} else {
			print "$P: --- \"$x\"ignored.\n" if $V>1;
		}
	}
	return $files;
}

sub onefile {my $F='onefile';
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
	local($fil) = @_;
	local($dir,$err,$ndx,$new,$ttl,$TTL);
	print "$P: FILE '$fil'\n" if $V>1;
	if ($fil =~ /-$/) {
		print "$P: --- \"$x\"ignored.\n" if $V>1;
		return 0;
	}
	if (($dir,$ndx,$ttl) = ($fil =~ m"^(.+)/(\d+):(.+)$")) {
		print "$F: Matched file \"$fil\"\n" if $V>1;
		$TTL = &Cname($ttl);
		$new = "$dir/%$ndx:$TTL";
		print "$F: Rename \"$new\"\n" if $V>1;
		if (-f $new) {
			print "$F: Target file \"$new\" already exists.\n" if $V>1;
			# We leave the target file alone, and rename $file to backup.
		} elsif (link($fil,$new)) {
			print "$F: Renamed.\n" if $V>1;
			# The file has been renamed to the current convention.
		} else {
			print "$F: Rename '$fil' -> '$new' failed [$!].\n" if $V>0;
			return 0;
		}
		if ($err = &Backup($fil)) {	# For now we rename the file to backup
			print STDERR "$F: Backup failed [$err]\n" if $V>0;
		} else {
			print "$F: \"$fil\"\n" if $V>0;
		}
	} else {
		print "$F: Can't handle \"$fil\"\n" if $V>1;	# Nothing done to this file
		return 0;
	}
}
