####################################################################
# (C) Copyright 2012-2015 Hewlett-Packard Development Company, L.P.
# @(#) Metrocluster 3PARRC CLI passwordfile Script.
# @(#) Product Name                :  HP Metrocluster 3PARRC
# @(#) Product Version             :  B.12.00.30
#
# *** Note: This file MUST NOT be edited. *****
##################################################################

USER=$1
SYS=$2

if [[ -e /etc/redhat-release ]]; then
	SG_PATH="/usr/local/cmcluster"
else
	SG_PATH="/opt/cmcluster";
fi

echo "Metrocluster 3PAR password file creation utility...."
echo "The user name specified is ${USER}"
echo "The storage array specified is ${SYS}"

echo "Enter the username once more and the password when prompted"

if [[ $# -ne 2 ]]
then
	echo "Usage:setupMC3PARpasswdfile <username> <storagearray IP or DNS Name> "
	exit 1
fi

echo "Trying to create the password file"

mkdir -p ${SG_PATH}/conf/3parrc


cli  -sys $SYS setpassword -file ${SG_PATH}/conf/3parrc/${USER}_${SYS}.pwf -saveonly

if [[ $? -ne 0 ]];then
	echo "Failed to create the password file!!!"
	echo "Create the password file and copy it manually to all nodes in the location ${SG_PATH}/conf/3parrc"
	echo "NOTE: The password file must of format <3parArrayUserName>_<3parArrayName>.pwf"
	exit 1
fi
echo "Created the passwordfile"

echo "Copying the password file to all nodes in the cluster"
nodes=`cmviewcl -s config -f line | grep node | grep name  | awk -F [=] '{print \$2}'`

if [[ $? -ne 0 ]];then
	echo "Failed to get the cluster nodes list!!"
	echo "Unable to copy the password file"
	echo "Note: Copy the password files in the location ${SG_PATH}/conf/3parrc to all nodes in the cluster"
	echo "Note: The password file must be present only under ${SG_PATH}/conf/3parrc in all nodes"
	exit 1
fi

NODE=""
ERROR=0

for node in ${nodes}
do
	cmexec ${node} mkdir -p ${SG_PATH}/conf/3parrc
	cmcp ${SG_PATH}/conf/3parrc/${USER}_${SYS}.pwf ${node}:${SG_PATH}/conf/3parrc/${USER}_${SYS}.pwf
	if [[ $? -ne 0 ]]; then
		NODE="${NODE} ${node}"
		ERROR=1
	fi
done

if [[ $ERROR -ne 0 ]];then
    echo "Copy failed to the nodes ${NODE}"
else
    echo "Successfully copied password files"
fi



