#!/usr/pkg/bin/perl

=begin metadata

Name: pig
Description: reformat input as Pig Latin
Author: Jonathan Feinberg, jdf@pobox.com
License: perl

=end metadata

=cut


# pig - eformatray inputway asway Igpay Atinlay
use strict;

use File::Basename qw(basename);
use Getopt::Std qw(getopts);

use constant EX_SUCCESS => 0;
use constant EX_FAILURE => 1;

$|++;

our $VERSION = '1.0';
my $Program = basename($0);

sub VERSION_MESSAGE {
  warn "$Program version $VERSION\n";
  exit EX_SUCCESS;
}

sub help {
  warn "usage: $Program\n";
  exit EX_FAILURE;
}

# Dr. Bronner's top secret pig-latin algorithm!
# ALL-ONE! ALL-ONE! ALL-ONE!
sub igpay {
  local $_ = shift;
  my $ordway;
  my $initcaps = /^[A-Z]/;
  my $allcaps  = /^[A-Z]+$/;
  if (/^[aeiou]/i) {
     $ordway = $_ . ($allcaps ? 'WAY' : 'way');
  }
  else {
    /([^aieou]+)(.*)/i;
    $ordway = ($2 || '') . lcfirst $1 . 'ay';
    if ($allcaps) {
       $ordway = uc $ordway;
    } elsif ($initcaps) {
       $ordway = ucfirst $ordway;
    }
  }
  return $ordway;
}

getopts('') or help();
@ARGV = (); # stdin only

while (<>) {
  s/([A-Z]+)/igpay($1)/gexi;
  print;
}
exit EX_SUCCESS;

__END__

=head1 NAME

pig - eformatray inputway asway Igpay Atinlay

=head1 SYNOPSIS

B<pig>

=head1 DESCRIPTION

Ethay igpay utilityway eadsray ethay andardstay inputway andway iteswray
itway outway otay andardstay outputway inway Igpay Atinlay.

Usefulway orfay eneratinggay onthlymay eportsray.

=head1 BUGS

I<pig> ashay onay ownknay ugsbay.

=head1 AUTHOR

Ethay Erlpay implementationway ofway I<pig> asway ittenwray byay
Onathanjay Einbergfay, I<jdf@pobox.omcay>.

=head1 COPYRIGHT and LICENSE

Isthay ogrampray isway opyrightcay (c) Onathanjay Einbergfay 1999.

Isthay ogrampray isway eefray andway openway oftwaresay. Ouyay aymay
useway, odifymay, istributeday, andway ellsay isthay ogrampray (andway
anyway odifiedmay ariantsvay) inway anyway ayway ouyay ishway,
ovidedpray ouyay oday otnay estrictray othersway omfray oingday ethay
amesay.

