#!/bin/ksh

#
# Copyright (c) 1996 by Sun Microsystems, Inc. All Rights Reserved.
#
# @(#)feeder_setup 1.21 99/02/18
#

##  =()<. @<_PATH_NEWSLIB>@/innshellvars>()=
. /etc/opt/SUNWsns/innshellvars
TEXTDOMAIN=isp_sns # We need to set this because if we give the domainname as an argument the xgettextsh
                   # fails.
export TEXTDOMAIN
script_name=$(basename $0)
script_dir=$(dirname $0)

## Decide if this is a Feeder-only or Feeder+Reader configuration.
## If package SUNWsnsr is not installed, then it is Feeder-only.
this_config="`${NEWSSETUP}/get_config_type | cut -f1`"

if [ "$this_config" = "RF" ]; then
  print `/usr/bin/gettext "                Feeder + Reader configuration"`
else
  print `/usr/bin/gettext "                  Feeder only configuration"`
fi
print "============================================================"
print " "
print " "

verbose=YES
ready_to_go=YES

mkdir_cmd="mkdir -p"
cp_cmd="cp -p"
if test ! -z "$_NEWS_INSTALL_TEST" ; then
    cp_cmd="echo $cp_cmd"
    mkdir_cmd="echo $mkdir_cmd"
fi

#
# Although root is a variable, its value cannot change,
# as INN has been compiled with these pathnames.
#
root=/var/news

if test -z "$_NEWS_INSTALL_TEST" -a -d $root ; then
    echo " "
    printf "`/usr/bin/gettext 'The top-level directory for news setup, %s, exists.\nHere are its contents.'`\n" $root
    printf "----------------------\n"
    ls -l $root
    printf "----------------------\n"
    while true
    do
      reply=`ckyorn -Q -p "Is it okay to continue doing the setup ? "`
      case "$reply" in
	[yY]) break;;
	*) exit 1;;
      esac
    done
fi

if test "X$script_dir" = "X" -o "X$script_dir" = "X." ; then
    PATH=$PATH:`pwd`
else
    PATH=$PATH:$script_dir
fi
export PATH

##  =()<. @<_PATH_NEWSBIN>@/setup/mkaccess.shf>()=
. ${NEWSSETUP}/mkaccess.shf

INNCONF=$NEWSCONF/inn.conf
NEWSCRONTAB=$NEWSCONF/news.crontab
HOSTSNNTP=$NEWSCONF/hosts.nntp

#
# Setting the following variables allows the script to create
# a News installation.
#
state_place=
spool_place=
log_place=
server_fqdn=
organization=
feed=

#
# Defaults for the above variables; they aren't advisable, unless
# you have a huge root partition.
#
default_state_place=$(dirname $ACTIVE)
default_spool_place=$(dirname $SPOOL)
default_log_place=$MOST_LOGS


#
# Override the mkdir command, so that we change the ownership of
# the directories we create
#
makedir()
{
    $mkdir_cmd $1 && set_access $1
}


confirm()
{
    typeset ans

    printf "`/usr/bin/gettext 'Your choices were:'`\n"
    printf "`/usr/bin/gettext '	Hostname:		%s'`\n" $server_fqdn
	printf "`/usr/bin/gettext '	Organization:		'`"
	echo $organization
    if [ -n "$feed" ]; then
      printf "`/usr/bin/gettext '	Feed:			%s'`\n" $feed
      printf "`/usr/bin/gettext '	Is %s a slave of %s?	%s'`\n" $server_fqdn $feed $is_slave
    else
      printf "`/usr/bin/gettext '	Feed:			<none>'`\n"
    fi
    printf "`/usr/bin/gettext '	Storage location:	%s'`\n" $spool_place
    printf "`/usr/bin/gettext '	State location:		%s'`\n" $state_place
    echo
    print -n `/usr/bin/gettext "Are these correct ? (y/n): "`
    read ans
    echo
    case "$ans" in
	[yY]*)	return 0 ;;
	*)	return 1 ;;
    esac
}


simple_config()
{
    typeset ans

    while true
    do
        print `/usr/bin/gettext "Please provide the following information:"`
	print " "
	print `/usr/bin/gettext "Fully-qualified hostname of this News server:"`
	print -n `/usr/bin/gettext "(for example, news.isp.net): "`
	read server_fqdn
	print -n `/usr/bin/gettext "Name of your group/organization: "`
	read organization
	print -n `/usr/bin/gettext "Fully-qualified hostname of the feed []: "`
	read feed
	feed="`echo $feed`"
	is_slave=""
	if [ -n "$feed" ]; then
	  is_slave=`ckyorn -Q -p "Is this News server a slave to $feed ? "`
	fi
	printf "`/usr/bin/gettext 'News Storage location [%s]: '`" $default_spool_place
	read ans
	spool_place=${ans:-$default_spool_place}
	printf "`/usr/bin/gettext 'News state location [%s]: '`" $default_state_place
	read ans
	state_place=${ans:-$default_state_place}
	confirm && break
    done
    log_place=$default_log_place
}

#
# Process any options
#
while getopts q c 2>/dev/null
do
	case "$c" in
	    q)	verbose=NO ;;
	    *)	printf "`/usr/bin/gettext 'Usage: %s [-q]'`\n"  $script_name; exit 2 ;;
	esac
done
shift $(($OPTIND-1))

echo " "
echo `/usr/bin/gettext "Ready to setup Sun(TM) Internet News Server(TM)."`
echo "----------------------------"
simple_config

# Test whether $server_fqdn is a valid hostname and domain-name
# If not, exit with fail status
#
specifiedlabels=$(echo $server_fqdn | awk -F. '{ print NF }')
if test $specifiedlabels -gt 1 ; then
    specifiedserver=$(echo $server_fqdn | cut -d. -f 1)
    specifieddomain=$(expr $server_fqdn : "$specifiedserver\.\(.*\)")
fi

# This is commented out because it won't work in DNS world and it is a bogus check.
#thisdomainname=`domainname`
#thisserver=$(echo $thisdomainname | cut -d. -f 1)
#thisdomain=$(expr $thisdomainname : "$thisserver\.\(.*\)")
#if [ "$thisdomain" = "$specifieddomain" ]; then
#  echo "Domain name matches that of this host"
#  echo "Domainname of this host is: " $thisdomain
#  echo "Domainname specified in hostname server is: " $specifieddomain
  # Now ping the machine, just in case there was a typo in server name
  #
  ping $server_fqdn > /dev/null 2>&1
  if [ $? -eq 0 ] ; then
    echo "Server is up"
  else
    echo "Server not accessible in this domain: " $server_fqdn
    exit 9
  fi
#else
#  echo "Domain name mismatch with current host"
#  echo "Domainname of this host is: " $thisdomain
#  echo "Domainname specified in hostname is: " $specifieddomain
#  echo "Please specify correct fully-qualified host name and retry"
#  exit 9
#fi

makedir $root
# The following should be created later otherwise mkas will fail in case of non default news storage setup.
#makedir $LOCKS 
makedir $(dirname $NNTPCONNECT)

#
# Create the directory structure
#
{
    test $verbose = YES && printf "`/usr/bin/gettext '%s: creating storage directory'`\n" $script_name
    if [ ! -d $spool_place ]; then
	printf "`/usr/bin/gettext  '%s:  must exist. Create the directory\n'`" $spool_place
	exit 8
    fi
    $mkdir_cmd $spool_place/articles `dirname $SPOOL`
    mkas $spool_place/articles $SPOOL
} &&
{
    test $verbose = YES && printf "`/usr/bin/gettext '%s: creating state directory'`\n" $script_name
    if [ ! -d $state_place ]; then
	printf "`/usr/bin/gettext  '%s:  must exist. Create the directory\n'`" $state_place
	exit 8
    fi
    mkas $state_place $root/state
} || exit $?
if [ "$this_config" = "RF" ]; then
  {
    test $verbose = YES && printf "`/usr/bin/gettext '%s: creating reader state directory'`\n" $script_name
    mkas $root/rstate $root/rstate
  } || exit $?
fi

{
  test $verbose = YES && printf "`/usr/bin/gettext '%s: creating log directory'`\n" $script_name
  mkas $log_place $root/logs	
} || exit $?

makedir $LOCKS
#
# Create the News "database"
#
{
    test $verbose = YES && printf "`/usr/bin/gettext '%s: configuring storage directory'`\n" $script_name
    mkspool $spool_place/articles
} &&
{
    test $verbose = YES && printf "`/usr/bin/gettext  '%s: configuring state directory'`\n" $script_name
    mkstate $state_place
} &&
{
    test $verbose = YES && printf "`/usr/bin/gettext  '%s: setting up active file'`\n" $script_name
    mkactive "$feed" $state_place
} &&
{
    test $verbose = YES && printf "`/usr/bin/gettext  '%s: configuring logs'`\n" $script_name
    mklogs
} || exit $?

#
# Now create the actual configuration
#
# SNS TODO: create separate log directories for reader and feeder
#
test $verbose = YES && printf "`/usr/bin/gettext '%s: installing configuration files'`\n" $script_name
if [ ! -d $NEWSCONF ]; then
  $cp_cmd -r $NEWSGOLDENCONF $NEWSCONF
else
  $cp_cmd -r $NEWSGOLDENCONF/* $NEWSCONF
fi

if [ "$this_config" = "RF" ]; then
  test $verbose = YES && printf "`/usr/bin/gettext '%s: installing reader configuration files'`\n" $script_name

  if [ ! -d $NEWSRCONF ]; then
    $cp_cmd -r $NEWSGOLDENRCONF $NEWSRCONF
  else
    $cp_cmd -r $NEWSGOLDENRCONF/* $NEWSRCONF
  fi

fi


#
# Usage:  setup_crontab filename
#
# Setup default crontab entry for news:
#	news.daily
#	nntpsend
#
setup_crontab()
{
    typeset ctab=$NEWSCRONTAB

    $cp_cmd /dev/null $ctab
    set -o noglob
    echo "12,32,52 * * * * $NEWSSBIN/nntpsend" >> $ctab
    echo "5 9,15,21 * * * $NEWSSBIN/news.daily delayrm expireover norenumber nomail nologs >/dev/null 2>&1" >> $ctab
    echo "5 3 * * * $NEWSSBIN/news.daily delayrm expireover" >> $ctab
    set +o noglob

    set_access $ctab
}


#
# Usage:
#	innconf_append label value
#
innconf_append()
{
    typeset label=$1
    typeset value=$2

    if test -z "$value" ; then
	printf "`/usr/bin/gettext '%s: you will need to set %s in %s'`\n" $script_name $label $INNCONF
	ready_to_go=NO
    else
	echo "$label:		$value" >> $INNCONF
    fi
}

labels=$(echo $server_fqdn | awk -F. '{ print NF }')
if test $labels -gt 1 ; then
    server=$(echo $server_fqdn | cut -d. -f 1)
    domain=$(expr $server_fqdn : "$server\.\(.*\)")
    fromhost=$server_fqdn
fi
$chmod_cmd u+w $INNCONF
innconf_append fromhost "$fromhost"
innconf_append organization "$organization"
innconf_append server "$server"
innconf_append domain "$domain"
$chmod_cmd u-w $INNCONF

$chmod_cmd u+w $HOSTSNNTP
echo "localhost:" >> $HOSTSNNTP
$chmod_cmd u-w $HOSTSNNTP
 
setup_crontab

if test "X$feed" != "X" ; then
    test $verbose = YES && printf "`/usr/bin/gettext '%s: adding feed: %s'`\n" $script_name $feed
    feed add $feed
    if [ "X$is_slave" = "Xy" -o "X$is_slave" = "XY" ]; then
      ${NEWSSETUP}/slave_setup -e $feed
    fi
else
    printf "`/usr/bin/gettext '%s: You will need to add your feed(s) manually.'`\n" $script_name
    echo "------"
    printf "`/usr/bin/gettext '  Step 1: add entry to $NEWSFEEDS'`\n" $NEWSFEEDS
    printf "`/usr/bin/gettext '  Step 2: add entry to $NEWSCONF/nntpsend.ctl'`\n" $NEWSCONF/nntpsend.ctl
    printf "`/usr/bin/gettext '  Step 3: add entry to $NEWSCONF/hosts.nntp'`\n" $NEWSCONF/hosts.nntp
    echo "------"
    ready_to_go=NO
fi

if test $ready_to_go = YES ; then
    echo
    printf "`/usr/bin/gettext ' %s: You can now start the News system by typing:'`\n" $script_name
    echo "		/etc/init.d/snsnews start"
    echo
    exit 0
else 
  echo 
  printf "`/usr/bin/gettext ' %s: Setup failure'`" $script_name
  exit 1
fi

