#!/bin/sh
# $Header: Pnews.SH,v 4.3.1.4 86/09/05 15:21:10 lwall Exp $
#
# $Log:	Pnews.SH,v $
# Revision 4.3.1.4  86/09/05  15:21:10  lwall
# Changes for new top-level newsgroup names.
# 
# Revision 4.3.1.3  85/08/01  14:24:06  lwall
# Added AUTHORCOPY.  Temp file is no longer in /tmp.  'e editor' added.
# 
# Revision 4.3.1.2  85/05/17  10:36:46  lwall
# Removed some extra backslashes.
# 
# Revision 4.3.1.1  85/05/10  11:30:21  lwall
# Branch for patches.
# 
# Revision 4.3  85/05/01  12:20:33  lwall
# Baseline for release with 4.3bsd.
# 
#
# syntax: Pnews -h headerfile			or
#	  Pnews -h headerfile oldarticle	or
#         Pnews newsgroup title			or just
#         Pnews

export PATH || (echo "OOPS, this isn't sh.  Desperation time.  I will feed myself to sh."; sh $0; kill $$)

# System dependencies

mailer="/usr/lib/sendmail"
# if you change this to something that does signatures, take out signature code

case undef in
define)
# your site name
sitename=``
case $sitename in
	*.*)
		;;
	*)
		sitename=${sitename}.iipo.gtegsc.com
		;;
esac
# where recordings, distributions and moderators are kept
lib=`/news/lib/filexp /news/lib`
# where important rn things are kept
rnlib=`/news/lib/filexp /news/lib`
;;
undef)
# your site name
sitename="wlonex.iipo.gtegsc.com"
# where recordings, distributions and moderators are kept
lib="/news/lib"
# where important rn things are kept
rnlib="/news/lib"
;;
esac

# your organization name
orgname="GTE Government Systems"
# what pager you use--if you have kernal paging use cat
pager="${PAGER-/usr/ucb/more}"
# how you derive full names, bsd, usg, or other
nametype="bsd"
# default editor
defeditor="/usr/ucb/vi"
# how not to echo with newline
n="-n"
c=""

# You should also look at the distribution warnings below marked !DIST!
# to make sure any distribution regions you are a member of are included.
# The following are some prototypical distribution groups.  If you do not
# use them all set the unused ones to a non-null string such as 'none'.
loc="wlv"
org="iipo"
city="wlv"
state="ca"
cntry="usa"
cont="na"

test=/bin/test
sed=/bin/sed
echo=/bin/echo
cat=/bin/cat
egrep=/usr/bin/egrep
grep=/usr/ucb/grep
rm=/bin/rm
tr=/usr/bin/tr
inews=inews

dotdir=${DOTDIR-${HOME-$LOGDIR}}
tmpart=$dotdir/.article

if $test -f $dotdir/.pnewsexpert; then
    expertise=expert
else
    $cat <<'EOM'
I see you've never used this version of Pnews before.  I will give you extra
help this first time through, but then you must remember what you learned.
If you don't understand any question, type h and a CR (carriage return) for
help.

If you've never posted an article to the net before, it is HIGHLY recommended
that you read the netiquette document found in mod.announce.newusers so
that you'll know to avoid the commonest blunders.  To do that, interrupt
Pnews, and get to the top-level prompt of rn.  Say "g mod.announce.newusers"
and you are on your way.

EOM
    expertise=beginner
fi

case $cntry in
  can) stpr=Province ;;
  *)   stpr=State ;;
esac

headerfile=""
case $# in
0) ;;
*)  case $1 in
    -h)
	headerfile="$2"
	shift
	shift
	case $# in
	0)
	    oldart=""
	    ;;
	*)
	    oldart="$1"
	    shift
	    ;;
	esac
	;;
    esac
    ;;
esac

case $headerfile in
'')
    . $rnlib/Pnews.header
    ;;
*)
    $cat < $headerfile  > $tmpart
    ;;
esac
    rescue="sleep 1; $cat $tmpart >>${HOME-$LOGDIR}/dead.article ; $echo Article appended to ${HOME-$LOGDIR}/dead.article ; exit"
    trap "$rescue" 1
    trap "$rescue" 2

$echo ""
set X `$sed < $tmpart -n -e '/^Distribution: /{' -e p -e q -e '}' -e '/^$/q'`
shift
case $# in
0|1)
    set X `$sed < $tmpart -n -e '/^Newsgroups: /{' -e p -e q -e '}'`
    shift
    case $# in
    0|1)
	set "x misc.whatever"
	;;
    esac
    ;;
*)
    set $1 $2.whatever
    ;;
esac
shift

#: play recorded message
#if $test -s ${lib}/recording ; then
#     ng=`$echo $1 | $sed "s/,.*//"`
#    _rec1=${lib}/`$sed -n "/^$ng/s/^.*	//p" ${lib}/recording`
#    _tmp=`$echo $ng |$sed "s/\..*//"`
#    _rec2=${lib}/`$cat -s ${lib}/recording|$grep ${_tmp}.all|$sed "s/^.*	//"`
#    if $test -f ${_rec1} ; then
#	$cat -s ${_rec1}
#    fi
#    if $test -f ${_rec2} ; then
#	$cat -s ${_rec2}
#    fi
#fi

# tell them what we think they are doing... !DIST!
case $1 in
net.*|world.*|comp.*|news.*|sci.*|rec.*|misc.*|soc.*|talk.*|mod.*)
    $cat <<'EOM'
This program posts news to thousands of machines throughout the entire
civilized world.  You message will cost the net hundreds if not thousands of
dollars to send everywhere.  Please be sure you know what you are doing.

EOM
    ;;
$cont.*)
    $echo 'This program posts news to many machines throughout the continent.'
    ;;
$cntry.*)
    $echo 'This program posts news to many machines throughout the country.'
    ;;
$state.*)
    $echo 'This program posts news to many machines throughout the state.'
    ;;
$city.*)
    $echo 'This program posts news to many machines throughout the city.'
    ;;
$org.*)
    $echo 'This program posts news to machines throughout the organization.'
    ;;
$loc.*)
    $echo 'This program posts news to machines throughout the local organization.'
    ;;
*.*)
    $echo 'This program may post news to many machines.'
    ;;
*)
    $echo 'This program posts news to everyone on the machine.'
    ;;
esac
ans=""
while $test "$ans" = "" ; do
    $echo $n "Are you absolutely sure that you want to do this? [ny] $c"
    read ans
    case $ans in
    y*) ;;
    f*) suppressmess=y ;;
    h*) $cat <<'EOH'

Type n or CR to exit, y to post.

EOH
	ans="" ;;
    *) exit ;;
    esac
done

file=h
while $test "$file" = h ; do
    $echo ""
    $echo $n "Prepared file to include [none]: $c"
    read file
    case $file in
    h)
	$cat <<'EOH'

If you have already produced the body of your article, type the filename
for it here.  If you just want to proceed directly to the editor, type a
RETURN.  In any event, you will be allowed to edit as many times as you
want before you send off the article.
EOH
	;;
    '')
	$echo "" >> $tmpart
	state=edit
	;;
    *)
	$cat $file >>$tmpart
	state=ask
	;;
    esac
done

$echo ""

while true ; do
    case $state in
    edit)
	case $expertise in
	beginner)
	    $cat </dev/null >$dotdir/.pnewsexpert
	    $cat <<'EOMessage'
A temporary file has been created for you to edit.  Be sure to leave at
least one blank line between the header and the body of your message.
(And until a certain bug is fixed all over the net, don't start the body of
your message with any indentation, or it may get eaten.)

Within the header may be fields that you don't understand.  If you don't
understand a field (or even if you do), you can simply leave it blank, and
it will go away when the article is posted.

Type return to get the default editor, or type the name of your favorite
editor.

EOMessage
	    ;;
	esac
	case "${VISUAL-${EDITOR-}}" in
	'')
	    tmp=h
	    ;;
	*)
	    tmp=''
	    ;;
	esac
	while $test "$tmp" = h ; do
	    $echo $n "Editor [${VISUAL-${EDITOR-$defeditor}}]: $c"
	    read tmp
	    case $tmp in
	    h)
		$cat <<'EOH'

Type a return to get the default editor, or type the name of the editor you
prefer.  The default editor depends on the VISUAL and EDITOR environment
variables.

EOH
		;;
	    '')
		;;
	    *)
		VISUAL=$tmp
		export VISUAL
		;;
	    esac
	done
	trap : 2
	${VISUAL-${EDITOR-$defeditor}} $tmpart $oldart
	trap "$rescue" 2
	state=ask
	;;
	
    ask)
	$echo ""
	$echo $n "Send, abort, edit, or list? $c"
	read ans
	
	case "$ans" in
	a*)
	    state=rescue
	    ;;
	e*)
	    set $ans
	    case $# in
	    2)  VISUAL="$2" ;;
	    esac
	    state=edit
	    ;;
	l*)
	    $pager $tmpart
	    state=ask
	    ;;
	s*)
	    state=send
	    ;;
	h*)
	    $cat <<'EOH'

Type s to send the article, a to abort and append the article to dead.article,
e to edit the article again, or l to list the article.

To invoke an alternate editor, type 'e editor'.
EOH
	esac
	;;
    
    send)
	set X `$sed < $tmpart -n -e '/^Newsgroups: /{' -e p -e q -e '}'`
	shift
	case $# in
	2)
	    state=cleanup
	    if $test -f $lib/moderators; then
		tryinews=no
		shift
		case "$1" in
		*,*) set `$echo $1 | tr ',' ' '`;;
		esac
		for newsgroup in $*; do
# the following screwy sed should prevent Eunice from hanging on no match
		    moderator=`$sed <$lib/moderators \
		    -e "/^$newsgroup[ 	]/!s/.*//" \
		    -e "s/^$newsgroup[ 	]//"`
		    case ${moderator}X in
		    X)  tryinews=yes
			;;
		    *)
			$echo Mailing to moderator $moderator
			case "$sign" in
			n*) ;;
			*)
			    if $test -f $dotdir/.signature; then
				echo $n "Append .signature file? [y] $c"
				read ans
				case $ans in
				''|y*)
				    echo "-- " >> $tmpart
				    cat $dotdir/.signature >> $tmpart
				    ;;
				esac
			    fi
			    sign=no
			    ;;
			esac
			case "$mailer" in
			*recmail)
			    $echo To: $moderator | $cat - $tmpart | $mailer
			    ;;
			*)
			    $mailer $moderator < $tmpart
			    ;;
			esac
			case $? in
			0) ;;
			*)
			    $echo Unable to mail to moderator $moderator
			    state=rescue
			    ;;
			esac
			;;
		    esac
		done
	    else
		tryinews=yes
	    fi
	    case "$tryinews" in
	    yes)
		if $inews -h < $tmpart ; then
		    : null
		else
		    state=rescue
		fi
		;;
	    esac
	    ;;
	*)
	    $echo ""
	    $echo "Malformed Newsgroups line."
	    $echo ""
	    sleep 1
	    state=edit
	    ;;
	esac
	;;
    rescue)
	$cat $tmpart >> ${HOME-$LOGDIR}/dead.article
	$echo "Article appended to ${HOME-$LOGDIR}/dead.article"
	$echo "A copy may be temporarily found in $tmpart"
	exit
	;;
    cleanup)
	case "${AUTHORCOPY-none}" in
	none)
	    ;;
	*)  if $echo From $USER `date` >> $AUTHORCOPY; then
		$cat $tmpart >> $AUTHORCOPY
		$echo "Article appended to $AUTHORCOPY"
		$echo "" >> $AUTHORCOPY
		$echo "" >> $AUTHORCOPY
	    else
		$echo "Cannot append to $AUTHORCOPY"
	    fi
	    ;;
	esac
	exit
	;;
    esac
done
