#!/usr/bin/env perl

# Perform multiple moves/copies.

# Usage:    multi mv 's/$/.old/' *.c
#           multi cp 's:/tmp/(.*):$1:' *
#           multi - svn mv - 's/foo/bar/' *foo*
#
# Options:  -n    don't actually do anything, just print what it would do
#           -r    reverse the arguments to each command
#           -q    don't print the commands as they are executed

# Note that all variables in this script begin with a double
# underscore. This is because the <action> parameter is a piece of
# user-supplied Perl, which might perfectly legitimately want to
# define and use its own variables in the course of doing a complex
# transformation on file names. Thus, I arrange that all _my_
# variables stay as far out of its likely namespace as they can.

$usage =
  "usage: multi [flags] <cmd> <action> <files>\n" .
  "  e.g. multi mv 'tr/A-Z/a-z/' *\n" .
  "   or: multi [flags] - <multiple-word-cmd> - <action> <files>\n" .
  "  e.g. multi - svn mv - 'tr/A-Z/a-z/' *\n" .
  "where: -n                 print commands, but do not execute\n" .
  "where: -q                 execute commands, but do not print\n" .
  "where: -r                 reverse order of filenames passed to commands\n" .
  " also: multi --version    report version number\n" .
  "       multi --help       display this help text\n" .
  "       multi --licence    display (MIT) licence text\n";

$licence =
  "multi is copyright 1999-2004 Simon Tatham.\n" .
  "\n" .
  "Permission is hereby granted, free of charge, to any person\n" .
  "obtaining a copy of this software and associated documentation files\n" .
  "(the \"Software\"), to deal in the Software without restriction,\n" .
  "including without limitation the rights to use, copy, modify, merge,\n" .
  "publish, distribute, sublicense, and/or sell copies of the Software,\n" .
  "and to permit persons to whom the Software is furnished to do so,\n" .
  "subject to the following conditions:\n" .
  "\n" .
  "The above copyright notice and this permission notice shall be\n" .
  "included in all copies or substantial portions of the Software.\n" .
  "\n" .
  "THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND,\n" .
  "EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF\n" .
  "MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND\n" .
  "NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS\n" .
  "BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN\n" .
  "ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\n" .
  "CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n" .
  "SOFTWARE.\n";

$__quiet = $__donothing = $__reverse = 0;

while ($ARGV[0] =~ /^-(.+)$/) {
  shift @ARGV;
  $__quiet = 1, next if $1 eq "q";
  $__quiet = 0, $__donothing = 1, next if $1 eq "n";
  $__reverse = 1, next if $1 eq "r";
  if ($1 eq "-help") {
    print STDERR $usage;
    exit 0;
  } elsif ($1 eq "-version") {
    print "multi, version 20260326.66a3363\n";
    exit 0;
  } elsif ($1 eq "-licence" or $1 eq "-license") {
    print $licence;
    exit 0;
  }
}

die "multi: not enough arguments\n" if $#ARGV < 2;

@__cmd = ();
if ($ARGV[0] eq "-") {
    shift @ARGV;
    while (defined $ARGV[0] and $ARGV[0] ne "-") {
	push @__cmd, shift @ARGV;
    }
    if (!defined $ARGV[0]) {
	die "multi: no terminating - in multiple-word command mode\n";
    }
    shift @ARGV; # eat trailing -
} else {
    $__cmd[0] = shift @ARGV;
}
$__action = shift @ARGV;

while (@ARGV) {
  $_ = $__origfile = shift @ARGV;
  eval $__action;
  $__newfile = $_;
  ($__origfile, $__newfile) = ($__newfile, $__origfile) if $__reverse;
  &pcmd(@__cmd, $__origfile, $__newfile) if !$__quiet;
  &system(@__cmd, $__origfile, $__newfile) if !$__donothing;
}

sub pcmd {
  my (@words) = @_;
  printf "%s\n", join " ", map { &fmt($_) } @words;
}

sub fmt {
  local ($_) = @_;
  if (/[ !"#$&'()*;<>?\\`|~]/) {
    s/'/'\\''/g;
    "'$_'";
  } else {
    $_;
  }
}

sub system {
  my $pid = fork;
  die "multi: fork: $!\n" if $pid < 0;
  if ($pid == 0) {
    exec @_;
    die "multi: exec: $!\n";
  }
  waitpid $pid, 0;
  return $?;
}
