#!/bin/sh
#
# Configure script for FreeBSD port of NN-6.5.3
#
# Comments to Jasper O'Malley <jooji@webnology.com>
#

cd ${WRKSRC}/inews

if [ ! -f conf.h.dist ]; then
	if [ ! -f conf.h ]; then
		echo "Can't find the file ${WRKSRC}/inews/conf.h"
		echo "Do a \"make clean\" and try making the NN port again."
		exit 1
	else
		mv -f conf.h conf.h.dist
	fi
fi

while :
do
	read -p"Enter hostname or IP address of your news server: " news_server
	if [ -z $news_server ]; then
		echo "You must supply a hostname or IP address. Try again."
		echo ""
		break
	else
		while :
		do
			echo ""
			echo "You entered: ${news_server}"
			echo ""
			read -p"Is this what you wanted? [y/N] " yesno
			if [ -z $yesno ]; then
				yesno="N"
				break
			elif [ "$yesno" = "Y" -o "$yesno" = "y" -o "$yesno" = "n" -o "$yesno" = "N" ]; then
				break
			else
				echo "Please enter Y or N."
				echo ""
			fi
		done
		echo ""
		if [ "$yesno" = "y" -o "$yesno" = "Y" ]; then
			echo "\"${news_server}\" it is."
			echo ""
			break
		fi
	fi
done

echo "${news_server}" > ${WRKDIR}/nntp_server

cat << xxENDxx
NN requires a utility called INEWS to post news to an NNTP server.
INEWS is distributed with a variety of software packages, including the
INN news server.

A minimal version of INEWS--dubbed Mini-INEWS--is included with this
distribution of NN. Mini-INEWS has the poster's e-mail "From:" domain
(the bit that appears after the @ symbol) hardcoded into the utility.
The configuration program running now is used to set that domain.

xxENDxx

if [ -r ${PREFIX}/news/lib/inews ]; then
	strings ${PREFIX}/news/lib/inews | grep -q MINI-INEWS
	if [ $? -eq 1 ]; then
		cat << xxENDxx
However, a version of INEWS that may or may not be Mini-INEWS has been detected
on your system, in location "${PREFIX}/news/lib/inews".

Because you already have a version of INEWS, you probably do not need to
install Mini-INEWS at this time.  However, if you want to make changes to a
previous Mini-INEWS installation, you will need to overwrite the existing
version. In addition, installing Mini-INEWS now will allow future installations
of NN to detect that the Mini-INEWS client is installed.

xxENDxx
		while :
		do
			read -p"Do you wish to install Mini-INEWS and overwrite ${PREFIX}/news/lib/inews? " yesno
			if [ "$yesno" = "y" -o "$yesno" = "Y" -o "$yesno" = "n" -o "$yesno" = "N" ]; then
				break
			else
				echo "Please enter Y or N."
				echo ""
			fi
		done
		if [ "$yesno" = "N" -o "$yesno" = "n" ]; then
			echo "Mini-INEWS will not be installed. If you wish to install Mini-INEWS"
			echo "at a later time, reinstall the NN port."
			rm -f ${WRKDIR}/.install_inews
			exit 0
		else
			echo ""
		fi
	else
		echo "You have what appears to be a version of Mini-INEWS installed on"
		echo "your system, in location \"${PREFIX}/news/lib/inews\"."
		echo ""
		while :
		do
			read -p"Do you wish to reinstall Mini-INEWS with new settings? " yesno
			if [ "$yesno" = "y" -o "$yesno" = "Y" -o "$yesno" = "n" -o "$yesno" = "N" ]; then
				break
			else
				echo "Please enter Y or N."
				echo ""
			fi
		done
		if [ "$yesno" = "Y" -o "$yesno" = "y" ]; then
			echo ""
		else
			echo "Mini-INEWS will not be installed."
			rm -f ${WRKDIR}/.install_inews
			echo ""
			exit 0
		fi
	fi
fi

defdomain=`hostname | sed -e 's/^[^.]*\.//' | sed -e 's/^[^.]*$//`

while :
do
	echo "What do you want to appear after the @ symbol in the From:"
	echo "header in Usenet articles posted by your users with NN?"
	echo ""
	read -p"[default: ${defdomain:-(none)}]: " domain
	if [ -z ${domain:=${defdomain}} ]; then
		echo ""
		echo "You must supply a value. Try again."
		echo ""
	elif [ `echo ${domain} | grep -c '\.'` -eq 0 ]; then
		echo ""
		echo "You cannot supply a local hostname (i.e. the hostname must contain a . character)"
		echo ""
	else
		while : 
		do
			echo ""
			echo "You entered: ${domain}"
			echo "Example header: From: user@${domain}"
			echo ""
			read -p"Is this what you wanted? [y/N] " yesno
			if [ -z $yesno ]; then
				yesno="N"
				break
			elif [ "$yesno" = "y" -o "$yesno" = "Y" -o "$yesno" = "n" -o "$yesno" = "N" ]; then
				break
			else
				echo "Please enter Y or N."
				echo ""
			fi
		done
		echo ""
		if [ "$yesno" = "y" -o "$yesno" = "Y" ]; then
			echo "\"${domain}\" it is."
			break
		fi
	fi
done

cat conf.h.dist | sed -e '/\/\* #define.*DOMAIN/c\' -e"#define DOMAIN \"${domain}\"" > conf.h
touch ${WRKDIR}/.install_inews
echo ""
