#!/bin/bash

# NAME
#    ReplyTo
# DESCRIPTION
#    See 'skim replyto'. Don't run this script directly.
# COPYRIGHT
#    Skim - Off-line news reading package optimized for slow lines.
#    Copyright (C) 1995  Rene W.J. Pijlman
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program; if not, write to the Free Software
#    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
# VERSION
#    Skim version 0.6.
#    /home/rene/sys/CVS_MasterSourceRepository/skim/ReplyTo,v 1.14 1995/07/15 15:41:02 rene Exp

if [ "$IAmSkim" != "1" ]
then
    echo "You appear to be running $0 directly. Use 'skim replyto' instead." 1>&2
    exit 1
fi

if [ "$SKIMFROM" = "" ]
then
    echo "Environment variable SKIMFROM is not set" 1>&2
    exit 1
fi

for Article in $*
do
    Reply=${SKIMDIR}/Post/${Article}.reply

    Header=${SkimTmp}/skimheader.$$
    Body=${SkimTmp}/skimbody.$$

    SplitArticle $SKIMDIR/Articles/$Article $Header $Body
    if [ $? -ne 0 ]
    then
        echo "SplitArticle failed" 1>&2
        exit 1
    fi

    if [ -f $Reply ]
    then
	echo "$Reply already exists." 1>&2
	exit 1
    else
	echo "From: $SKIMFROM" >>$Reply

	echo "Path: `hostname`" >>$Reply

	# Construct 'Newsgroups:' line according to section 2.2.3 of RFC-1036.
	if { grep -i '^Followup-To: ' $Header >/dev/null }
	then
	    grep -i '^Followup-To: ' $Header | \
	        sed 's/[^:]*: /Newsgroups: /' >>$Reply
	elif { grep -i '^Newsgroups: ' $Header >/dev/null }
	then
	    grep -i '^Newsgroups: ' $Header >>$Reply
	else
	    echo -e "\
Warning: article '$Article' does not contain the mandatory 'Newsgroups:'\n\
or the optional 'Followup-To:' line. Are you sure this is a news article?\n\
You must manually add a 'Newsgroups:' line to the reply before posting it.\n\
" 1>&2
	fi

	# Construct 'Subject:' line according to section 2.1.4 of RFC-1036.
	if { grep -i '^Subject: ' $Header >/dev/null }
	then
	    grep -i '^Subject: ' $Header | sed 's/[Rr][Ee]: //g' |
	        sed 's/:/: Re:/' >>$Reply
	else
	    echo -e "\
Warning: article '$Article' does not contain the mandatory 'Subject:' line.\n\
Are you sure this is a news article? You must manually add a 'Subject:' line\n\
to the reply before posting it.\n\
" 1>&2
	fi

	if [ "$SKIMREPLYTO" != "" ]
	then
	    echo "Reply-To: $SKIMREPLYTO" >>$Reply
	fi

	# Construct 'References:' line according to section 2.2.5 of RFC-1036.
	if { grep -i '^References: ' $Header >/dev/null }
	then
	    if { grep -i '^Message-ID: ' $Header >/dev/null }
	    then
	        echo "References:" \
	          "`grep -i '^Message-ID: ' $Header | sed 's/[^:]*: //'`" \
	          "`grep -i '^References: ' $Header | sed 's/[^:]*: //'`" \
	          >>$Reply
	    else
	        echo -e "\
Warning: article '$Article' does not contain the mandatory 'Message-ID:' line.\n\
Are you sure this is a news article? You must manually add a 'References:'\n\
line to the reply before posting it.\n\
" 1>&2
	    fi
	else 
	    if { grep -i '^Message-ID: ' $Header >/dev/null }
	    then
	        echo "References:" \
	          "`grep -i '^Message-ID: ' $Header | sed 's/[^:]*: //'`" \
	          >>$Reply
	    else
	        echo -e "\
Warning: article '$Article' does not contain the mandatory 'Message-ID:' line.\n\
Are you sure this is a news article? You must manually add a 'References:'\n\
line to the reply before posting it.\n\
" 1>&2
	    fi
	fi

	if [ "$SKIMORGANIZATION" != "" ]
	then
	    echo "Organization: $SKIMORGANIZATION" >>$Reply
	fi

	echo "X-Newsreader: skim version 0.6" >>$Reply

	# Add an empty line to separate header and body in the reply.
	echo >>$Reply

	if { grep -i '^From: ' $Header >/dev/null }
	then
	    echo "`grep -i '^From: ' $Header | sed 's/[^:]*: //'` wrote:" >>$Reply
	fi

	# Include the body, indented with ">".
	sed 's/^/>/' <$Body >>$Reply

	echo >>$Reply

	if [ -f ~/.signature ]
	then
	    echo "-- " >>$Reply
	    cat ~/.signature >>$Reply
	fi
    fi

    # Remove temporary files.
    rm -f ${Header} ${Body}
done

