##
# Copyright (c) 2008 Health Market Science, Inc
##
package _TMP;
use strict;
use warnings;
our $CVS_ID = '$Id: emacs-utils.el,v 1.82 2008/07/11 13:35:40 kburton Exp $'; #'
our $VERSION = ( qw$Revision: 1.82 $ )[1];


sub run {
  my($self) = @_;

  my $rot       = shift @ARGV || die "You must supply a rotation amount.\n";
  my $inputFile = shift @ARGV || die "You must supply an input file.\n";
  unless (-e $inputFile) {
    die "Error, input file $inputFile does not exist.";
  }
  my $outputFile = shift @ARGV || die "You must supply an output file.\n";

  my @alpha   = ('a' .. 'z');
  my @rotated = ('a' .. 'z');

  for ( 1 .. $rot) {
    push @rotated, shift @rotated;
  }

  my %map;
  for (my $ii = 0; $ii < @alpha; ++$ii ) {
    my $ch       = $alpha[$ii];
    my $rot      = $rotated[$ii];
    $map{$ch}    = $rot;
    $map{uc $ch} = uc $rot;
  }

  my $data = $self->readFile($inputFile);
  my $fh = $self->openFile($outputFile,">");

  foreach my $ch (split //, $data) {
    print $fh ($map{$ch} || $ch);
  }
  close $fh;
}

sub openFile {
  my($self,$fname,$mode) = @_;
  $mode ||= '<';
  my $fh;
  unless (open $fh, $mode, $fname) {
    die "Error opening file:$fname mode:$mode : $!\n";
  }
  return $fh;
}

sub readFile {
  my($self,$file) = @_;

  local $/ = undef;
  my $fh = $self->openFile($file);
  my $data = <$fh>;
  return $data;
}


1;

_TMP->run;

