#! /bin/sh -x
#
# Shell script to find, sort and translate all .doc files in a given 
# directory into LaTeX sources.  Executing:
#
#	/bin/sh doc-to-tex <sed> <dir> <file>
#
# Will translate all .doc files in the directory <dir> into LaTeX source,
# using the sed script whose pathname is <sed>. The results of the
# translation are appended onto the end of <file>
#
# Modified 28.02.91 by JVT to take care of strange file names.
# Original code:
#
#   for file in `cd $1; ls -1 *.doc | sed 's/.doc$//g' | sort -f` 
#       do 
#         sed -f doc-to-tex.sed $1/$file.doc >> $2
#       done
#
# The problem with this occurred when finding either *.doc or hidden
# files.
#     1) Hidden files (e.g. ..doc):
#        These are not trapped by "ls -1 *.doc".  The command was
#        therefore changed to "ls -A1 | grep '\.doc".
#     2) *.doc:
#        This caused a pattern match to all files when encountered
#        inside "for file in `.......`".  We therefore find all
#        the files we're interested in, and put them in ".files".
#        While doing this, we put single quotes around the names
#        protect them from misinterpretation.  In the "for" loop,
#        we strip off these single quotes, and perform the "sed"
#        that we're really interested in.  These steps may seem
#        to be over the top.  But, they ensure that all the right
#        files are found, and using their names has no adverse
#        side effects.
#
# Modified 91.06.17 to take a pathname to the sed script as an argument.

(cd $2;ls -A1 | grep '\.doc' | grep -v '\.doc~' | sort -f | sed "s/.doc$/.doc'/g" | sed "s/^/'/g") > .files

for file in `cat .files | sed "s/^'//g" | sed "s/.doc'/.doc/g"`
     do 
       sed -f $1 $2/"$file" >> $3
     done
rm .files
