#!/bin/csh -f
#
# A script to install the VMEIO software on a TMC CMIOP system.
# Don't run this on anything other than a CMIOP!
#

# ----------------------------------------------------------------------------
set path=(/bin /usr/bin /usr/ucb /usr/local /usr/local/etc /etc /usr/etc .)
set DEBUG=""
set home = `pwd`
set date = `date`
set whoiam = `whoami`

alias BackUpFile 'set now=`date`; if ( ! -f \!*%TMC%${now[2]}_${now[3]}_${now[6]}) sh -c "${DEBUG} mv \!* \!*%TMC%${now[2]}_${now[3]}_${now[6]} && echo \!* saved as \!*%TMC%${now[2]}_${now[3]}_${now[6]}"'
alias CopyBackUpFile 'set now=`date`; if ( ! -f \!*%TMC%${now[2]}_${now[3]}_${now[6]}) sh -c "${DEBUG} cp \!* \!*%TMC%${now[2]}_${now[3]}_${now[6]} && echo \!* saved as \!*%TMC%${now[2]}_${now[3]}_${now[6]}"'

echo ""
echo "Updating /etc/rc.local"
CopyBackUpFile /etc/rc.local

if { grep -si 'connection.*machine' /etc/rc.local } then
	echo "Connection machine daemons already in /etc/rc.local"
else
	echo "Adding Connection Machine daemons to /etc/rc.local"
	cat >>/etc/rc.local <<EoFk
#
# Connection Machine daemons
#
(echo -n 'Connection machine daemons:')                          >/dev/console
(echo '.')                                                        >/dev/console
EoFk
endif

if ( -d /home/dv ) then
  mv /home/dv /home/cmfs
  if ( { grep -s /home/dv /etc/rc.local } ) then
    ed - /etc/rc.local >/dev/null << EoFb
/home\/dv/
s/\/dv/\/cmfs/
w
q
EoFb
  endif
endif

mkdir /home/cmfs >&/dev/null
if (! { grep -s fsserver /etc/rc.local } ) then
	ed - /etc/rc.local >/dev/null << EoFa
/[Cc]onnection [Mm]achine [Dd]aemons:/
a
[ -f /usr/local/etc/fsserver ] && {
	/usr/local/etc/fsserver -p 1 -c 512 /home/cmfs &
	(echo -n ' fsserver') >/dev/console
}
.
w
q
EoFa
endif

if (! { grep -s cm-logger /etc/rc.local } ) then
	ed - /etc/rc.local >/dev/null << EoFh
/[Cc]onnection [Mm]achine [Dd]aemons:/
a
[ -f /usr/local/etc/cm-logger ] && {
	/usr/local/etc/cm-logger -a central.acct_log -e central.error_log -c fatal ; (echo -n ' cm-logger') >/dev/console
}
.
w
q
EoFh
else
	ed - /etc/rc.local >/dev/null << EoFh
/\/usr\/local\/etc\/cm-logger[ ;][; ]/
s/\/usr\/local\/etc\/cm-logger/\/usr\/local\/etc\/cm-logger -a central.acct_log -e central.error_log -c fatal/
w
q
EoFh
endif

if { grep -s 'vioconfig' /etc/rc.local } then
	echo "vioconfig already in /etc/rc.local"
else
	echo "Adding vioconfig to /etc/rc.local"
	cat >>/etc/rc.local <<EofL
#
# Configure VMEIO interface
#
[ -f /usr/local/etc/vioconfig ] && {
	/usr/local/etc/vioconfig -b BUS-ID -t STATION-ID -x 2 >/dev/console 2>&1
}
EofL
endif

# Install log rotation in crontab; default is weekly rotation of files (at
# one minute past midnight).
# Change all crontab lines below as follows if you want monthy log files:
# replace "1 0 * * 7" with "1 0 1 * *"
#
cd /
echo "Updating crontab"
setenv VISUAL ex
setenv EDITOR ex
set CRONEDIT = "crontab -e root"
set CRONCAT =  "crontab -l root"
CopyBackUpFile /usr/spool/cron/crontabs/root

${DEBUG} ${CRONCAT} >/tmp/crontab$$

if (! { grep -s newcmlog /tmp/crontab$$ } ) then
   ${DEBUG} ${CRONEDIT} >/dev/null << EoFh
\$a
# Crontab entries added by CMSS installation, $date
# Roll CM log files
1 0 * * 7 /usr/local/etc/newcmlog >/usr/spool/cm/newcmlog.out 2>&1
.
w
q
EoFh
else
   ${DEBUG} ${CRONEDIT} >/dev/null << EoFh
1
/newcmlog/s;/[A-Za-z0-9/]*/newcmlog;/usr/local/etc/newcmlog;
1
i
# Crontab entries modified by CMSS installation script $date
.
w
q
EoFh
endif

# Install cm-logger and cm-logger-conn services in services database.  If a
# site is running Yellow Pages, these edits must be made on the appropriate
# machine for YP Service to take effect.
#
echo "Updating services"
egrep -s 'cm-logger[	 ]*8243/udp' /etc/services # correct service already there?
if ($status != 0) then				# no,
	egrep -s '8243/udp' /etc/services	# port taken by another service?
	if ($status != 0) then			# no,
		CopyBackUpFile /etc/services
		egrep -s 'cm-logger[	 ]*' /etc/services # logger using other port?
		if ($status != 0) then		# no, insert logger service
			echo "  Adding cm-logger to /etc/services ..."
			ed - /etc/services >/dev/null << EoFh
\$a
cm-logger	8243/udp			# system-wide logging daemon
.
w
q
EoFh
		else				# yes, change logger service
			ed - /etc/services >/dev/null << EoFh
/cm-logger/
c
cm-logger	8243/udp			# system-wide logging daemon
.
w
q
EoFh
		endif

	else					# port 8243 already taken
		echo ""
		echo "ERROR: cm-logger service not installed in /etc/services."
		echo "UDP port 8243 is already in use as follows:"
		egrep '8243/udp' /etc/services
		echo "See the cm-logger installation guide for instructions"
		echo "on how to install the cm-logger service manually."
		echo "Note that you must do this on all system components."
		echo ""
	endif
endif

echo "  Adding cm-logger-conn to /etc/services ..."
egrep -s 'cm-logger-conn[ 	]*8243/tcp' /etc/services # correct svc already there?
if ($status != 0) then                          # no,
        egrep -s '8243/tcp' /etc/services       # port taken by another service?
        if ($status != 0) then                  # no,
                CopyBackUpFile /etc/services
                egrep -s 'cm-logger-conn[ 	]*' /etc/services # logger using other port?
                if ($status != 0) then          # no, insert logger service
                        ed - /etc/services >/dev/null << EoFh
\$a
cm-logger-conn	8243/tcp			# system-wide logging daemon
.
w
q
EoFh
                else                            # yes, change logger service
                        ed - /etc/services >/dev/null << EoFh
/cm-logger-conn/
c
cm-logger-conn	8243/tcp			# system-wide logging daemon
.
w
q
EoFh
                endif

        else                                    # port 8243 already taken
                echo ""
                echo "ERROR: cm-logger-conn service not installed in /etc/services."
                echo "TCP port 8243 is already in use as follows:"
                egrep '8243/tcp' /etc/services
                echo "See the cm-logger installation guide for instructions"
                echo "on how to install the cm-logger service manually."
                echo "Note that you must do this on all system components."
                echo ""
        endif
endif

set ypwhich=`which ypwhich`
if ($ypwhich[1] != no) then
    ypwhich -m services.byname >& /tmp/yp$$
    set YPMASTER=`cat /tmp/yp$$`
    rm -f /tmp/yp$$
    if ($YPMASTER[1] != ypwhich:) then
	echo "You are running the Yellow Pages service"
	if ( { (ypcat services.byname | egrep -s 'cm-logger.*8243/udp') } ) then
	    echo "  The cm-logger service is correctly installed in the Yellow Pages"
	else
	    echo " "
   	    echo "*** You must add the cm-logger service to the "
	    echo "*** master services database on the "
	    echo "*** YP master server $YPMASTER before"
	    echo "*** the CM error and accounting logging system will work."
	    echo " "	
	endif
        if ( { (ypcat services.byname | egrep -s 'cm-logger-conn.*8243/tcp') } ) then
            echo "  The cm-logger-conn service is correctly installed in the Yellow Pages"
        else
            echo " "
            echo "*** You must add the cm-logger-conn service to the "
            echo "*** master services database on the "
            echo "*** YP master server $YPMASTER before"
            echo "*** the CM error and accounting logging system will work."
            echo " "
        endif
    endif
endif

wait
