#!/bin/bash
#
# connect : Used for Remote console connection from RP Host
#
# Dec 2014, Jisu 
#
# Copyright (c) 2015 by Cisco Systems, Inc.
# All rights reserved.
#

if [ -z "$1" ]
  then
    echo "Usage: .attach_console <slot number>"
    exit
fi

# Steps 
# 1. Check master ship of RP by reading 0xe8000330
# 2. If master RP the proceed to write 
# 3. If salve RP exit with Error message 

# Check Master ship 
MASTER_REG=`iorw r 0xe8000330 w | awk '{print $2}'`
if [ $? -ne 0 ]; then
    echo ""
    echo "ERROR: Failed to read RP Mastership register"
    exit 1
fi

ACTIVE_BIT=`echo ${MASTER_REG:0:1}`
if [ $? -ne 0 ]; then
    echo "ERROR: Failed interpreting RP Mastership status"
    exit 1
fi

if [ "$ACTIVE_BIT" == "1" ]; then
    echo "MASTER-RP:Proceding to remote console of Slot: $1"
else
    echo "SLAVE-RP: Please run Remote console connection from Master RP"
    exit 1
fi

# If MASTER RP connect to console of Remote card 
iorw w 0xe80000fc `printf "0x80%02x0000" $1` w

echo "Escape character is '^x' [control+x]"
microcom -s 115200 /dev/ttyS1 
echo "Exited"
