#!/bin/csh -f
#
# nextmail - send a nextmail attachment (any set of directories or files)
#
# Written by Zacharias J. Beckman, Dolphin Technologies Inc.  You may
# distribute this script as you see fit, as long as you don't take this
# comment out.  For an object which makes it possible to send NeXTMAIL from
# inside your programs, send mail to info@dolphin.com.  Copyright (C) 1994
# Dolphin Technologies Inc.  This script comes without warranty, and Dolphin
# Technologies assumes no liability for damages, direct or consequential,
# occuring as a result of using this script.
#
# Requirements:
#
# The C shell, tar, uuencode, and compress.  Sendmail must be available and
# live in /usr/lib/sendmail, but you can change this easily.  You must have
# write permission in /tmp, but this can also be changed easily.  See the very
# first 'set' commands for details.
#
# Warnings:
#
# Don't try to put a space in the subject or recipient arguments without
# enclosing them in quotes; the script expects the first argument to be the
# subject, the second argument to be the recipient(s), and the remaining
# arguments to be attachments.
#
# Example:
#
# nextmail "Pretty Pictures" zac@dolphin.com ~/Images/Pictures ~/NeXT.tiff

# set a few commonly used variables (if you have a variant UNIX that you want
# to use this on, it is likely that you will have to change one or both of the
# first two):

set sm = /usr/lib/sendmail
set tm = /tmp
set cm = $0
set cm = $cm:t

# offer help and verify that arguments are valid:

if ( $#argv < 3 ) then
    echo "$cm : $cm subject recipient[,recipient...] attachment [attachment...]"
    echo "    example:    $cm ProjectStuff zac,tammy *.[hm] PB.project"
    echo '    subject:    A non-broken or quoted string such as "Pretty pictures";\
                avoid any use of special characters such as exclamation points\
                or parenthesis as many shells will treat them strangely.\
    recipient:  List of recipients, either comma delimited or quoted and space\
                separated such as "zac, sherry, tammy".\
    attachment: One or more attachments, which can be directories or files, to\
                send in the NeXTMAIL (wildcards are ok).'

    exit 1
endif 

# obtain a temporary directory that is safe to work under and a destination
# filename for the file to send:

@ pn = $$
set of = $tm/.tar.$pn.No_Subject_.attach
set wd = $tm/.tar.$pn.attach
set to = "$argv[2]"
set sub = "$argv[1]"

while ( -e $of )
    @ pn++
    set of = $tm/.tar.$pn.No_Subject_.attach
end

while ( -e $wd )
    @ pn++
    set wd = $tm/.tar.$pn.attach
end

# set up the working directory where we create the attachment contents:

mkdirs $wd

if ( ! -d $wd ) then
    echo "$cm : could not create working directory; aborted"
    exit 1
endif

@ count = 0

# prepare the nextmail header index.rtf

echo "{\rtf0\ansi{\fonttbl\f1\fnil Times-Regular;}\
\margl120\
\margr120" > $wd/index.rtf

# append each file into the nextmail document

while ($#argv > 2)
    set if = $argv[3]:t
    
    if ( ! -e $argv[3] ) then
        echo "$cm : $argv[3] does not exist"
    else
        echo "$cm : creating NeXTMAIL attachment for $if"
        cp -pr $argv[3] $wd/$if
echo "{{\attachment${count} ${if}\
}\
\pard\tx560\tx1120\tx1680\tx2240\tx2800\tx3360\tx3920\tx4480\tx5040\tx5600\f1\b0\i0\ulnone\fs28\fc0\cf0 ${if}\\
" >> $wd/index.rtf
        @ count++
    endif

    shift
end

# close the nextmail attachment

echo "}" >> $wd/index.rtf

# create the NeXTMAIL compatible archive:

if ($count > 0) then
    cd $wd
    tar cvf - . | compress -c | uuencode $of:t > $of

    # then send it off using sendmail, creating the Next-Attachment line:
    
    set sz = `wc $of`
    set sz = $sz[3]

    echo "To: $to" >> prefix
    echo "Subject: $sub" >> prefix
    echo "Next-Attachment: $of:t, $sz, 1/1, 0, 0" >> prefix
    echo "" >> prefix

    cat prefix $of | $sm "$to"

    # print summary statistics:

    echo "$cm : sent NeXTMAIL attachments to $to ($sz bytes)"
else
    echo "$cm : no files sent; message aborted"
endif
    
# clean up

cd ..
rm -rf $wd
rm -rf $of
