#!/usr/local/bin/perl -w

# title: morseDecode.pl
# written by: Marty J Miller
# purpose: Morse code redivision -- find latent messages 
#
# usage: morseDecode.pl 

if (@ARGV != 1){    # make sure there only one argument
    print "usage: morseDecode \n";
    exit 1;
}

$linenum=1;  # used later
print "creating output.txt...";
open(OUT, ">output.txt");  # open file for writing output

if ( open(IN, $ARGV[0]) ){ # open file for reading input
    print OUT "\n\n                 Original Plaintext\n";
    print OUT "____________________________________________________\n\n";

    while($line = ){
        print OUT "Line $linenum:\n";
	chomp($line);
	@temp = split / /, $line;
        print OUT "$line\n";
	for($i=0;  $i < @temp; $i++){
	    $b= bin2text($temp[$i]);
	    print OUT "$b";
	}
	print OUT "\n";
        $linenum++;
    }
    close(IN);
    
}
else {
    print "could not open file: $ARGV[0]\n";
    exit 1;
}

open( IN,$ARGV[0]);
print OUT "\n";
print OUT "            Possible Hidden Messages\n";
$linenum=1;
while ( $line =  ){
    chomp($line);
    print OUT "____________________________________________________\n\n";
    print OUT "Line $linenum:\n";

    $theLine = shiftLeft($line);
    print OUT "$theLine (shift left)\n";
    @temp = split / /, $theLine;
    for($i=0;  $i < @temp; $i++){
	$b= bin2text($temp[$i]);
	print OUT "$b";
    }
    print OUT "\n";

    $theLine = shiftRight($line);
    print OUT "$theLine (shift right)\n";
    @temp = split / /, $theLine;
    for($i=0;  $i < @temp; $i++){
	$b= bin2text($temp[$i]);
	print OUT "$b";
    }
    print OUT "\n";
    
    $linenum++;
}
close(OUT);
close(IN);
print "done\n";

# sub-routine "shiftLeft"
# takes in an array of binary strings.  Takes each string
# and sends the left most bit to the string on its left
# prints to filehandle "OUT" and assumes it has been opened for writing
sub shiftLeft {
    $str = shift;
    @temp = split / /, $str;
    $i = 0;
 
    for($t=0; $t < @temp; $t++){
	$myArray[$i] = " ";
        $i++;  
        $myArray[$i] = $temp[$t];
        $i++;
    }

    for($ti = @myArray - 1; $ti > 0; $ti=$ti-2){
        $temp = $myArray[$ti]; 
    	$sub1 = substr $temp,0,1;
	$sub2 = substr $temp,1,(length($temp)-1);  

	$myArray[$ti]   = $sub2;
	$myArray[$ti-1] = $sub1;
    }
 
    $String = "$myArray[0]";
    for($ti = 1; $ti < @myArray-2; $ti=$ti+2){
        $temp = "$myArray[$ti]" . "$myArray[$ti+1]";
        $String = "$String" . " " . "$temp"; 
    }
    $String = "$String". " ". "$myArray[@myArray-1]";

    return $String;
}


# sub-routine "shiftRight"
# takes in an array of binary strings.  Takes each string
# and sends the right most bit to the string on its right
# prints to filehandle "OUT" and assumes it has been opened for writing
sub shiftRight {
    $str = shift;
    @temp = split / /, $str;
    $i = 0;
 
    for($t=0; $t < @temp; $t++){
        $myArray[$i] = $temp[$t];
        $i++;
	$myArray[$i] = " ";
        $i++;  
    }

    for($ti = 0; $ti < @myArray - 1; $ti=$ti+2){
        $temp = $myArray[$ti]; 
    	$sub = chop($temp);  
	$myArray[$ti]   = $temp;
	$myArray[$ti+1] = $sub;
    }
 
    

    $String = "$myArray[0]";

    for($ti = 1; $ti < @myArray-2; $ti=$ti+2){
        $temp = "$myArray[$ti]" . "$myArray[$ti+1]";
        $String = "$String" . " " . "$temp"; 
    }

    $String = "$String". " ". "$myArray[@myArray-1]";

    $String =~ s/^\s//; 
    return $String;
}


# sub-routine "bin2text"
# purpose: to translate binary morse code to English
# one character at a time.  Returns the character.
# Returns "*" if it's invalid and " " if it's a space.
sub bin2text {
    $str = shift;
    if ($str eq "01"){
	$text = "A";
    } 
    elsif ($str eq "1000"){
	$text = "B";
    }
    elsif ($str eq "1010"){
	$text = "C";
    }
    elsif ($str eq "100"){
	$text = "D";
    }
    elsif ($str eq "0"){
	$text = "E";
    }
    elsif ($str eq "0010"){
	$text = "F";
    }
    elsif ($str eq "110"){
	$text = "G";
    }
    elsif ($str eq "0000"){
	$text = "H";
    }
    elsif ($str eq "00"){
	$text = "I";
    }
    elsif ($str eq "0111"){
	$text = "J";
    }
    elsif ($str eq "101"){
	$text = "K";
    }
    elsif ($str eq "0100"){
	$text = "L";
    }
    elsif ($str eq "11"){
	$text = "M";
    }
    elsif ($str eq "10"){
	$text = "N";
    }
    elsif ($str eq "111"){
	$text = "O";
    }
    elsif ($str eq "0110"){
	$text = "P";
    }
    elsif ($str eq "1101"){
	$text = "Q";
    }
    elsif ($str eq "010"){
	$text = "R";
    }
    elsif ($str eq "000"){
	$text = "S";
    }
    elsif ($str eq "1"){
	$text = "T";
    }
    elsif ($str eq "001"){
	$text = "U";
    }
    elsif ($str eq "0001"){
	$text = "V";
    }
    elsif ($str eq "011"){
	$text = "W";
    }
    elsif ($str eq "1001"){
	$text = "X";
    }
    elsif ($str eq "1011"){
	$text = "Y";
    }
    elsif ($str eq "1100"){
	$text = "Z";
    }
    elsif ($str eq "01111"){
	$text = "1";
    }
    elsif ($str eq "00111"){
	$text = "2";
    }
    elsif ($str eq "00011"){
	$text = "3";
    }
    elsif ($str eq "00001"){
	$text = "4";
    }
    elsif ($str eq "00000"){
	$text = "5";
    }
    elsif ($str eq "10000"){
	$text = "6";
    }
    elsif ($str eq "11000"){
	$text = "7";
    }
    elsif ($str eq "11100"){
	$text = "8";
    }
    elsif ($str eq "11110"){
	$text = "9";
    }
    elsif ($str eq "11111"){
	$text = "0";
    }
    else {
	$text = " ";
    }
    return $text;
}