#! /bin/csh
#
#	$Header: configure,v 4.300 91/06/09 00:59:49 root Rel41 $
#	Created January 1989 HTM
#	Further Documentation added Febuary 1989, HTM, WPC
#	Copyright 1990, Performance Systems International Inc.
#	Copyright 1989, NYSERNet, Inc.
#	Copyright 1990, Sony Corp.
#	All Rights Reserved.
#
#	SNMP auto configure script.
#	This script is to help the systems person to install the SNMP
#	Report Generation package. Often, this may be on a different
#	machine, or in a different directory than the other SNMP tool
#	sources.
#
#	This script will set up environment variables for the scripts
#	that generate reports, and will allow the minor tweaking of the
#	configuration of the report generator.
#
#
#
onintr leave

set config=../config/report-env.NEW
set snmpawk=../crontab/snmp.awk
############################### Opening  #########################
cat	<< END

	NYSERNet SNMPStats Configuration Script.
	
	This script will set up environment variables for the scripts
	that generate reports, and will allow the minor tweaking of the
	configuration of the report generator.

	This script will create a new report-env file in
	$config and it will update $snmpawk

	The old report-env will not be destroyed unless you choose to replace
	it at the end of this script.

	Some modifications are done by Sony Corporation for NEWS-OS.

	Enter a <Control-C> to terminate the script.

END

/bin/rm -f $config

cat	<< END > $config
#! /bin/csh
#
# STOP STOP STOP STOP STOP STOP STOP STOP STOP STOP STOP
#
# this file was set up during installation
# NEVER change it except by re-installing the package
#
#

set path=(/usr/ucb /bin /usr/bin /usr/etc /usr/local)
END
############################### Report ROOT  #########################

echo	"set HOST=`hostname`" >> $config
echo	"#	under which directory the report generater is located" >> $config

set temp = `pwd`
set temp = $temp:h
cat	<< END

	The current directory of the SNMP Report Generator is
	$temp

	Do you want to change this location (y/n) [default no]?
END
echo -n "  -->	"
set ans = $<
switch ($ans)
  case y:
  case Y:
	echo	""
	echo	"	Enter the full path name of the directory."
	echo -n	"  -->	"
	set ans = $<
	if ($ans == "") then
	  echo	"$0 : No path name. Exit"
	  /bin/rm -f $config
	  exit(1)
	else
	  echo	"set ROOT=$ans" >> $config
	  set ROOT=$ans
	endif
	breaksw
  default:
	echo	"set ROOT=$temp" >> $config
	set ROOT=$temp
	breaksw
endsw
############################### SNMP application BIN  #########################

echo	"#	where all the SNMP applications are" >> $config
echo	""
echo	"	Enter the path name of where the SNMP applications (snmppoll,snmptrapd)"
echo	"	are located."
echo -n	"  -->	"
set ans = $<
if ($ans == "") then
  echo	" $0 : No path name. Exit"
  /bin/rm -f $config
  exit(1)
else
  set	SNMP="$ans"
  echo	"set SNMP=$ans" >> $config
endif

echo	'set CONFIG=$ROOT/config' >> $config
############################### mail to OPERATER   #########################

echo	"#	whoever wants to know what happens" >> $config

cat << END

	The SNMP Report Generation package is set-up to send status messages
	to anyone who is interested. If there is a person, or mailing list
	where these status messages may be sent, enter below.

	Enter recipients (comma separated) or  RETURN if none.
END
echo -n	"  -->	"
set ans = $<
if ($ans == "") then
  echo	"unset OPERATER" >> $config
else 
  echo	"set OPERATER=$ans" >> $config
  set	OPERATER="$ans"
endif

############################### Polled Data  #########################
echo	"#	where the data generated by snmppoll goes" >> $config

echo	""
echo	"	Pathname of where the data gathered by snmppoll is stored ?"
echo 	"	(Default: $ROOT/data/today)"
echo -n	"  -->	"
set ans = $<
if ($ans == "") then
  echo	'set POLLDATASRC=$ROOT/data/today' >> $config
  set POLLDATASRC=$ROOT/data/today
else 
  echo	"set POLLDATASRC=$ans" >> $config
  set POLLDATASRC=$ans
endif

############################### Saved polled Data  #########################
echo	"#	where the compressed data goes" >> $config
echo	""
echo	"	What is the directory where the compressed data after daily statistics"
echo	"	will be stored ?  (default: $ROOT/data)"
echo -n	"  -->	"
set ans = $<
if ($ans == "") then
  echo	'set RAWDATASRC=$ROOT/data' >> $config
  set RAWDATASRC=$ROOT/data
else 
  echo	"set RAWDATASRC=$ans" >> $config
  set RAWDATASRC=$ans
endif
############################### Disk Usage Messages  #########################

echo	"#	We may want to know the disk usage by the report generater" >> $config
cat	<< END

	It may take a large amount of disk space to store the compressed data
	mentioned in the question above.

	Would you liked to be informed of disk usage by the report generator ?
END
echo -n " -- (y|n) [default no] -->	"
set ans = $<
switch ($ans)
  case y:
  case Y:
        echo    "Enter the full path name of the partition"
        echo    "the compressed data is located on."
        echo -n "--> "
        set ans = $<
        if ($ans == "") then
          echo  "No path name. Disk space statistics will not be performed."
          echo 'unset PARTITION' >> $config
        else
          echo "set PARTITION=$ans" >> $config
        endif
        breaksw
  default:
	echo  "	Disk space statistics will not be performed."
	echo 'unset PARTITION' >> $config
	breaksw
endsw
############################### DO WEEKLY  #########################

echo	"#	I want the weekly report (set) or not (unset)" >> $config
echo	""
echo	"	Do you want to do weekly statistics ?	(y/n)[default yes]"
echo -n	"  -->	"
set ans = $<
switch ($ans)
  case y:
  case Y:
  case "":
        echo	"set DOWEEKLY" >> $config
	set DOWEEKLY
	echo	"#	The day I want the weekly report generated very week." >> $config
	echo	""
	echo	"	Which day do you want weekly statistics done (choose a number) ?"
	echo	"		0 : Sunday (Default)"
	echo	"		1 : Monday"
	echo	"		2 : Tuesday"
	echo	"		3 : Wednesday"
	echo	"		4 : Thursday"
	echo	"		5 : Friday"
	echo	"		6 : Saturday"
	echo	 -n "   -->	"
	set ans = $<
	if ($ans == "") then
	  echo	"set GENWEEKLY = 0" >> $config
	else
	  echo	"set GENWEEKLY = $ans" >> $config
	endif
        breaksw
  default:
	echo	"unset DOWEEKLY" >> $config
	unset DOWEEKLY
        endif
endsw
############################### Keeping Data  #########################

echo	"#	how many days of compressed data kept in RAWDATASRC" >> $config

echo	""
echo	"	How many days of compressed data need to be kept in"
echo	"	$RAWDATASRC"

if ($?DOWEEKLY) then
  echo	-n " --	( 7 [default] or more )  -->	"
else 
  echo	-n " --	( 0 or more)[default: 7] -->	"
endif

set ans = $<
if ($?DOWEEKLY) then
  if ($ans == "") then
    echo	"@ RECORDAGE = 7" >> $config
  else
    if ($ans < 7) then
      echo	"@ RECORDAGE = 7" >> $config
    else
      echo	"@ RECORDAGE = $ans" >> $config
    endif
  endif
else
  if ($ans == "") then
    echo	"@ RECORDAGE = 7" >> $config
  else
    echo	"@ RECORDAGE = $ans" >> $config
  endif
endif
####################### Where daily reports go ########################

echo	"#	Where the daily report should be stored" >> $config

echo    ""
echo    "	Where do you want to store the daily reports generated?"
echo 	"	( Default $ROOT/daily/report )"
echo -n	"  -->	"

set ans = $<

if ($ans == "") then
  echo 'set DAILYREPORTS=$ROOT/daily/report' >> $config
  set DAILYREPORTS=$ROOT/daily/report
else
  echo "set DAILYREPORTS=$ans" >> $config
  set DAILYREPORTS=$ans
endif

####################### Where weekly reports go ########################

echo	"#	Where the weekly report should be stored" >> $config

if (! $?DOWEEKLY) then
  echo 'unset WEEKLYREPORTS' >> $config
else 
  echo	""
  echo	"	Where do you want to store the weekly reports generated?"
  echo  "	( Default $ROOT/weekly/report )"
  echo -n "  --> 	"

  set ans = $<

  if ($ans == "") then
    echo 'set WEEKLYREPORTS=$ROOT/weekly/report' >> $config
    set WEEKLYREPORTS=$ROOT/weekly/report
  else
    echo "set WEEKLYREPORTS=$ans" >> $config
    set WEEKLYREPORTS=$ans
  endif
endif
	
############################### SNMPPOLL.CF  #########################

echo	"#	where is the snmppoll.cf file" >> $config

echo	""
echo	"	Where is the snmppoll.cf located ?"
echo	"	Default: $ROOT/config"
echo -n	"  -->	"

set ans = $<
if ($ans == "") then
  echo	'set SNMPPOLLCF=$ROOT/config' >> $config
  set SNMPPOLLCF=$ROOT/config
else
  echo	"set SNMPPOLLCF=$ans" >> $config
  set SNMPPOLLCF=$ans
endif

############################### SNMPTRAPD.CF  #########################
echo	"#	where is the snmptrapd.cf file" >> $config

echo	""
echo	"	Where is the snmptrapd.cf located ?"
echo	"	Default: $ROOT/config"
echo	 -n "  -->	"

set ans = $<
if ($ans == "") then
  echo	'set SNMPTRAPDCF=$ROOT/config' >> $config
  set SNMPTRAPDCF=$ROOT/config
else
  echo	"set SNMPTRAPDCF=$ans" >> $config
  set SNMPTRAPDCF=$ans
endif
############################### SNMPTRAP LOG FILE  #########################

echo	"#	the full path name and file name of snmptrapd log file" >> $config

echo	""
echo	"	What is the full path name and file name of snmptrapd log file ?"
echo	"	Default: $ROOT/data/snmptrapd.log"
echo	"	 (not under data/today subdirectory)"
echo	 -n "  -->	"

set ans = $<
if ($ans == "") then
  echo	'set TRAPLOG=$ROOT/data/snmptrapd.log' >> $config
  set TRAPLOG=$ROOT/data/snmptrapd.log
else
  echo	"set TRAPLOG=$ans" >> $config
  set TRAPLOG=$ans
endif

###############################  DAILY Copies  #########################

echo	"	You can make an extra copy of the reports by copying them to"
echo	"	some place else or you can move them to some place else."

echo	"#	user may want to copy or move the daily reports somewhere else" >> $config

echo	""
echo	"	Do you want to copy or move the DAILY reports to somewhere else ?"
echo 	"	(Type 'c' for copy, 'm' for move and RETURN if not needed.)"
echo -n	"  -->	"

set ans = $<
if ($ans != "") then
  if ($ans != "c" && $ans != "m") then
    unset mc
  else
    set mc = $ans
  endif
else 
  unset mc
endif

if ($?mc) then
  echo	"Enter the path:"
  echo	 -n "-->	"
  set ans = $<
  if ($ans == "") then
    echo	"unset MVDAILYRPT" >> $config
    echo	"unset CPDAILYRPT" >> $config
    echo	"#	where to copy or move the daily reports" >> $config
    echo	"unset DAILYREPORTDIR" >> $config
  else
    if ($mc == 'c') then
      echo	"set CPDAILYRPT" >> $config
      echo	"unset MVDAILYRPT" >> $config
    else
      echo	"unset CPDAILYRPT" >> $config
      echo	"set MVDAILYRPT" >> $config
    endif
    echo	"#	where to copy or move the daily reports" >> $config
    echo	"set DAILYREPORTDIR=$ans" >> $config
  endif
else
  echo	"unset MVDAILYRPT" >> $config
  echo	"unset CPDAILYRPT" >> $config
  echo	"#	where to copy or move the daily reports" >> $config
  echo	"unset DAILYREPORTDIR" >> $config
endif

###############################  WEEKLY Copies  #########################

echo	"#	user may want to copy or move the weekly reports somewhere else" >> $config

echo	""
echo	"	Do you want to copy or move the WEEKLY reports to somewhere else ?"
echo	"	(Type 'c' for copy, 'm' for move and RETURN if not needed.)"
echo -n	"  -->	"

set ans = $<
if ($ans != "") then
  if ($ans != "c" && $ans != "m") then
    unset mc
  else
    set mc = $ans
  endif
else 
  unset mc
endif

if ($?mc) then
  echo	"Enter the path:"
  echo	 -n "-->	"
  set ans = $<
  if ($ans == "") then
    echo	"unset MVWEEKLYRPT" >> $config
    echo	"unset CPWEEKLYRPT" >> $config
    echo	"#	where to copy or move the weekly reports" >> $config
    echo	"unset WEEKLYREPORTDIR" >> $config
  else
    if ($mc == 'c') then
      echo	"set CPWEEKLYRPT" >> $config
      echo	"unset MVWEEKLYRPT" >> $config
    else
      echo	"unset CPWEEKLYRPT" >> $config
      echo	"set MVWEEKLYRPT" >> $config
    endif
    echo	"#	where to copy or move the weekly reports" >> $config
    echo	"set WEEKLYREPORTDIR=$ans" >> $config
  endif
else
  echo	"unset MVWEEKLYRPT" >> $config
  echo	"unset CPWEEKLYRPT" >> $config
  echo	"#	where to copy or move the weekly reports" >> $config
  echo	"unset WEEKLYREPORTDIR" >> $config
endif

echo	'set path=($path $ROOT/daily/bin $ROOT/weekly/bin)' >> $config

############################# FLATDIR structure #################
#	this could be coded to a dual level directory structure, by 
#	removing the following line.
echo	'set FLATDIR' >> $config
#	a single level directory structure with unique interface names
#	is strongly suggested.
############################# graphs (depends on FLATDIR) #################

echo	"#	generate printer ready plots of daily data" >> $config

echo	""
echo 	"	Do you want to do generate simple graphs?  (y/n)[default no]"
echo -n	"  -->	"

set ans = $<
switch ($ans)
  case y:
  case Y:
	echo	"set GRAPHDAILY" >> $config
	set	GRAPHDAILY
        breaksw
  default:
	echo	"unset GRAPHDAILY" >> $config
	unset GRAPHDAILY
	breaksw
endsw
  
############################# display diff of config's #################
if ( -e $config:r ) then
  echo	"	Following is a diff of the report-env.NEW you have just created,"
  echo	"	and the previous report-env."
  echo	""
  echo	"diff $config $config:r"
  diff $config $config:r
endif

############################# accept/reject new script #######################

cat	<< END

	If you leave the script at this time, without installing the
	$config as $config:r, then
	nothing will be changed.

	If you wish to replace the current $config:r with the one you
	have just created, then enter 'yes' below.
END
echo -n	"  -->	"
set	ans=$<

if ($ans != "yes") then		#### reject
  echo	"	You have selected not to install this config script."
  echo	"	If you wish to look at the script you have just rejected,"
  echo	"	it may be found in $config"
  echo	""
  echo	"	goodbye".
  exit
endif				#### accept

echo	"	Moving $config to $config:r"
if ( -e $config:r)	mv	$config:r $config:r.bak
mv	$config	$config:r
echo	"	$config:r has been updated."

set	snmpstats=$ROOT/crontab/snmpstats
set	snmpgraph=$ROOT/plot/bin/snmpgraph
set	snmpxgraph=$ROOT/plot/bin/snmpxgraph
set	HOST=`hostname`
if (! $?OPERATER) then
  set OPERATER = ""
endif
set	OPERATER=(`echo $OPERATER | sed 's/\//\\\//g'`)
set	ROOT2=$ROOT
set	ROOT=(`echo $ROOT | sed 's/\//\\\//g'`)
set	SNMP2=$SNMP
set	SNMP=(`echo $SNMP | sed 's/\//\\\//g'`)
set	RAWDATASRC2=$RAWDATASRC
set	RAWDATASRC=(`echo $RAWDATASRC | sed 's/\//\\\//g'`)
set	SNMPPOLLCF2=(`echo $SNMPPOLLCF | sed 's/\//\\\//g'`)
set	SNMPTRAPDCF2=(`echo $SNMPTRAPDCF | sed 's/\//\\\//g'`)

if (-e $snmpstats)	mv	$snmpstats	$snmpstats.bak

sed	"s/ReportRoot/$ROOT/" $snmpstats.template >! $snmpstats
chmod	755	$snmpstats

if (-e $snmpawk)	mv	$snmpawk	$snmpawk.bak

sed	"s/SNMPBIN/$SNMP/" $snmpawk.template | \
sed	"s/HOST/$HOST/"	| \
sed	"s/ROOT/$ROOT/"	| \
sed	"s/SNMPPOLLCF/$SNMPPOLLCF2/" | \
sed	"s/SNMPTRAPDCF/$SNMPTRAPDCF2/" | \
sed	"s/OPERATER/$OPERATER/"	>! $snmpawk

if (-e $snmpgraph)	mv	$snmpgraph	$snmpgraph.bak

sed	"s/ROOT/$ROOT/" $snmpgraph.template | \
sed	"s/RAWDATASRC/$RAWDATASRC/" >! $snmpgraph
chmod	755	$snmpgraph

if (-e $snmpxgraph)	mv	$snmpxgraph	$snmpxgraph.bak

sed	"s/ROOT/$ROOT/" $snmpxgraph.template | \
sed	"s/RAWDATASRC/$RAWDATASRC/" >! $snmpxgraph
chmod	755	$snmpxgraph

############## to check if some important directoris exist ############

cat << ENDD
	Going to check to see if all the directory names entered exist.
	If any of them does not exist, that corresponding directory must
	be created or the report generater will not run properly.
ENDD
echo -n " Press RETURN to continue --> "
set ans=$<

set dirlist=($ROOT2 $SNMP2 $POLLDATASRC $RAWDATASRC2 $RAWDATASRC2/plot $DAILYREPORTS $WEEKLYREPORTS $SNMPPOLLCF $SNMPTRAPDCF $TRAPLOG:h)

while ($#dirlist != 0)
  if (! -e $dirlist[1]) then
    echo "Warning : $dirlist[1] does not exist."
  endif
  shift dirlist
end

if ($?WEEKLYREPORTDIR) then
  if (! -e $WEEKLYREPORTDIR) then
    echo "Warning : $WEEKLYREPORTDIR does not exist."
  endif
endif

if ($?DAILYREPORTDIR) then
  if (! -e $DAILYREPORTDIR) then
    echo "Warning : $DAILYREPORTDIR does not exist."
  endif
endif

echo "	If there are any directories missing as mentioned above."
echo "	Check to see if the input was incorrect and create the missing"
echo "	directories if needed before running the report generater."
echo -n " Press RETURN to continue --> "
set ans = $<

############################# some text info for operator #################
cat	<< END

	If this an original installation of SNMP Report Generation,
	then you will have to do additional work to install SNMP data collection
	programs on your machine.

	The following is a list of things which should be done:

	1)  The tools (echo, dateformat, in1period, prsdate, topspr) in ../src
	    should be moved into both weekly/bin and daily/bin.

	2)  Place (move) plot/bin/{snmpgraph,snmpxgraph} in the bin 
	    (/usr/sony/bin) directory.

	3)  Set up the configuration file for snmptrapd
	    (../config/snmptrapd.cf)

	4)  Set up the configuration file for snmppoll
	    (../config/snmppoll.cf, see ../config/gen_poll)

	5)  Edit /etc/rc.local, and install code to start snmppoll
	    and snmptrapd while booting.
	    (see ../config/rc.local-example)

	6)  Edit /etc/crontab, and install entries to restart snmppoll
	    and snmptrapd, in case they should crash .
	    (see ../config/crontab-example)

	7)  Invoke snmppoll and snmptrapd.

Note:	Check if snmppoll and snmptrapd have the write access to the
	directories where these two processes are supposed to put data in.

END
exit

leave:
echo	"Interrupted. Exit."
/bin/rm $config
exit
