#! /bin/sh
#+++
#  RCS setnewsconf.sh,v 1.8 1995/07/13 11:37:03 tom Exp
#  title:	setnewsconf.sh
#  abstract:	set news config according to (key,value) parameters from stdin.
#  author:	T.R.Hageman, Groningen, The Netherlands
#  created:	December 1994
#  modified:	(see RCS Log at end)
#  copyleft:
#
#		Copyright (C) 1994,1995  Tom R. Hageman.
#
#	This 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 software 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 software; if not, write to the Free Software
#	Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
#  description:
#
#	This script is part of NewsConfig.app
#
#---

NEWSCONFIGAPP=$1

# =()<. ${NEWSCONFIG-@<NEWSCONFIG>@}>()=
. ${NEWSCONFIG-/usr/local/lib/news/bin/config}
export NEWSCTL NEWSBIN NEWSARTS NEWSPATH NEWSUMASK NEWSMASTER NEWSCONFIG
PATH=$NEWSCONFIGAPP:$NEWSCTL/maint:$NEWSBIN/bin:$NEWSBIN:$NEWSPATH
export PATH

# Read the new configuration (key, value) pairs from stdin.
while read key value
do
    eval "$key=\"$value\""
done

# Split NEWSFEED info into sitename and /exclusions (aliases).
NEWSFEED_NAME=`expr "$NEWSFEED" : '^\([^/:]*\).*'`
NEWSFEED_EXCLUSIONS=`expr "$NEWSFEED" : '^[^:/]*\(/[^:]*\)'`

status=1
locks=
buiddir=
trap 'rm -f $locks;trap 0;exit $status' 0 1 2 15

# Lock the news system while we're doing our dastardly deeds.
lock=$NEWSCTL/LOCK
ltemp=$lock.$$
locks=$ltemp
echo $$ >$ltemp || exit
newslock $ltemp $lock || { 
	sleep 5
	# Try again, once.
	echo $$ >$ltemp || exit
	newslock $ltemp $lock || { status=${EXIT_LOCKED-99}; exit; }
}
rm -f $ltemp
locks=$lock

# Create a temp. directory to build the new configuration in.
builddir=/tmp/setnewsconf$$
rm -rf $builddir
mkdir $builddir || exit
cd $builddir || exit

# simple things first
echo $ORGANIZATION >organization || exit
echo $HOSTNAME >mailname || exit
echo $UUCPNAME >whoami || exit

[ -z "$EXPIRERMDIR" ] || touch expirermdir || exit
[ -z "$IMMEDNEWS" ] || touch rnews.immed || exit
[ -z "$ADDSIGNATURE" ] || echo $ADDSIGNATURE >addsignature || exit

# build new sys file

ORGFEED=''

if [ -r $NEWSCTL/sys ]
then
	canonsys.sh $NEWSCTL/sys >sys.tmp || exit
	[ -n "$NEWSFEED_NAME" ] || {
		NEWSFEED_NAME=`sed -n '/^ME:/d; /^\([^:/]*\).*/{; s//\1/p; q; }' sys.tmp`
	}
	ORGFEED=`grep "^$NEWSFEED_NAME[:/]" sys.tmp`
	# Get default values from existing entry.
	[ "$NEWSFEED_EXCLUSIONS" = "/" ] &&
		NEWSFEED_EXCLUSIONS=`expr "$ORGFEED" : '^[^:/]*\(/[^:]*\)'`
	[ -z "$NEWSFEED_SUBSCRIPTIONS" ] &&
		NEWSFEED_SUBSCRIPTIONS=`expr "$ORGFEED" : '^[^:]*:\([^:/]*\)'`
	[ -z "$NEWSFEED_DISTRIBUTION"  ] &&
		NEWSFEED_DISTRIBUTION=`expr "$ORGFEED" : '^[^:]*:[^:/]*/\([^:]*\)'`
	[ -z "$NEWSFEED_FLAGS" ] &&
		NEWSFEED_FLAGS=`expr "$ORGFEED" : '^[^:]*:[^:]*:\(.*\)'`

	# Defaults for my own subscriptions
	MYFEED=`grep "^ME[:/]" sys.tmp`
## Only subscriptions are meaningful for receiving news.
##	DISTRIBUTION=`expr "$ORGFEED" : '^[^:]*:[^:/]*\(/[^:]*\)'`
	[ -z "$ACTIVEGROUPS" ] &&
		ACTIVEGROUPS=`expr "$MYFEED" : '^[^:]*:\([^:/]*\)' | tr ',' ' '`
else
	cat >sys.tmp <<! || exit
# $ID
#
# Only the ME line is mandatory; the others are just samples of how to do
# things.  Virtually everything will need modifying for your local feeds
# and newsgroups.

# Line indicating what we are willing to receive:
ME:all
!
fi

if [ "$ACTIVEGROUPS" = "$AVAILABLEGROUPS" ]
then
	SUBSCRIPTIONS=all
else
	SUBSCRIPTIONS=`echo $ACTIVEGROUPS | tr ' ' ','`
fi
##[ "$ACTIVEGROUPS" = "all" ] && ACTIVEGROUPS="$AVAILABLEGROUPS"
##[ -z "$DISTRIBUTION" ] && DISTRIBUTION="/all"

if [ -n "$ALLGROUPS" ]
then
	EXCEPTIONS=`echo $AVAILABLEGROUPS | tr ' ' '\012' |
		gngp -av "$SUBSCRIPTIONS" | sed 's/^/!/'`
	if [ -n "$EXCEPTIONS" ]
	then
		SUBSCRIPTIONS=all,`echo $EXCEPTIONS | tr ' ' ','`
	else
		SUBSCRIPTIONS=all
	fi
fi

# Default values
[ "$NEWSFEED_EXCLUSIONS" = "/" -o -z "$NEWSFEED_EXCLUSIONS" ] &&
	NEWSFEED_EXCLUSIONS=""
[ -z "$NEWSFEED_SUBSCRIPTIONS" ] &&
	NEWSFEED_SUBSCRIPTIONS="all,!junk,!general"
[ "$NEWSFEED_DISTRIBUTION" = "/" -o -z "$NEWSFEED_DISTRIBUTION" ] &&
	NEWSFEED_DISTRIBUTION="all,!local"
[ -z "$NEWSFEED_FLAGS" ] &&
	NEWSFEED_FLAGS="f:"

# Normalize fields; replace spaces with commas.
# (leading and trailing spaces are already stripped automagically by read)
for field in NEWSFEED_EXCLUSIONS NEWSFEED_SUBSCRIPTIONS NEWSFEED_DISTRIBUTION
do
  eval 'case "$'$field'" in #(
	*\ *) '$field'=`echo "$'$field'" | sed "s/ /,/g;s/,,,*/,/g"`
	esac'
done

# replace feed in sys
sed "/^ME[:\/]/c\\
ME:$SUBSCRIPTIONS$DISTRIBUTION\\
\\
# Primary newsfeed:\\
$NEWSFEED_NAME$NEWSFEED_EXCLUSIONS:$NEWSFEED_SUBSCRIPTIONS/$NEWSFEED_DISTRIBUTION:$NEWSFEED_FLAGS
/^# Primary newsfeed:\$/d
/^$NEWSFEED_NAME/d" sys.tmp >sys || exit
rm sys.tmp


# support function
zcat_maybe() { # root file name
    while :
    do
	case "$#" in
	0) break
	esac

	cat "$1"		2>/dev/null ||
	zcat "$1".Z		2>/dev/null ||
	gzcat "$1".gz	2>/dev/null ||
	  echo "zcat_maybe: cannot read file $1 (plain, .Z or .gz)" >&2

	shift
    done
}

# Create new active file by merging existing active (if any) with template
# in app wrapper.  Same with newsgroups and active.times file.

ACTIVE_CURRENT=$NEWSCTL/active
[ -r $ACTIVE_CURRENT ] || ACTIVE_CURRENT=/dev/null

zcat_maybe $NEWSCONFIGAPP/active | mergeactive.sh $ACTIVE_CURRENT - |
	gngp -a "$SUBSCRIPTIONS" >active || exit


NEWSGROUPS_CURRENT=$NEWSCTL/newsgroups
[ -r $NEWSGROUPS_CURRENT ] || NEWSGROUPS_CURRENT=/dev/null

zcat_maybe $NEWSCONFIGAPP/newsgroups | mergenewsgroups.sh $NEWSGROUPS_CURRENT - |
	gngp -a "$SUBSCRIPTIONS" >newsgroups || exit


ACTIVE_TIMES_CURRENT=$NEWSCTL/active.times
[ -r $ACTIVE_TIMES_CURRENT ] || ACTIVE_TIMES_CURRENT=/dev/null

mergeacttimes.sh active $ACTIVE_TIMES_CURRENT |
	gngp -a "$SUBSCRIPTIONS" >active.times || exit


# Provide a default "newgroupperm" config file (for newgroup and rmgroup) 
# (but not for CNews Cleanup Release that supports controlperm)
[ -f $NEWSCTL/newgroupperm -o -f $NEWSCTL/controlperm ] ||
	cp -p $NEWSCONFIGAPP/newgroupperm . || exit


# Move stuff in place; i.e., copy all files that have been changed to $NEWSCTL,
# while making a backup of the originals (in a backup directory).

date="`date | sed 's/^... \(.*:[0-5][0-9] \).* \(....\)\$/\1\2/'`"
backupdate=Backup_`echo $date | tr ' ' '_'`_by_NewsConfig

backupdir=$NEWSCTL/$backupdate

mkdir $backupdir || exit

# Some files have meaning solely by their presence.
for f in rnews.immed expirermdir addsignature
do
	if [ -f $f ]
	then
		[ -f $NEWSCTL/$f ] || touch $backupdir/$f.NOT
	else
		[ ! -f $NEWSCTL/$f ] || mv $NEWSCTL/$f $backupdir/$f || exit
	fi
done


# Move new setup in place whilst making a backup of existing setup in $NEWSCTL
for f in *
do
	if [ -f $NEWSCTL/$f ]
	then
		cmp -s $NEWSCTL/$f $f || {
			cp -p $NEWSCTL/$f $backupdir/$f &&
			cp $f $NEWSCTL/$f || exit
		}
	else
		cp $f $NEWSCTL/$f || exit
	fi
done

# Try to remove the backupdir.  Only succeeds if setup is unchanged.
rmdir $backupdir >/dev/null 2>&1

# Remove temporary build directory
rm -rf $builddir

# Create spool directory for principal newsfeed if it does not exist.
[ -d $NEWSARTS/out.going/$NEWSFEED_NAME ] ||
	mkdirs $NEWSARTS/out.going/$NEWSFEED_NAME || exit

status=0
exit

#======================================================================
# setnewsconf.sh,v
# Revision 1.8  1995/07/13 11:37:03  tom
# add newssys locking; more strict error handling throughout;
# add ALLGROUPS support; fixed avoid-copy bug (still copied/-);
# [fixed Copyright notice]
#
# Revision 1.7  1995/01/27  04:19:01  tom
# Try to make cleanup work (still doesn't).
#
# Revision 1.6  1995/01/25  13:57:23  tom
# (NEWSFEED): normalize fields; collapse sequences of multiple spaces and
#  commas into a single comma.
#
# Revision 1.5  1995/01/13  22:40:45  tom
# (NEWSFEED_SUBSCRIPTIONS,NEWSFEED_DISTRIBUTION,NEWSFEED_FLAGS): taken from
#  GUI frontend.
#
# Revision 1.4  1995/01/10  04:29:36  tom
# remove distribution from ME line since it is menaingless.
#
# Revision 1.3  1995/01/02  20:41:56  tom
# (NEWSFEED_DISTRIBUTION): add !local to default;
# (zcat_maybe): simplified; (ACTIVE_EGREP): removed, use gngp instead;
#
# Revision 1.2  1994/12/28  20:04:39  tom
# Fleshed out.
#
# Revision 1.1  1994/12/20  14:41:15  tom
# Initial revision
#
#======================================================================
