#!/usr/bin/perl
# EmmQueue - Easy Mnemonics for Mercurial Queues

$_ = shift @ARGV;
$args = join ' ', @ARGV;

if (!$_ || /^help$/i) {
  print <<EOF
  Welcome to EmmQueue -- Easy Mnemonics for Mercurial Queues
  This program is dead simple, it just translates from 'mq'
  syntax to 'hg' syntax.  The following commands are available:

    mq init == hg qinit -c
      Initialize patch repository.
      (Type this before beginning to use Mercurial Queues.)

    mq update == hg qpop -a; hg pull -u
      Unapply all patches and update the tree.

Patch Manipulation Commands

    mq create PATCHNAME == hg qnew -f PATCHNAME
      Fold unsaved changes into a new mq patch called PATCHNAME.

    mq save ["msg"] == hg qrefresh; hg qcommit -m "msg"
      Save your changes to the current patch and backup
      with message "msg" (defaults to "backup").

    mq unapply [-a] == mq pop [-a] == hg qpop [-a]
      Unapply the current patch [all patches].

    mq apply [-a|PATCHNAME] == hg qpush [-a|PATCHNAME]
      Apply [all patches | the named patch].

    mq absorb PATCHNAME == hg qfold PATCHNAME
      Apply named unapplied patch and fold into current patch.

    mq rename PATCHNAME == hg qrename PATCHNAME
      Renames current patch to PATCHNAME. (Specifying two PATCHNAMEs
      renames the first PATCHNAME to the second PATCHNAME.)

    mq message == mq msg == hg qrefresh -e
      Adds a checkin comment to the current patch. (Has side effect
      of absorbing local changes into patch.)

Queue Status Commands

    mq list == hg qseries
      Print list of all saved patches.

    mq ulist == hg qunapplied
      Print list of all unapplied patches.

    mq alist == hg qapplied
      Print list of all applied patches.

    mq log == hg log -r qbase:qtip
      Print log of applied queued patches.

Current Patch Status Commands

    mq current == hg qtop -s
      Print name of current patch.

    mq diff == hg qdiff
      Print diff of current changes.

    mq status == hg status --rev qparent
      Print current status.

    mq ldiff == mq new == hg diff
      Print diff of unsaved changes.

    mq lstatus == mq loose == hg status
      Print unsaved status.

  Arguments to mq are passed through to hg.
  Commands can be abbreviated, e.g. mq ap == mq apply.

EOF
}

# Returns true if $_ is at least 2 chars and matches start of 1st string
sub match { # match("command")
  my $string = shift @_;
  return 0 if (2 > length $_);
  return $string =~ /^$_/;
}

if (match('create')) {
  if ($args) {
    print `hg qnew -f $args`;
  }
  else {
    print "mq create PATCHNAME -- missing PATCHNAME\n";
  }
  exit;
}
if (match('save')) {
  $args = 'backup' if (!$args);
  print `hg qrefresh`;
  print `hg qcommit -m $args`;
  exit;
}
if (match('diff')) {
  print `hg qdiff $args`;
  exit;
}
if (match('ldiff') || match('new')) {
  print `hg diff $args`;
  exit;
}
if (match('lstatus') || match('loose')) {
  print `hg status $args`;
  exit;
}
if (match('status')) {
  print `hg status --rev qparent $args`;
  exit;
}
if (match('log')) {
  print `hg log -r qbase:qtip $args`;
  exit;
}
if (match('list')) {
  print `hg qseries $args`;
  exit;
}
if (match('alist')) {
  print `hg qapplied $args`;
  exit;
}
if (match('ulist')) {
  print `hg qunapplied $args`;
  exit;
}
if (match('apply')) {
  print `hg qpush $args`;
  exit;
}
if (match('unapply')) {
  print `hg qpop $args`;
  exit;
}
if (match('current')) {
  print `hg qtop -s $args`;
  exit;
}
if (match('update')) {
  $_ = `hg qpop -a 2>&1`;
  print;
  print `hg pull -u` if (/queue now empty/ || /no patches applied/);
  exit;
}
if (match('finish')) {
  $args = 'qbase' if (!$args);
  print `hg qfinish $args`;
  exit;
}
if (match('absorb')) {
  print `hg qfold $args`;
  exit;
}
if (match('message') || match('msg')) {
  exec 'hg qrefresh -e';
  exit;
}
if (match('init')) {
  print `hg qinit -c`;
  exit;
}

print "Unknown command. Type mq help for more info.\n";

