Pretty Printing C++ Archives from Emails
I'm just putting this here because I nearly managed to lose it. This is a part of a pretty unvarnished BASH script for a very specific purpose, taking an email file containing a ZIP of submitted C++ code from students. This script produces pretty printed PDFs of the source files named after each author to facilitate marking and annotation. It's not a thing of beauty. I think I'll probably write a new cleaner version in future.
#!/bin/bash
#
# A script to take C++ files in coursework and produce pretty printed PDF
# listings named with the author information.
#
# It takes a ZIP file of .cpp and .h files and produces a ZIP file of PDFs
#
# Requires
# enscript
# ps2pdf
# munpack
#
# Called for each file to be encoded
#
pretty_print_file()
{
# Extract the Author JavaDoc information
author=
1 | sed -n -e 's/^.*@[Aa]uthor 
(cat
/\1/gp')
# How many lines did we get back?
lines=
author_snip" | wc -l)
# If we got no author info
if [
author_snip" ]
then
author="no-author"
author_snip="no-author"
fi
# if we got too many
if [
1, Author
author_snip)"
output=
1
output+=".pdf"
echo "Encoding
1 | ps2pdf - parsed-output/
1 ]
then
echo "Usage: unpack_coursework <email_file>"
exit
fi
# Make a temporary directory and copy the email file into it.
echo "Creating temporary directory..."
dir=`mktemp -d`
echo
1
dir
# Unpack the email
echo "Unpacking email..."
munpack
f *.cpp
unzip -Cj
f
done
# And the same for source files
echo "Parse .cpp files..."
for f in *.cpp
do
pretty_print_file
dir/parsed-output
zip parsed-output *.pdf
cd ..
# Back to the directory we started in.
popd
# Copy the parsed ZIP to the current directory for inspection and marking
cp
dir
rm -rf $dir

