# Copyright 1989, 1990 Michael DeCorte


cd ${archive_dir}

mailfile=$1

tmpfile=${TMPDIR}/$$.parse
outgoing=${TMPDIR}/$$.outgoing
file_valid=1
set --

cat > ${tmpfile}

to=`${awk} '/^TO:/ {print substr($0, length($1)+1)}' < ${tmpfile}`
size=`${awk} '/^SIZE/ {print $2}' < ${tmpfile}`
limit=`${awk} '/^LIMIT/ {print $2}' < ${tmpfile}`
language=`${awk} '/^LANGUAGE/ {print $2}' < ${tmpfile} | tr A-Z a-z `
length=`${awk} '/^LENGTH/ {print $2}' < ${tmpfile}`
pack=`${awk} '/^PACK/ {print $2}' < ${tmpfile} | tr A-Z a-z `
archiver=`${awk} '/^ARCHIVER/ {print $2}' < ${tmpfile} | tr A-Z a-z `
encoder=`${awk} '/^ENCODER/ {print $2}' < ${tmpfile} | tr A-Z a-z `
files=`${awk} '/^(SEND|ENCODEDSEND)/ { $1=""; print $0}' < ${tmpfile}`

# see if we should just junk it
grep "^ABORT$" < $tmpfile > /dev/null 2>&1
if [ $? = 0 ]
then
        exit
fi

# you should put in productions for all of the likely ways of spelling
# the specified language such as:
# eng = english
# spa = spanish = esp = espanol
# You get the idea.
# the default lanague is english
# you don't have to use 3 letter names just so long as the final
# is an extention for the stuff in docs_dir and is unique
#
case ${language} in
  eng* ) language=eng
  ;;
  * )    language=eng  # the default is english
  ;;
esac

# do all of the searches
sed -n 's/^SEARCH/search/p' < ${tmpfile} |
   ${SHELL} -f > ${work_dir}/$$.search 2>&1

if [ -s ${work_dir}/$$.search ]
then
        files="$files ${work_dir}/$$.search"
fi

# do all of the finds
for f in `sed -n 's/^FIND//p' < ${tmpfile} `
do
        find "$f" | sed -n "s@${archive_dir}/@@p" > ${work_dir}/$$.find  2>&1
done

if [ -s ${work_dir}/$$.find ]
then
        files="$files ${work_dir}/$$.find"
fi

# check to see if archiver, pack, encoder
if [ ! -s ${archiver_dir}/${archiver}.arc ]
then
  files="${docs_dir}/archiver_wrong.${language} ${docs_dir}/help.${language}"
  archiver=none
  pack=none
  encoder=none
  file_valid=0
fi
if [ ! -s ${pack_dir}/${pack}.pac ]
then
  files="${docs_dir}/packer_wrong.${language} ${docs_dir}/help.${language}"
  archiver=none
  pack=none
  encoder=none
  file_valid=0
fi
if [ ! -s ${encoder_dir}/${encoder}.enc ]
then
  files="${docs_dir}/encoder_wrong.${language} ${docs_dir}/help.${language}"
  archiver=none
  pack=none
  encoder=none
  file_valid=0
fi

#check to see if help was asked for
grep '^HELP' ${tmpfile}  > /dev/null 2>&1
if [ $? = 0 ]
then
  files="${files} ${docs_dir}/help.${language}"
fi


# check to see if the files exist

for f in $files
do
    if [ -f $f ]
    then
        echo $f >> ${TMPDIR}/$$.found
    elif echo $f | grep '/Index$' > /dev/null 2>&1 
    then
        if [ -d `echo $f | sed 's@/[^/]*$@@'` ]
        then
            mkindex.sh `echo $f | sed 's@/Index@@'` >> ${TMPDIR}/$$.Index
            echo ${TMPDIR}/$$.Index >> ${TMPDIR}/$$.found
        else
            echo $f >> ${TMPDIR}/$$.not_found
        fi
    else
        echo $f >> ${TMPDIR}/$$.not_found
    fi
done

if [ -s ${TMPDIR}/$$.not_found ]
then
  files="${TMPDIR}/$$.not_found ${docs_dir}/not_found.${language}"
  archiver=none
  pack=none
  encoder=none
  file_valid=0
elif [ -s ${TMPDIR}/$$.found ]
then
  files=`cat ${TMPDIR}/$$.found /dev/null | sort -u`
else
  files="${docs_dir}/empty_request.${language} ${docs_dir}/help.${language}"
  archiver=none
  pack=none
  encoder=none
  file_valid=0
fi

# you can't have a packer if you are using arc as your archiver
if [ ${archiver} = arc -o ${archiver} = zoo ]
then
        pack=none
fi

set -- ${files}

# don't let people ask for tons and tons of files
if [ $# -gt ${maxfiles} ]
then
  echo $* | long-lines.sh 0 > ${TMPDIR}/$$.to_many
  set -- ${docs_dir}/to_many.${language} ${TMPDIR}/$$.to_many
  archiver=none
  pack=none
  encoder=none
fi

# don't let people ask for HUGE files
if [ `file-sizes.sh $*` -gt ${limit} ]
then
  ls -l $*  > ${TMPDIR}/$$.to_big
  set -- ${docs_dir}/to_big.${language} ${TMPDIR}/$$.to_big
  archiver=none
  pack=none
  encoder=none
fi

# check to see if the file needs to be encoded

should_encode=0

if [ ${file_valid} = 1 ]
then
        grep '^ENCODEDSEND' ${tmpfile}  > /dev/null 2>&1

        if [ $? = 0 ]
        then
                should_encode=1
        fi

# check to see if any of the files are binary

        cat $* /dev/null | is_mailable ${LENGTH} > /dev/null 2>&1

        if [ $? != 0 ]
        then    
                should_encode=1
        fi
fi

# you can't use shar, dclshar, none, simple to pack binary files 
# so have to use tar

if [ ${should_encode} = 1 ]
then
  if [ ${archiver} = shar -o  ${archiver} = none  -o ${archiver} = simple  -o ${archiver} = dclshar ]
  then
    archiver=tar
  fi
fi

# treat 1 and n files requests differently.
if [ $# = 1 ]
then
        outgoing=$1
else
# this for loop is here only because sed can't take long lines grrrr
        f=""
        for x in $*
        do
                f="$f "`echo $x | sed "s@${archive_dir}/@@g"`
        done
        ${archiver_dir}/${archiver}.arc $f > ${outgoing}
fi

# check to see if the file needs to be packed

if [ ${pack} != "none" ]
then
        echo ${outgoing} | fgrep "${TMPDIR}" > /dev/null 2>&1
        if [ $? != 0 ]
        then
                cp ${outgoing} ${TMPDIR}/$$.outgoing
                outgoing=${TMPDIR}/$$.outgoing
        fi
        ${pack_dir}/${pack}.pac ${outgoing}
fi

# check to see if the file needs to be encoded.  Yes this has to be done
# twice because the file may have been compressed or the archiver
# may have made the files non-mailable.

cat ${outgoing} /dev/null | is_mailable ${LENGTH} > /dev/null 2>&1

if [ $? != 0 ]
then
        should_encode=1
fi

if [ ${should_encode} = 1 -a ${encoder} != none ]
then
        echo ${outgoing} | fgrep "${TMPDIR}" > /dev/null 2>&1
        if [ $? != 0 ]
        then
                cp ${outgoing} ${TMPDIR}/$$.outgoing
                outgoing=${TMPDIR}/$$.outgoing
        fi
        ${encoder_dir}/${encoder}.enc ${outgoing}
fi

# get the file names but cleaned up a bit
# this is only used for accounting and the subject line so if it is a little
# off its not big deal
for f in ${files}
do
        sending="${sending} "`echo ${f} | sed "s@${archive_dir}/@@"`
done

# do a little bit of book keeping
echo `basename ${mailfile}`,\
     `date | ${awk} '{printf("%4s %3s %2s %s\n", $6, $2, $3, $4);}'`,${to},`\
     ls -l ${outgoing} | ${awk} '{print $4}'`,$sending >> \
     ${accounting}/outgoing

# split file up if necessary

file-split.sh ${TMPDIR}/$$. ${size} ${outgoing}

# mail out file

n=0
total=`echo ${TMPDIR}/$$.*split | wc | ${awk} '{print $2}'`
for f in ${TMPDIR}/$$.*split
do
# the sleep is an attempt to not kill the mailer
        sleep ${total}
        n=`expr $n + 1`
        (
        echo "Return-path: ${server}"
        echo "From: ${server}"
# I only give the first 3 lines of the list of files being sent because
# some mailers seem to puke on really long header lines.  
        echo "Subject: ${n}/${total}" of ${sending} | long-lines.sh 3
        echo "To: ${to}"
        echo "Sender: ${server}"
        echo "Reply-To: ${server}"
        echo "In-Reply-To: " `basename ${mailfile}`
        echo "Comment: archiver:${archiver} pack:${pack} encoder:${encoder}"
        echo "Precedence: bulk"
        echo ""
        echo " -------------------- cut here --------------------"
        cat $f /dev/null
        echo " -------------------- cut here --------------------"
        echo .
        ) | /usr/lib/sendmail -ba -t
done

