#!/bin/bash

# CptS 427/527 Demo Script for Program #1 -- Caesar Cipher
# Revised:  2 November 2013, by AD McKinnon

echo "CptS 427/527 Program #1 Grading Script"

if [ $# -lt 1 ]
then
    echo "Usage:  demo.sh cpts427p1[LASTNAME]"
    exit
fi

###---------------------------
### make/compile the programs
###---------------------------

rm -Rf $1
mkdir $1
cd $1
cp ../demo.txt .
cp ../message.plaintext .
cp ../writing_sample.text .

tar -xzf ../$1.tar.gz
make

###-----------------------------------------------
### Begin grading with the provided demo.txt file
###-----------------------------------------------

echo " "
echo "****** Caesar Cipher ********************************************************"
./caesarCipher -i demo.txt -k 42 -o demo.cipher

echo " "
echo "****** TESTING Caesar Cipher output *****************************************"
echo "============================================================================="
diff -s demo.cipher ../demo.cipher | head
echo "============================================================================="
echo "*****************************************************************************"

echo " "
echo "****** Character Frequency Generation ***************************************"
./charFreqGen -i demo.txt -o demo.freq

echo " "
echo "****** Caesar Cipher Statistical Attack *************************************"
./caesarAttack -c demo.cipher -f demo.freq -p phi.txt -o demo.plain

echo " "
echo "****** TESTING Caesar Cipher Attack output **********************************"
echo "============================================================================="
diff -s demo.plain ../demo.txt | head
echo "============================================================================="
echo "*****************************************************************************"

###-------------------------------------------
### Continue grading using unknown test files
###-------------------------------------------

echo " "
echo "****** Encipher an (unknown) file *******************************************"
./caesarCipher -i message.plaintext -k 24 -o message.ciphertext

echo " "
echo "****** Character Frequency Generation (unknown sample) **********************"
./charFreqGen -i writing_sample.text -o char.probability -t 5

echo " "
echo "****** Launching the statistical attack (best key) **************************"
./caesarAttack -c message.ciphertext -f char.probability -p phi_values.txt -o message.decrypted -r 1

echo " "
echo "****** TESTING Caesar Cipher Attack output **********************************"
echo "============================================================================="
diff -s message.decrypted message.plaintext | head
echo "============================================================================="
echo "*****************************************************************************"

echo " "
echo "****** Launching the statistical attack (2nd best key) **********************"
./caesarAttack -c message.ciphertext -f char.probability -p phi_values.txt -o message.decrypted2 -r 2

echo " "
echo "--- Caesar Attack Comparison, with 2nd choice key ---"
echo "*****************************************************************************"
diff -s message.decrypted2 message.plaintext | head
echo "*****************************************************************************"

###----------------------------------------------
### All done grading, return to parent directory
###----------------------------------------------

cd ..
