#!/usr/bin/perl

# Copyright (C) 2010 Thorsten Kukuk
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# in Version 2 as published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA  02110-1301, USA.

=head1 NAME

    rot13 - a simple substitution cipher

=head1 SYNOPSIS

    rot13 [options] Text

=head1 DESCRIPTION

    ROT13 ("rotate by 13 places", sometimes hyphenated ROT-13) is a
    simple substitution cipher used in online forums as a means of
    hiding spoilers, punchlines, puzzle solutions, and offensive materials
    from the casual glance. ROT13 has been described as the "Usenet
    equivalent of a magazine printing the answer to a quiz upside down".
    ROT13 is an example of the Caesar cipher, developed in ancient Rome.

    ROT13 is its own inverse; that is, to undo ROT13, the same algorithm
    is applied, so the same action can be used for encoding and decoding.
    The algorithm provides no cryptographic security, and is often cited
    as a canonical example of weak encryption. ROT13 has inspired a variety
    of letter and word games on-line, and is frequently mentioned in
    newsgroup conversations.

=head1 OPTIONS

    -k|--key <key>      Rotate characters by <key>, rather than 13
    --version           Print version number and exit
    -h|-?|--help	Help

=cut

use strict;
use warnings;
use Pod::Usage;
use Crypt::Rot13;

#
# process command line arguments
#
use Getopt::Long;
my $key = 13;
my $help = 0;
my $man = 0;
my $version = 0;
my $usage = 0;

GetOptions('k|key=i' => \$key,
	   'version' => \$version,
	   'man' => \$man,
	   'u|usage' => \$usage,
	   'help|h|?' => \$help) or pod2usage(2);
pod2usage(0) if $help;
pod2usage(-exitstatus => 0, -verbose => 2) if $man;
pod2usage(-exitstatus => 0, -verbose => 0) if $usage;

if ($version) {
  print "rot13 (geo-tools) 1.23\n";
  exit;
}

my $crypt = new Crypt::Rot13;

if ($#ARGV >= 0) {
  foreach (@ARGV) {
    $crypt->charge ($_);
    print $crypt->rot13 ($key), " ";
  }
  print "\n";
} elsif (($#ARGV < 0) ||($#ARGV == 0 && $ARGV[0] eq "-")) {
  # read from tty
  while (<>) {
    $crypt->charge ($_);
    print $crypt->rot13 ($key), " ";
  }
  print "\n";
}

exit;
