#!/usr/local/bin/perl5 -w

# $Id: range,v 1.3 2001/03/27 10:15:10 max Exp $

use strict;
use Getopt::Long;

my $start = undef;
my $end = undef;
my $stepsize = 1;
my $prefix = undef;
my $suffix = undef;
my $zeropad = undef;
my $strtmpl = "%s%d%s%s";
my $separator = " ";

GetOptions("start=i" => \$start,
	   "end=i" => \$end,
	   "prefix=s" => \$prefix, 
	   "suffix=s" => \$suffix,
	   "stepsize=i" => \$stepsize,
	   "zeropad=i" => \$zeropad,
	   "separator=s" => \$separator,
	   "help" => \&showhelp);

if ((defined $start) && (defined $end)) {
    # Everything is ok. User specified everything through option
} elsif ((!defined $start) && (!defined $end) && (scalar @ARGV >= 2)) {
    # Use up to the first four things in ARGV as start, end, prefix and
    # suffix  respectively.
    $start = $ARGV[0];
    $end = $ARGV[1];
    $prefix = $ARGV[2] if ((! defined $prefix) && (@ARGV >= 3));
    $suffix = $ARGV[3] if ((! defined $suffix) && (@ARGV >= 4));
    $zeropad = $ARGV[4] if ((! defined $zeropad) && (@ARGV >= 5));
} elsif ((!defined $start) && (!defined $end) && (scalar @ARGV == 0)) {
    print "Usage: range [options] <start> <end>\n";
    exit;
} else {
    print "You must specify both the start and the end of the range.\n";
    exit;
}

$prefix = "" unless defined $prefix;
$suffix = "" unless defined $suffix;

if ((defined $zeropad) && ($zeropad < 0)) {
    print "You cannot have negative padding!\n";
    exit;
}
if ((defined $stepsize) && ($stepsize <= 0)) {
    print "You cannot have a stepsize less than or equalt to zero!\n";
    exit;
}

if ((defined $zeropad) && ($zeropad != 0)) {
    $strtmpl = "%s%0" . $zeropad . "d%s%s";
}

my $i;
if ($start <= $end) {
    for ($i = $start; $i <= $end; $i += $stepsize) {
	printf($strtmpl, $prefix, $i, $suffix, $separator);
    }
} else {
    for ($i = $start; $i >= $end; $i -= $stepsize) {
	printf($strtmpl, $prefix, $i, $suffix, $separator);
    }
}
print "\n";	 

sub showhelp {
    print<<EOT;
NAME
  range - print a range of strings with running integer index

SYNOPSIS
  range [--help] [--start=I] [--end=I] [--stepsize=I] [--zeropad=I] 
        [--prefix=S] [--suffix=S] [--separator=S] [start] [end]

DESCRIPTION
  range prints a list of strings, separated by space by default. The
  strings have some integer specifying the range. The numbers of the
  strings can have leading zeros and, optionally, be prefixed or 
  suffixed by arbitrary strings.

OPTIONS

  --start=INTEGER
      Specifies the starting value. Can optionally be specified as the 
      first non-option argument on the command line.

  --end=INTEGER
      Specifies the end value. Can optionally be specified as the second
      non-option argument on the command line.

  --stepsize=INTEGER
      Specifies the step size. This value must be positive and larger than
      zero. If the range is to descend, it will be used as an decrement, 
      otherwise an increment.

  --zeropad=INTEGER
      Specifiy how many digits the number field should occupy. If the value
      is zero, no padding will be used, and the field only takes up as many
      characters as digits in the index. Note that this value can be less
      than the number of digits in the largest index. Can optionally be 
      specified as the fifth non-option argument on the command line.

  --prefix=STRING
      A string to prefix the resulting string with. Can optionally be 
      specified as the third non-option argument on the command line.

  --suffix=STRING
      A string to suffix the resulting string with. Can optionally be 
      specified as the fourth non-option argument on the command line

  --separator=STRING
      A string to be used as a separator between the diffrent strings.

SEE ALSO
  max homepage

BUGS
  lots of them

EOT
}