#!/bin/bash
#
# slotr : Used for Reload Remote cards from RP Host
#
# Dec 2014, Jisu 
#
# Copyright (c) 2015 by Cisco Systems, Inc.
# All rights reserved.
#
if [ -z "$1" ]
  then
    echo "Usage: slotr <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: Proceeding to Reload  Slot: $1"
else
    echo "SLAVE-RP: Please run Reload utility from Master RP"
    exit 1
fi

# If MASTER RP reload the slot
iorw w 0xe80000fc `printf "0x80%02x00ec" $1` w
