#!/bin/bash
# INTEL CONFIDENTIAL
# Copyright (c) 2008-2012  Intel Corporation All Rights Reserved.
# The source code contained or described herein and all documents related
# to the source code ("Material") are owned by Intel Corporation or its
# suppliers or licensors. Title to the Material remains with Intel Corporation
# or its suppliers and licensors. The Material contains trade secrets and
# proprietary and confidential information of Intel or its suppliers and
# licensors. The Material is protected by worldwide copyright and trade
# secret laws and treaty provisions. No part of the Material may be used,
# copied, reproduced, modified, published, uploaded, posted, transmitted,
# distributed, or disclosed in any way without Intel's prior express
# written permission.
#
# No license under any patent, copyright, trade secret or other intellectual
# property right is granted to or conferred upon you by disclosure or delivery
# of the Materials, either expressly, by implication, inducement, estoppel or
# otherwise. Any license under such intellectual property rights must be
# express and approved by Intel in writing.

# [ICS VERSION STRING: @(#) ./bin/xedge_capture 7_4_3_0_2 [01/26/18 09:02]

Usage()
{
	echo "Usage: xedge_capture -t node_guid [-v] [-h hca] [-o port] [-K|-w passwd] outfile" >&2
	echo "      -t node_guid - switch to capture" >&2
	echo "      -v - verbose output" >&2
	echo "      -h - hca to use for path to fabric" >&2
	echo "      -o - hca port to use for path to fabric" >&2
	echo "      -K - enforce password based security for all operations - prompts for password" >&2
	echo "      -w - enforce password based security for all operations - password on command line" >&2
	echo "      outfile - file to produce with dump" >&2
	exit 2
}

# convert $1 to an absolute path
fix_dir()
{
	if [ x$(echo $1|cut -c1) != x"/" ]
	then
		echo "$PWD/$1"
	else
		echo "$1"
	fi
}

# arguments used in expect code below must be exported
export guid=
export verbose=n
hca=1
port=0
export passwd=""
get_passwd=n
while getopts t:vh:o:Kw: param
do
	case $param in
	t)  export guid="$OPTARG";;
	v)  export verbose=y;;
	h)  hca="$OPTARG";;
	o)  port="$OPTARG";;
	K)	get_passwd=y;;
	w)	export passwd="$OPTARG";;
	*)  Usage;;
	esac
done
shift $(($OPTIND -1))
if [ $# -ne 1 ]
then
	Usage
fi
# convert file to an absolute path because we cd below
export outfile=$(fix_dir "$1")

if [ x"$guid" = x ]
then
	echo "xedge_capture: -t option is required" 2>&1
	Usage
fi

# resolve hca and port to a hca name and physical port consistent with
# how other FastFabric tools would do it using oib_utils:oib_get_portguid
hca_port=$(iba_resolve_hca_port $hca $port)
if [ "$?" -ne 0 ]
then
	echo "xedge_capture: Invalid HCA and Port specified: $hca:$port" >&2
	Usage
fi
hca=$(echo "$hca_port"|cut -f1 -d:)
port=$(echo "$hca_port"|cut -f2 -d:)
export hca_port_arg="hca $hca sport $port"

export tooldir=/opt/iba/ib_tools
export LD_LIBRARY_PATH="$tooldir:$LD_LIBRARY_PATH"

if [ $get_passwd = "y" ]
then
	echo -n "Enter password:" > /dev/tty
	stty -echo < /dev/tty > /dev/tty
	passwd=
	read passwd < /dev/tty
	stty echo < /dev/tty > /dev/tty
	echo > /dev/tty
	export passwd
fi
if [ x"$passwd" = "x" ]
then
	export passwd_arg=
else
	export passwd_arg="vkey *"
fi
rm -f $outfile


# Use expect to start an L8simlnx session and request it to connect to
# the specified switch and perform a dump
expect -c '
	proc verbose_print { message } {
		global env
		if { [ string equal "$env(verbose)" "y" ] } {
			puts stdout "$message"
		}
	}

	global env
	if { [ string equal "$env(verbose)" "n" ] } {
		# disable output to stdout of expect transactions
		log_user 0
	}

	verbose_print "Starting $env(tooldir)/L8simlnx"

	# L8simlnx must be run in this directory so it can find its .so
	cd $env(tooldir)

	if { [ catch { spawn -noecho $env(tooldir)/L8simlnx } res ] != 0 } {
		puts stderr "Unable to start $env(tooldir)/L8simlnx"
		exit 1
	}

	expect_before {
	{failed} {
				puts stderr "failure during $expecting"
				exit 1
			}
	{aborted} {
				puts stderr "failure during $expecting"
				exit 1
			}
	{Unrecognized} {
				puts stderr "failure during $expecting"
				exit 1
			}
	}

	expect_after {
	timeout {
				puts stderr "timeout waiting for $expecting"
				exit 1
			}
	eof {
				puts stderr "EOF waiting for $expecting"
				exit 1
			}
	}
		
	set timeout 120
	set expecting "L8simlnx startup"
	expect {
	{s20->} {
				send "connect ib $env(hca_port_arg) dguid $env(guid) $env(passwd_arg)\n"
			}
	}

	if { ! [ string equal "$env(passwd_arg)" "" ] } {
		set timeout 120
		set expecting "password prompt"
		expect {
		{Enter vendor key} { send "$env(passwd)\n" }
		}
	}


	set timeout 120
	set expecting "connect to $env(guid)"
	expect {
	{s20->} {
			send "dump all gzip file \"$env(outfile)\"\n"
			}
	}

	set timeout 600
	set expecting "dump of $env(guid)"
	expect {
	{completed} { puts stdout "dumped $env(guid) to $env(outfile)" }
	}

	set timeout 120
	set expecting "prompt"
	expect {
	{s20->} { send "quit\n" }
	}

	# wait for it to properly exit
	set timeout 120
	set expecting "exit"
	expect {
	eof { }
	}
	exit 0
'

if [ "$?" -ne 0 ]
then
	echo "xedge_capture: failed to capture $guid" >&2
	exit 1
fi
exit 0
