#! /bin/sh
#
#     fixrtf
#     v1.002
#
#     John H. Yates
#     July 2008
#     January 2009 - added error exit if file sedscript does not exist
#
#
# This script patches Reunion rtf reports in order to use Mills
# style sources defined and used as described.
#
# The file "sedscript" must be in the same direcory as this script
# and contains the rewrite rules.
#
# this script is a Unix Bourne shell script (/bin/sh) that will
# run in a terminal window on MacOS X. If you haven't realized
# it, MacOS X is Unix at its lower level, and it is directly
# accessible in a terminal window.
#
# Be sure to make this script executable by typing in the terminal
# window: "chmod u+x fixrtf"
# and "chmod u+x sedscript" may not be necessary, but it can't hurt.
#
# The output file name is the same as the input file name but with MI
# (Mills Inspired) appended in capital letters. (The file extension
# remains .rtf).
#
# You can change the value of the parameter DEFAULTFNAME below to
# be the default file name to be processed. i.e. if you simply
# hit "enter" at the prompt.
#
DIR="${HOME}"
CURRENTDIR=`pwd`
REPORTDIR="${HOME}/Documents/Reunion Files/Reports"
SEDSCRIPTNAME="${CURRENTDIR}/sedscript"
DEFAULTFNAME="Mills_template_Sources"
#
echo "Your Reunion Report Default Directory is: ${REPORTDIR}"
#
if [ ! -f ${SEDSCRIPTNAME} ]
then
echo "ERROR: ${SEDSCRIPTNAME} does not exist."
exit 1
fi
#
CHOICES=`ls -c1 "${REPORTDIR}" | grep "\.rtf" | sed 's/.rtf//g'`
#
if [ "${CHOICES}" = "" ]
then
echo "There are no files to process."
exit 1
fi
#
echo "Would you like a list of available rtf files (this may be long) [y/n]: \c"
read FLAG
if [ "${FLAG}" = "y" -o "${FLAG}" = "Y" ]
then
echo "rtf file choices found in ${REPORTDIR}:"
echo " "
echo "${CHOICES}"
echo " "
fi
#
echo "Type the [case sensitive] rtf file name [without the .rtf] to process [${DEFAULTFNAME}]: \c"
read F
#
if [ "${F}" != "" ]
then
FNAME="${REPORTDIR}/${F}"
else
FNAME="${REPORTDIR}/${DEFAULTFNAME}"
fi
#
OUTFILE="${FNAME}_MI.rtf"
FNAME="${FNAME}.rtf"
#
# just in case someone modifys file names, this will prevent
# the input and output file names from being the same
# which would overwrite the input
#
if [ "${FNAME}" = "${OUTFILE}" ]
then
echo "Error: Input and Output File names are the same: ${FNAME}"
exit 1
fi
#
#echo " "
#echo "Input  File= ${FNAME}"
#echo "Output File= ${OUTFILE}"
echo " "
#
if [ ! -f "${FNAME}" ]
then
echo "${FNAME} does not exist."
exit 1
fi
#
if [ -f "${OUTFILE}" ]
then
/bin/rm "${OUTFILE}"
fi
#
sed -f ${SEDSCRIPTNAME} "${FNAME}" > "${OUTFILE}"
#
echo "${FNAME}"
echo "       has been rewritten to:"
echo "${OUTFILE}"
echo "       according to the sed rules in the file ${SEDSCRIPTNAME}."
#
#diff "${FNAME}" "${OUTFILE}"
#
