#!/bin/bash

# Configurationscript for newsflash
# Copyright by Matthias Kabel <mkabel@debian.org> 1999
# Feel free to redistrubute and/or modify it  under the terms of the 
# GNU Public Licence
# You should hve the GPL somewhere on your computer if not, write to
# the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.

CONFIG_PATH="/etc/news/newsflash/"
GROUPLIST="list"
GROUPLIST0="list0"
GROUPLIST1="list1"

SERVERLIST="serverlist"

ACTIVE_PATH="/var/lib/news/"
ACTIVE_FILE="active"

MAX_NUMBER=4

DATE=`date +%s`
DATE=`expr $DATE - 86400`

README="#Read /usr/doc/newsflash/README and edit the file for optimization"

##############################################################
ask_active_file()
{
	echo "There is an active file from inn, should we take this ?
This would be only a first guess, you have to optimize the resulting files.
Please answer y or n
"
	read a
	case $a in
		y|Y) create_grouplists ; useactive ;;
		n|N) readme ;;
		*) ask_active_file;;
	esac
}

##############################################################
ask_fqdn()
{
	echo -n "So I need the FQDN of your newsserver you want to fetch from
 (e.g. news.your_provider.com or news.your_univerity.edu)
"
	read fqdn	
	echo "Is $fqdn correct? Please say yY or nN"
	read a
	case $a in
		y|Y) return 0;;
		n|N) ask_fqdn 0;;
		*) ask_fqdn;;
	esac
}

##############################################################
ask_port()
{
	echo "Wich port are you using. Assuming 119 port=[119]"
	read port
	if $port [ "$port" = "" ]
	then
		port="119";
	else
		echo "Is $port correct? Please say yY or nN"
		read a	
		case $a in
			y|Y) return 0;;
			n|N) ask_port 0;;
			*) ask_port;;
		esac
	fi
}

##############################################################
create_grouplists()
{
	rm -f $CONFIG_PATH"list"*[0-9]

	cut -f 1 -d " " < $ACTIVE_PATH$ACTIVE_FILE | sed /junk/D | sed /control/D  |  sort > $CONFIG_PATH$GROUPLIST0
	
	file_no=1
	group_no=1
	while read group
	do
		filename1=$CONFIG_PATH"list"$file_no
		if  [ $group_no -le $MAX_NUMBER ] 
		then
			echo $group >> $filename1
			group_no=`expr $group_no + 1`
		else
			group_no=1
			filename2=$CONFIG_PATH"list"`expr $file_no + 1`
			sort -r  < $filename1 > $filename2
			file_no=`expr $file_no + 2`
		fi
	done < $CONFIG_PATH$GROUPLIST0
	
	rm $CONFIG_PATH$GROUPLIST0
	
}

##############################################################
readme()
{
echo "Ok, we do not use the active-file from inn, so please read
\"/usr/doc/newsflash/README\" and edit the folling files
\"/etc/news/newsflash/serverlist\"
\"/etc/news/newsflash/list\[?\]\""
echo "I will prepare there examplesfiles for you"
echo -n "somewhere news.somewhere.com    119 $GROUPLIST0 " > $CONFIG_PATH$SERVERLIST
echo $DATE >> $CONFIG_PATH$SERVERLIST
echo -n "somewhere news.somewhere.com    119 $GROUPLIST1 " >> $CONFIG_PATH$SERVERLIST
echo $DATE >> $CONFIG_PATH$SERVERLIST

echo "comp.os.linux.announce
alt.talk.bizarre
alt.sysadmin.recovery" > $CONFIG_PATH$GROUPLIST0

echo "alt.sysadmin.recovery
alt.talk.bizarre
comp.os.linux.announce" > $CONFIG_PATH$GROUPLIST1
}

##############################################################
save_oldfiles()
{
	if [ -f $CONFIG_PATH"serverlist" ]
	then
		mv $CONFIG_PATH"serverlist" $CONFIG_PATH"serverlist.old"
	fi
	if [ -f $CONFIG_PATH"list1" ] 
	then
		for i in $CONFIG_PATH"list"*[0-9]
		do
			mv $i $i.old
		done
	fi
}
##############################################################
useactive()
{
	ask_fqdn
	ask_port		
		
	echo "So we are using the active file, I will create the grouplistfiles therefrom.
	 
Read

/usr/doc/newsflash/README

for more information and how to optimize newsflash to your needs.
It will increase speed heavily if you play a little with the configuration.
I am sorry but optimization depends very heavily from your newsgroups, and your connection to the internet so I can not handle the optimization for you.


I will fetch the news of the last 24h hours for you.
This is only the initial value. If everything works well,
I will only fetch new news for you after the first time"		

i=1
file_no=`ls -l /etc/news/newsflash/list[0-9]* | wc -l`

while [ $i -le $file_no ] 
do
	echo -n "$fqdn $fqdn $port $CONFIG_PATH$GROUPLIST$i " >> $CONFIG_PATH$SERVERLIST
	echo $DATE >> $CONFIG_PATH$SERVERLIST
	i=`expr $i + 1`
done
}


#############################################################
#
# First we have to install the directory
install -d $CONFIG_PATH

# then we should save any old file if there are some
save_oldfiles

#
# Now we should look if there is an active file from inn

if [ -f $ACTIVE_PATH$ACTIVE_FILE ] 
then
	ask_active_file
else
	readme
fi

chown news.news /etc/news/newsflash -R
chmod ug+w /etc/news/newsflash/ -R

echo "Press RETURN to end" 
read a
exit 0
