#!/bin/ksh
#
# $Revision: 1.3 $ $Date: 2012-07-26 17:11:49-04 $
# $Source: /home/vogelke/bin/RCS/storemail,v $
# $Host: sys7.com $
# $UUID: 6578e13e-747e-39dd-9d07-eebd4c953a8c $
#
#<storemail: store mail messages wrapped, without quoted-printable.
# usage: storemail INPUT-FILE [MAILBOX]
# default output file is your main mailbox $MAIL.

PATH=/bin:/usr/bin:/usr/local/bin:/opt/sfw/bin:$HOME/bin
export PATH
tag=`basename $0`

die () {
    echo "$tag: $*" >& 2
    exit 1
}

case "$#" in
    1) file="$1"; out=$MAIL ;;
    2) file="$1"; out=$2 ;;
    *) die "usage: $tag input-file [mailbox]" ;;
esac

lck="$out.lock"
test -f "$file" || die "$file: not found"
lockfile -0 -r0 $lck || die unable to lock $out

qp < $file | formail -ds email-wrap >> $out
echo rm -f $lck
exit 0
