/*
 * install.cpp	This is a shell script that goes through the
 *		C Preprocessor to include the definitions from
 *		custom.h.
 *
 * Version	@(#) install.cpp  4.50  03-Mar-1995  MvS.
 */
#include "custom.h"

/* Some constants. */
LIBPROGS="control expire inews sendbatch unbatch updgroups mkhist"
BINPROGS="checknews pnews"
SUIDPROGS="inews control expire sendbatch unbatch mkhist"
CONTROLPR="checkgroups newgroup rmgroup cancel sendsys senduuname version"
SHELLS="mkspool mkgroup delgroup"

/* Store some directory names from custom.h */
libdir=$ROOTDIR`echo LIBDIR`
confdir=$ROOTDIR`echo CONFDIR`
mandir=$ROOTDIR`echo MANDIR`
bindir=$ROOTDIR`echo BINDIR`
spooldir=$ROOTDIR`echo SPOOLDIR`
vardir=$ROOTDIR`echo VARDIR`
incoming=$ROOTDIR`echo SPOOLDIR`/`echo INCOMING`
nntpdir=$ROOTDIR`echo SPOOLDIR`/.tmp
rlibdir=`echo LIBDIR`
rconfdir=`echo CONFDIR`

umask 022

/* Tell the user about the version. */
echo VERSION

/* See if there is a "news" user. */
grep news: /etc/passwd > /dev/null
if [ $? != 0 ]
then
  echo "Before installing W-News, you need to add a user \"news\" and a"
  echo "group \"news\" to your system. This means you need to add lines"
  echo "like this:"
  echo
  echo "/etc/passwd:  news:50:50:*:News System:/usr/lib/news:"
  echo "/etc/group:   news::50:"
  echo
  echo "Please do this now, and then retry the installation."
  exit 1
fi

/* Figure out the hostname. */
if [ HAVE_HOST = 1 ]
then
  /* We have a normal hostname command. */
  hostname=`hostname`
  echo $hostname | grep '\.' > /dev/null 2>&1
  if [ $? != 0 -a HAVE_DOM = 1 ]
  then
	hostname=$hostname.`domainname`
  fi
elif [ HAVE_HOSTENV != "HAVE_HOSTENV" ]
then
  /* The host name in an environment variable. This will never happen
   * under *nix, and since this script only runs under *nix, we shouldn't
   * have to worry about this at all.
   */
  hostname=$HAVE_HOSTENV
  hostenv=HAVE_HOSTENV
  if [ "$hostname" = "" ]
  then
	echo "The environment variable $hostenv is not set."
	echo "Please enter your hostname (+domain) now."
	echo -n "? "
	read hostenv
	hostname=$hostenv
  fi
elif [ HAVE_UUHOST != "HAVE_UUHOST" ]
then
  /* Hostname in a file. */
  if [ -f HAVE_UUHOST ]
  then
	exec 4>&0
	exec < HAVE_UUHOST
	read hname
	/* See where we need to fetch the domainname */
	if [ -f HAVE_UUDOM ]
	then
		exec < HAVE_UUDOM
	fi
	read dname
	exec 0>&4 4>&-
  fi
  /* Ask for hostname and domainname if we don't have it. */
  if [ "$hname" = "" -o "$dname" = "" ]
  then
	echo "Your system keeps the hostname and domainname in files."
	echo "These are not setup (properly) yet."
	echo "Please enter your host and domainnames seperately now."
	echo -n "Hostname? "
	read hname
	echo -n "Domainname? "
	read dname
	echo $hname > HAVE_UUHOST
	chown news HAVE_UUHOST
	chgrp news HAVE_UUHOST
	if [ HAVE_UUDOM != "HAVE_UUDOM" ]
	then
		echo $dname > HAVE_UUDOM
		chown news HAVE_UUDOM
		chgrp news HAVE_UUDOM
	else
		echo $dname >> HAVE_UUHOST
	fi
  fi
  hostname=$hname.$dname
else
  /* Hmm hostname not found. */
  echo "I don't know how you did it, but there is no possible way to"
  echo "figure out the hostname of this system. Please edit the header"
  echo "file for your system and try again."
  exit 1
fi

/* Okay, hostname setup properly. */
echo "Your system name is $hostname."

/* Check if we have symbolic links. */
>/tmp/inst.$$
ln -s /tmp/inst.$$ /tmp/inst2.$$ 2>/dev/null
if [ $? = 0 ]
then
	symlink=1
else
	symlink=0
fi
rm -f /tmp/inst.$$ /tmp/inst2.$$

/* Create needed directories. */
echo "I am now going to create the needed directories. If this fails,"
echo "please correct it by hand and try again."
for i in $spooldir $batchdir $incoming $nntpdir $oldnews $confdir $vardir $libdir
do
  if [ ! -d $i ]
  then
	echo "Creating directory $i"
	mkdir $i || exit 1 /* Exit if failed. */
	chown news $i
	chgrp news $i
  fi
done

/* Install binaries in eg. /usr/bin */
echo "Installing public binaries in" $bindir
for i in $BINPROGS
do
  if [ $symlink = 1 ]
  then
	cp $i $libdir || exit 1
	rm -f $bindir/$i
	ln -s $rlibdir/$i $bindir/$i || exit 1
	chown news $libdir/$i $bindir/$i
	chgrp news $libdir/$i $bindir/$i
  else
	cp $i $bindir || exit 1
	chown news $bindir/$i
	chgrp news $bindir/$i
  fi
done

/* Now install the library programs. */
echo "Installing private binaries in" $libdir
for i in $LIBPROGS
do
  [ ! -f $i ] && continue
  cp $i $libdir || exit 1
  chown news $libdir/$i
  chgrp news $libdir/$i
  chmod 511 $libdir/$i
done

/* Special case: rnews _needs_ to be a link to inews. */
echo "Installing rnews link in $bindir"
rm -f $bindir/rnews $libdir/rnews
if [ $symlink = 1 ]
then
  ln -s $rlibdir/inews $bindir/rnews
  chown news $bindir/rnews
  chgrp news $bindir/rnews
else
  /* Try to set up a link, otherwise copy. */
  ln $libdir/inews $bindir/inews 2>/dev/null
  if [ $? != 0 ]
  then
	echo "   rnews link failed, copying.."
	cp inews $bindir/rnews
	chown news $bindir/rnews
	chgrp news $bindir/rnews
	chmod 6511 $bindir/rnews
  fi
fi

/* Install the shell scripts. */
for i in $SHELLS
do
  cp $i.sh $libdir/$i || exit 1
  chown news $libdir/$i
  chgrp news $libdir/$i
  chmod 555 $libdir/$i
done

/* Set the right modes for some progs. */
echo "Setting up secure and setuid modes, etc.."
for i in $SUIDPROGS
do
  if [ $i = inews ]
  then
	mode=6511
  else
	mode=6510
  fi
  if [ -f $libdir/$i ]
  then
	chmod $mode $libdir/$i
  else
	chmod $mode $bindir/$i
  fi
done

/* Remove the old control programs. */
for i in $CONTROLPR
do
  rm -f $libdir/$i
done

/* Install man pages. */
if [ -d "$mandir"/man5 ]
then
  echo "Installing man pages (nroff source)."
  cd man
  for i in 1 5 8
  do
	for man in *.$i
	do
		cp $man $mandir/man$i/$man || exit 1
		chmod 644 $mandir/man$i/$man
		chown news $mandir/man$i/$man
		chgrp news $mandir/man$i/$man
	done
  done
  cd ..
else
  echo "Manual pages (nroff source) NOT installed."
fi

/* See if we need to initialize the history database. */
#if HAVE_HIST && HAVE_DBM
if [ ! -f $vardir/history ]
then
	/* First the normal history file. */
	> $vardir/history
	chown news $vardir/history
	chgrp news $vardir/history
fi
if [ ! -f $vardir/history.pag ]
then
	/* Create the history database. */
	echo "Creating history database, this might take a while..."
	$libdir/mkhist -d $vardir/history
fi
#endif

/* Check if we have a sys file or a newsfeeds file. */
if [ -f $confdir/sys ]
then
	sys=sys
else
	sys=newsfeeds
fi

echo
echo "Your news flow is controlled by the" $confdir/$sys "file."
echo "Please read the documentation of the format of this file."
echo

/* Set up configuration files. */
echo "Setting up configuration files in" $confdir
cd samples
change=0
active=0
for i in *
do
  [ $i = sys -a $sys = newsfeeds ] && continue
  [ $i = newsfeeds -a $sys = sys ] && continue
  if [ ! -f $confdir/$i ]
  then
	echo "Oh! You don't have a $i file.. I'll install a sample one."
	if [ $i = active ] && [ HAVE_LONGACT = 1 ]
	then
		sed -e 's/ 00000 00000 / 0000000000 0000000000 /' < active > $confdir/$i
	else
		cp $i $confdir/$i || exit 1
	fi
	chmod 644 $confdir/$i
	chown news $confdir/$i
	chgrp news $confdir/$i
	change=1
	[ $i = active ] && active=1
  fi
done

/* Set up symbolic links for the PRO release. */
if [ "$libdir" != "$confdir" -a $symlink = 1 ]
then
  for i in newsgroups active distributions
  do
	rm $libdir/$i 2>/dev/null
	ln -s $rconfdir/$i $libdir/$i
	chown news $libdir/$i
	chgrp news $libdir/$i
  done
fi

echo "Normally we would create the spool directory hierarchy now,"
echo "but starting at this version Wnews creates the spool directories"
echo "on-the-fly. This means that we're done installing!"
echo

if [ $change = 1 ]
then
  echo "** If any new files got installed, then edit them first to reflect"
  echo "** your system setup."
  echo
else
  echo "Your system seems to be already configured, but please check"
  echo "all configuration files before using wnews."
  echo
fi

exit 0
