#!/bin/bash
#
# This script sets variables for migrating from ASR9K IOS XR 64 Bit (eXR) to IOS XR 32 Bit (classic XR)
#
# Copyright (c) 2014-2017 by Cisco Systems, Inc.
# All rights reserved.

source /etc/init.d/set_emt_value_lib.sh

if [[ ($# -eq 0) || ($# -eq 1 && ("$1" == "-h" || "$1" == "--help")) ]]; then
	printf "\n"
	echo "Usage: $0 [-b|--boot eusb|tftp]"
	echo "                            [-a|--address ipv4_address]"
	echo "                            [-n|--netmask ipv4_subnet_mask]"
	echo "                            [-g|--gw ipv4_default_gateway]"
	echo "                            [-s|--server tftp_server_ip]"
	echo "                            [-p|--path tftp_file_path_for_the_image]"
	echo "                            [-m|--mgmt rps/rp_bootup_slot]"
	echo "                            [-r|--reboot ]"
	printf "                            [-h|--help]\n\n"
	printf "Two options for booting the ASR9K IOS XR 32 Bit (classic XR) image:\n\n"
	printf "1. Boot the image from the eUSB. Copy the image to eUSB before running this script.\n"	
	printf "Example: $0 -b eusb -m RSP0 -r\n\n"
	printf "2. Boot the image from external tftp server:\n"
	printf "Example: $0 -b tftp -a 1.66.27.123 -n 255.255.0.0 -g 1.66.0.1 -s 223.255.254.245 -p /auto/tftpboot/asr9k-mini-px.vm-5.3.3 -m RSP0 -r\n\n"
	printf "* IMPORTANT NOTE: Please back up the admin and IOS XR configurations before reloading the device.\n\n"

	exit 0
fi


# Get the number of RSP/RP
num_rp=$(/opt/cisco/calvados/bin/canbus_client -s1 -c6 | grep -c "slot.*[0-9]*.*:.*RS*P")

# Get the internal IP's for RSP0/RP0 and RSP1/RP1 
rp0_admin_ip=$(grep rsp0_admin /etc/hosts | awk '{print $1}')
rp1_admin_ip=$(grep rsp1_admin /etc/hosts | awk '{print $1}')

# If two RSP/RP's, both internal IP's should be available
if [[ (num_rp -eq 2) ]]; then
 
	if [[ -z "${rp0_admin_ip}" ]]; then
		error_exit "Error: Failed to get internal IP address for RSP0/RP0 from /etc/hosts."
	fi

	if [[ -z "${rp1_admin_ip}" ]]; then
		error_exit "Error: Failed to get internal IP address for RSP1/RP1 from /etc/hosts."
	fi
fi


temp_args="$@"
NUM_ARGS=$#
while [ $# -gt 0 ]; do
    case "$1" in
        -r|--reboot)
            shift
            REBOOT_AUTOMATICALLY=1
            ;;
        -m|--bootup_slot)
            shift
            if [[ "$1" == "RP0" || "$1" == "RSP0" || "$1" == "Rp0" || "$1" == "Rsp0" || "$1" == "rp0" || "$1" == "rsp0" ]]; then
                BOOT_RP=0;
            elif [[ "$1" == "RP1" || "$1" == "RSP1" || "$1" == "Rp1" || "$1" == "Rsp1" || "$1" == "rp1" || "$1" == "rsp1" ]]; then
                BOOT_RP=1;
            else
		        error_exit "Error: Invalid RP in bootup_slot ."
            fi
            shift
            ;;
		*)
            shift
            ;;
	esac
done


function execute_script_on_rps()
{
	local script="$1"

	if [[ num_rp -eq 1 ]]; then
		echo "No standby RSP/RP detected."
		$script $temp_args
	elif [[ num_rp -eq 2 ]]; then

        if [[ "$BOOT_RP" == "0" ]]; then
            echo "Executing the migration script on RSP0/RP0..."
            ssh $rp0_admin_ip "$script $temp_args && exit"
        else 
            # Update reboot mode to turbo boot IOS XR 32 Bit (emt mode 9)
            ssh $rp0_admin_ip "/etc/init.d/set_emt_value_lib.sh && exit"
            echo "Setting boot mode on RSP0/RP0."
        fi

		if [[ $? -ne 0 ]]; then
            exit 1
        fi    

        if [[ "$BOOT_RP" == "1" ]]; then
            echo "Executing the migration script on RSP1/RP1..."
            ssh $rp1_admin_ip "$script $temp_args && exit"
        else 
            # Update reboot mode to turbo boot IOS XR 32 Bit (emt mode 9)
            ssh $rp1_admin_ip "/etc/init.d/set_emt_value_lib.sh && exit"
            echo "Setting boot mode on RSP1/RP1."
        fi

		if [[ $? -ne 0 ]]; then
            exit 1
        fi    
	else
		error_exit "Error: Detected unexpected number of RSP/RP's - $num_rp."
	fi
}

# 4 or 5 arguments only valid for migration via eUSB
if [[ $NUM_ARGS -eq 4 ||  $NUM_ARGS -eq 5 ]]; then
    roption="none"
    bootoption="none"
    eusboption="none"
    
    for str in $temp_args
    do
        if [[ ("$str" == "-b") ]]; then
            bootoption=$str
        elif [[ ("$str" == "-r") ]]; then
            roption=$str
        elif [[ ("$str" == "--boot") ]]; then
            bootoption=$str
        elif [[ ("$str" == "eusb") ]]; then
            eusboption=$str
        fi
    done
    
    if [[ (("$bootoption" == "-b") || ("$bootoption" == "--boot")) && ("$eusboption" == "eusb") ]]; then
		# Check if classic XR image is in eUSB 

		IMAGE=$(find /eusbb | grep -m 1 "asr9k-mini-px.vm.*")
        	if [ -z "${IMAGE}" ]; then
                    error_exit "Error: Missing an ASR9K IOS XR 32 Bit image in harddiskb:. Note: the filename of the image must match wildcard expression asr9k-mini-px.vm*"
		fi
		execute_script_on_rps "/etc/init.d/setclassic.sh" 
			
	else
		error_exit "Error: Invalid input."	
	fi
    
# 14 or 15 arguments only valid for migration via TFTP
elif [[ $NUM_ARGS -eq 14 ||  $NUM_ARGS -eq 15 ]]; then
	execute_script_on_rps "/etc/init.d/setrommon.sh"
else
	error_exit "Error: Incorrect number of input."
fi


if [[ "$REBOOT_AUTOMATICALLY" == "1" ]]; then
echo "Rebooting Automatically"
/opt/cisco/calvados/sbin/reimage_chassis_asr9k -r <<EOI
y
EOI
fi

echo "Reload to boot IOS XR 32 Bit image."
echo "*** IMPORTANT *** Please back up your admin and XR configurations before reloading."
