#!/bin/perl

# cript.pl - Ferramenta para auxiliar a criacao de criptogramas 
# by Ivan da Silva Sendin (isendin@geocities.com)
# Licensa de uso: GPL (veja em www.gnu.org)
# uso:
# cat texto | cript.pl > saida.tex
# latex saida.tex
#



use strict;

my $prod = 1; # flag para devel X prod

my $espaco = "\\hspace{.3in}";

my $newline ="\\\\\n";

my $cifras =5; # numero de cifras por linha

my $prefixo_latex = "\\documentclass{article}\n\\begin{document}\n";
my $sufixo_latex = "\\end{document}";

my @greek = ("\\alpha","\\beta","\\gamma","\\delta","\\lambda","\\pi","\\phi","\\psi");

my @binary = ("\\ast","\\star","\\circ","\\bigtriangleup","\\bigtriangledown",
              "\\oplus","\\otimes","\\dagger","\\ddagger");

my @relation = ("\\in","\\gg","\\bowtie");

my @misc = ("\\surd","\\forall","\\exists","\\infty","\\clubsuit","\\diamondsuit",
	    "\\heartsuit","\\spadesuit");


my @alphabet = (@greek,@binary,@relation,@misc);

my %used; #mantem as letras usadas e o seu codigo

my $plaintext; 


if ($prod) {
  while (<STDIN>) {
    $plaintext = $plaintext . $_;
  } 
}

$plaintext = "tudo posso naquele que me fortalece" unless $prod;

$plaintext = uc $plaintext;

my $ciphertext = $plaintext;

#vamos coletar as letras...
srand;
foreach my $i ('A'..'Z') {
  if ( $plaintext =~ /$i/) {
    my $index = int(rand($#alphabet));
    $used{$i} = $alphabet[$index];
    @alphabet = (@alphabet[0..$index-1] , @alphabet[$index+1..$#alphabet]);
  }
}

printf $prefixo_latex;

my $counter=0;

foreach my $k (sort keys %used) {
  printf "$k \$$used{$k} \$ $espaco \n";
  $ciphertext =~ s/$k/$used{$k}/g;
  $counter++;
  if ($counter % $cifras == 0) {
    printf "$newline\n"
  }
}

$ciphertext =~ s/\s/$espaco/g;

printf "\n\$$ciphertext\$\n\n";


printf $sufixo_latex;
