#!/usr/bin/ksh
# @(#)81        1.7  src/bos/usr/bin/mirror/cfgmir.sh, cmdmirror, bos720 4/23/01 11:13:45
#
# COMPONENT_NAME:  CMDMIRROR: Console mirroring
#
# NAME:        cfgmir.sh
#
# FUNCTIONS: Active or desactive the mirroring during the boot in "service" mode 
#
# SYNTAX:        cfgmir ON|OFF
#
# CALLED BY:     srvboot
#
# ORIGINS: 83
#
#  LEVEL 1, 5 Years Bull Confidential Information
#

PATH=/usr/bin:/etc:/usr/sbin:/sbin

umask 077

CFG_FILE=/usr/share/modems/mir_old_cfg  # file name to save variables values

#
# bootinfo -T is not support on all platforms.
#
MACHINE=`bootinfo -T 2>/dev/null`

case $1 in 
  ON)
    if [ "$MACHINE" = "rs6ksmp" ]
    then
	# save state and configure device attach to port s2 
	PARENT_S2=`odmget -q "connwhere=s2a AND PdDvLn LIKE adapter/sio/*" CuDv|grep name|cut -f 2 -d\"`
	if [ "$PARENT_S2" = "" ] 
	then
		exit
	fi
	PARENT_S2_STATUS=`lsdev -C -l $PARENT_S2 -F status`
	if [ "$PARENT_S2_STATUS" = "Defined" ] 
	then
		mkdev -l $PARENT_S2 >/dev/null
	fi
	TTY_S2=`odmget -q "parent=$PARENT_S2" CuDv|grep name|cut -f 2 -d\"`
	if [ "$TTY_S2" = "" ]
	then
		mkdev -c tty -s rs232 -t tty -p $PARENT_S2 -w s2 >/dev/null
		TTY_S2=`odmget -q "parent=$PARENT_S2" CuDv|grep name|cut -f 2 -d\"`
		TTY_S2_STATUS=""
	else
		TTY_S2_STATUS=`lsdev -C -l $TTY_S2 -F status`
		if [ "$TTY_S2_STATUS" = "Defined" ]
		then
			mkdev -l $TTY_S2 >/dev/null
		fi
	fi
	portmir -c $TTY_S2 >/dev/null
    else
	PORTMIR_MONITOR=$(portmir -q)
	if [[ "$PORTMIR_MONITOR" = "" ]]
	then
		exit 0
	fi

	# save state and configure device attached to portmir_monitor port

	# get device associated w/ portmir_monitor attribute
	TTY_MON=`odmget -q "attribute=portmir_monitor" CuAt | 
		 grep name | cut -f 2 -d\"`

	# if none exists then exit
	if [[ "$TTY_MON" = "" ]]
	then
		exit
	fi

	# get parent of portmir_monitor device
	PARENT_MON=`odmget -q "name=$TTY_MON" CuDv | grep parent | 
		    cut -f 2 -d \"`

	# keep getting parent device until parent device is a bus 
	# device or sio device
	print $PARENT_MON | egrep "bus|sio" > /dev/null 2>&1
	done="$?"
	while [[ "$done" != "0" ]]
	do
		ADAPTER_MON="$PARENT_MON"
		PARENT_MON_LIST="$PARENT_MON $PARENT_MON_LIST"
		PARENT_MON=`odmget -q "name=$PARENT_MON" CuDv | grep parent | 
		            cut -f 2 -d \"`
		print $PARENT_MON | egrep "bus|sio" > /dev/null 2>&1
		done="$?"
	done

	for p in $PARENT_MON_LIST
	do
		PARENT_MON_STATUS=`lsdev -C -l $p -F status`
		if [ "$PARENT_MON_STATUS" = "Defined" ] 
		then
			mkdev -l $p > /dev/null
		fi
	done

	mkdev -l $TTY_MON > /dev/null
    fi

    # start the mirror daemon if it exists
    # mirrord is a script that calls /usr/sbin/portmir -s mir_modem
    [ -x /usr/sbin/mirrord ] && /usr/sbin/mirrord mir_modem

    # save variables for mirroring OFF 
    echo >$CFG_FILE		# reset the CFG_FILE
    echo "PARENT_S2 $PARENT_S2" >>$CFG_FILE
    echo "TTY_S2 $TTY_S2" >>$CFG_FILE
    echo "PARENT_S2_STATUS $PARENT_S2_STATUS" >>$CFG_FILE
    echo "TTY_S2_STATUS $TTY_S2_STATUS" >>$CFG_FILE
    echo "ADAPTER_MON $ADAPTER_MON" >> $CFG_FILE

    ;;
  OFF)
    # kill the mirror daemon 
    MIR_PID=`ps -ef | grep portmir | grep -v grep | cut -c10-16`
    if [ "$MIR_PID" != "" ]
    then
	kill -15 $MIR_PID

        # wait (with timeout) the end of portmir
        for i in 1 2 3 4 5 6
        do
                if ps -ef | grep portmir | grep -v grep >/dev/null
                then
                        sleep 1
                else
                        break;
                fi
        done
    fi

    # restore variables values 
    if [ ! -r $CFG_FILE ]
    then
    	exit
    fi
    PARENT_S2=` awk '/PARENT_S2 / { print $2 }' $CFG_FILE `
    TTY_S2=` awk '/TTY_S2 / { print $2 }' $CFG_FILE `
    PARENT_S2_STATUS=` awk '/PARENT_S2_STATUS/ { print $2 }' $CFG_FILE `
    TTY_S2_STATUS=` awk '/TTY_S2_STATUS/ { print $2 }' $CFG_FILE `
    ADAPTER_MON=` awk '/ADAPTER_MON/ { print $2 }' $CFG_FILE `
    
    rm -f $CFG_FILE

    if [ "$ADAPTER_MON" != "" ]
    then
    	rmdev -l $ADAPTER_MON -R >/dev/null
    	exit 0
    fi

    # restore TTY_s2 status before the mirroring

    if [ "$TTY_S2" != "" ]
    then
    	if [ "$TTY_S2_STATUS" = "" ] 
    	then
    		rmdev -l $TTY_S2 -d >/dev/null
    	else
    		if [ "$TTY_S2_STATUS" = "Defined" ]
    		then
    			rmdev -l $TTY_S2 >/dev/null
    		fi
    	fi
    fi
	
    # restore PARENT_S2 status before the mirroring
    if [ "$PARENT_S2" != "" ]
    then
    	if [ "$PARENT_S2_STATUS" = "Defined" ]
    	then
    		rmdev -l $PARENT_S2 >/dev/null
    	fi
    fi
    ;;
esac
