#!/bin/ksh

#
# Copyright (c) 1996 by Sun Microsystems, Inc. All Rights Reserved.
#
# @(#)reader_setup 1.12 98/09/04
#
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)

print `/usr/bin/gettext "                  Reader only configuration"`
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"
    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

##  =()<opt_dir=@<_PATH_COMP_ROOT>@>()=
opt_dir=/opt/SUNWsns

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

##  =()<. @<_PATH_NEWSLIB>@/innshellvars>()=
. /etc/opt/SUNWsns/innshellvars
##  =()<. @<_PATH_NEWSBIN>@/setup/mkaccess.shf>()=
. /opt/SUNWsns/bin/setup/mkaccess.shf

INNCONF=$NEWSCONF/inn.conf
HOSTSNNTP=$NEWSCONF/hosts.nntp

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

#
# 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_config_place=$NEWSCONF
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
}

#
# Usage: mount_remotefs dirpath hostname file_in_dirpath
mount_remotefs()
{
  actfs=`df -k $1 | fgrep -v Filesystem | awk '{ print $1 }'`
  remhost=`echo "$actfs" | cut -d':' -f1`
  if [ -z "$remhost" ]; then
    printf "`/usr/bin/gettext '%s: %s is not mounted remotely.'`\n" $script_name $1
    return 1
  else
    if [ "$remhost" != "$2" ]; then
      printf "`/usr/bin/gettext '%s: %s is not mounted from %s.'`\n" $script_name $1 $2
      return 1
    else
      if [ ! -r $1/$3 ]; then
	printf "`/usr/bin/gettext '%s: %s does not contain %s.'`\n" $script_name $1 $3
	return 1
      fi
    fi
  fi
  return 0
}


confirm()
{
    typeset ans

    printf "`/usr/bin/gettext 'Your choices were:'`\n"
    printf "`/usr/bin/gettext '	Feeder Hostname:	%s'`\n" $feeder_host
    printf "`/usr/bin/gettext '	Feeder Storage location: %s'`\n" $spool_place
    printf "`/usr/bin/gettext '	Feeder State location:	%s'`\n" $state_place
    printf "`/usr/bin/gettext '	Feeder Config location:	%s'`\n" $config_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 -n `/usr/bin/gettext "Hostname of the Feeder: "`
	read feeder_host
	printf "`/usr/bin/gettext '  IMPORTANT: Before proceeding, please make the\n  following directory locations of %s\n  available to this host (using share(1m)).'`\n" $feeder_host

	printf "`/usr/bin/gettext 'Feeder storage location [%s]: '`" $default_spool_place
	read ans
	spool_place=${ans:-$default_spool_place}
	printf "`/usr/bin/gettext 'Feeder state location [%s]: '`" $default_state_place
	read ans
	state_place=${ans:-$default_state_place}
	printf "`/usr/bin/gettext 'Feeder config location [%s]: '`" $default_config_place
	read ans
	config_place=${ans:-$default_config_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 News(TM) Reader-only configuration."`
echo "------------------------------------------------------"
simple_config

makedir $root

#
# Mount remote directories and create others
#

if  [  $verbose = YES ] ; then
  printf "`/usr/bin/gettext '%s: verifying remote storage directory'`\n" $script_name
fi
mntfs $default_spool_place $feeder_host $spool_place "ro" articles
mntStatus=$?
#echo "In reader_setup, mntStatus is: " $mntStatus
if [ $mntStatus -ne 0 ] ; then
  if [ $mntStatus -ne 33 ] ; then
    umount $default_spool_place
    rmdir $default_spool_place
  fi
  exit $mntStatus
fi


if [  $verbose = YES ] ; then
  printf "`/usr/bin/gettext '%s: verifying remote state directory'`\n" $script_name
fi
mntfs $default_state_place $feeder_host $state_place "ro" active
mntStatus=$?
#echo "In reader_setup: mntStatus is : " $mntStatus
if [ $mntStatus -ne 0 ] ; then
  if [ $mntStatus -ne 33 ] ; then
    umount $default_state_place
    rmdir $default_state_place
  fi
  exit $mntStatus
fi

if  [ $verbose = YES ] ; then
  printf "`/usr/bin/gettext '%s: verifying remote config directory'`\n" $script_name
fi
mntfs $default_config_place $feeder_host $config_place "ro" inn.conf 
mntStatus=$?
if [ $mntStatus -ne 0 ] ; then
  if [ $mntStatus -ne 33 ] ; then
    umount $default_config_place
    rmdir $default_config_place
  fi
  exit $mntStatus
fi


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


#
# Now create the actual configuration
#
{
  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

} || exit $?


#
# Store mount points in config file for reader.server to mount/umount
#
mntpt_cfg=$NEWSRCONF/mountpoints.conf
if [ -r $mntpt_cfg ]; then
  rm -f $mntpt_cfg
fi
printf "`/usr/bin/gettext '%s: writing remote feed dir locations to %s'`\n" $script_name $mntpt_cfg
touch $mntpt_cfg &&
{
  echo $default_spool_place $feeder_host $spool_place "ro" articles >> $mntpt_cfg
  echo $default_state_place $feeder_host $state_place "ro" active >> $mntpt_cfg
  echo $default_config_place $feeder_host $config_place "ro" inn.conf >> $mntpt_cfg
}


# Store feed_host name in config file for reader.server to use as
# cmd-line parameter.
feedinfo_cfg=$NEWSRCONF/feedinfo.conf
if [ -r $feedinfo_cfg ]; then
  rm -f $feedinfo_cfg
fi
{
  printf "`/usr/bin/gettext '%s: writing feeder host name to %s'`\n" $script_name $feedinfo_cfg
  echo $feeder_host > $feedinfo_cfg
} || exit $?

printf "`/usr/bin/gettext '** Be sure to add this host to hosts.nntp on %s to enable posting.  **'`\n"  $feeder_host

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 `/usr/bin/gettext "		/etc/init.d/news.server start"`
    echo
    exit 0
else
  echo
  printf "`/usr/bin/gettext '%s: Setup failure'" $script_name
  exit 1
fi

