#!/usr/bin/perl
# Filename	: renamesequence
# Author	: Vasanth Philomin
# Date		: Thu Oct 19 16:31:48 EDT 1995
# Function	: Renames files starting from the first file to the last file
#		  last file specified according to the given increment or
#		  decrement. Prompting is turned on by default, but if user
#		  wishes to have no prompting when existing files are being
#		  overwritten, he can do so by giving the -noprompt option.
# Invocation	: "renamesequence ./im5.pgm ./im10.pgm +3"
#		  "renamesequence images/5.pgm images/10.pgm -2 -noprompt"
# Copyright (C) Vasanth Philomin (vasi@cs.umd.edu)
# This program can be freely distributed as long as the comments above stay in
# the file. It is provided without ANY WARRANTY obviously.
$0 =~ s!.*/!!;

if(@ARGV < 3)	{
    die "Usage: $0 firstfile lastfile inc[dec] [-noprompt]\n";
}
# Get the arguments
$first_file = shift;
$last_file = shift;
$inc = shift;
$prompt = 1;	# Default: prompting on
if(@ARGV == 1 && $ARGV[$#ARGV] eq '-noprompt')	{
    # No prompting requested by user
    print "****Prompting turned off****\n";
    $prompt = 0;
}

# Check if we really need to move stuff around
if($inc == 0)	{	# Not an error
    print "Nothing to do - Exiting ...\n";
    exit(0);
}

# Extract directory first
($dir, $first_file) = $first_file =~ /^(.*\/)(.*)$/;
($junk1, $last_file) = $last_file =~ /^(.*\/)(.*)$/;
unless(-d $dir)	{
    die "$0: $dir - No such directory\n";
}
unless($dir eq $junk1)	{
    die "$0: Files specified must be in same directory\n";
}
unless(-w $dir)	{
    die "$0: Directory $dir is write protected\n";
}
unless($dir)	{	# $dir is null indicating current directory
    die "$0: Need './' in front of filenames to indicate current directory\n";
}

# Check if first and last files exist and are writable
unless(-e $dir.$first_file && -e $dir.$last_file)	{
    die "$0: File(s) specified dont exist\n";
}
unless(-w $dir.$first_file && -w $dir.$last_file)	{
    die "$0: File(s) - no write permission\n";
}

# Split filename into prefix, frame number and suffix
($prefix, $start, $suffix) = $first_file =~ /^([^0-9]*)(\d+)(\..*)$/;
($junk1, $end, $junk2) = $last_file =~ /^([^0-9]*)(\d+)(\..*)$/;

# Get the number of digits in the number
$len = length $start;

unless(($start == 0 || $start) && $end)	{
    die "$0: Filenames not ok!\n";
}

# Check if the prefixes and suffixes match
if($prefix ne $junk1 || $suffix ne $junk2)	{
    die "$0: Moron: First & Last filenames should have same ",
			"prefix and suffix!\n";
}

# Check if end >= start
if($end < $start)	{
    die "$0: End < start\n";
}

# Check if start number gets screwed up after decrement
if($start + $inc < 0)	{	# $inc < 0 - only then this can happen
    die "$0: Decrementing start filenum will yield negative file number!\n";
}

# Whew, done with all the error checking
# So go ahead and move the stuff
if($inc > 0)	{	# Incrementing - so traverse from end to start
    $temp = $start;
    $start = $end;
    $end = $temp;	# Swap start and end
}

$temp = $start;	# Iterating variable
# Checking condition based on whether we increment or decrement
$cond = ($inc > 0) ? '($temp >= $end)' : '($temp <= $end)';
# Start main loop
while(eval($cond))	{
    $new = $temp + $inc;
    $oldfile = $dir.$prefix.sprintf("%.${len}d", $temp).$suffix;
    $newfile = $dir.$prefix.sprintf("%.${len}d", $new).$suffix;
    ($inc > 0) ? $temp-- : $temp++;	# Get next file for next iteration
    unless(-f $oldfile)	{
	print "Skipping $oldfile - doesnt exist\n";
	next;
    }
    if($prompt && -f $newfile)	{
	print "$newfile exists: Overwrite?? ";
	$ans = <STDIN>;
	next if $ans =~ /^[nN]$/;
    }
    print "Renaming $oldfile as $newfile\n";
    rename($oldfile, $newfile);
}	# End while
