#! /bin/sh
# A script to rebuild the newsfeed file after a gup run

. /var/lib/news/gup/gupconfig
PATH=/usr/bin:/bin:/usr/lib/news/bin

########################### end of user-config stuff ##########################

cd $gupdir
(
	cat newsfeeds.start
	cd sites
	for h in * ; do
		if [ -s "$h/groups" ]; then
			echo
			sed -e "s/HOST/$h/g" $h/header			#header
			sed -e 's/$/,\\/g' -e 's/\\.\\*$//g' $h/groups	#body
			sed -e "s/HOST/$h/g" $h/trailer			#trailer
		fi
	done
	cd ..
	cat newsfeeds.end
) >$newsfeeds.new

# commit the new version
mv -f $newsfeeds.old $newsfeeds.old1
mv -f $newsfeeds $newsfeeds.old
mv $newsfeeds.new $newsfeeds

# verify that it's ok
$ctlinnd checkfile >/dev/null 2>/dev/null
ok=$?
if [ "$ok" -ne 0 ]; then
  echo "argh!  $newsfeeds verification failed - moving back previous version"
  mv $newsfeeds $newsfeeds.broken
  mv -f $newsfeeds.old $newsfeeds
  $mailcmd -s "gup newsfeeds failure!" $news_admin <<SIGH
gup has gone weird - the generated newsfeeds file has been rejected by
$ctlinnd checkfile.  Please check:
	$newsfeeds.broken

to determine the source of the problem.

yours in efficiency,
the gupdate daemon
SIGH
  exit 1
fi

# tell INN about it
$ctlinnd reload newsfeeds gupup 2>&1 >/dev/null
