#!/bin/sh
# easyddr.sh
#
# A wrapper for scsimgr to help with DDR management
#
# (c) 2009 Olivier S. Masse, omasse ~at~ mayoxide ~dot~ com
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#
#
#
version="@(#) v0.1 2009/02/10"

tempfile=$(mktemp -c)

function debug
{
	echo "$*"
}


function get_ddr_names
{
	typeset instance_num

	echo "Gathering possible DDR names..."
	ioscan -kFNd esdisk | cut -d ":" -f 13 | while read instance_num
	do
		scsimgr ddr_name -C disk -I ${instance_num} pid | tail -1
	done | sort -u > ${tempfile}

	cat ${tempfile}
	echo
}

function register_ddr_names
{
	typeset ddr_name
	typeset wildcard
	typeset crap

	# 1. get a list of all registered DDRs
	echo "Registering DDR names if needed..."
	ddrlist=$(mktemp -c)
	scsimgr ddr_list > ${ddrlist}

	# 2. get a list of DDRs we want to register by reading the config file, grabbing wildcards
	grep -vE "^#" ${configfile} | while IFS=":" read wildcard crap
	do
		# 2a. Check if there's a wildcard specifying our DDR
		grep -E "${wildcard}" ${tempfile} | while read ddr_name
		do
			# 2a1. There is one. Check if it's registered.
			debug "Wildcard='${wildcard}' DDR='${ddr_name}':"
			grep -qE "${ddr_name}" ${ddrlist}
			if [ $? -ne 0 ]
			then
				# 2a1a. It's not registered. Register it.
				debug "   -> not in DDR list. Registering."
				if [ ${apply} -eq 1 ]
				then
					eval "yes | scsimgr ddr_add -N ${ddr_name}"
				else
					echo "        PREVIEW: yes | scsimgr ddr_add -N ${ddr_name}"
				fi
			else
				# 2a1b. It's already registered.
				debug "   -> already in DDR list."
			fi
		done	
	done

	[ -f ${ddrlist} ] && rm ${ddrlist}
	echo
}


function set_ddr_params
{
	typeset wildcard
	typeset parameters
	typeset param
	typeset ddr_name
	
	echo "Applying parameters to wildcarded DDRs..."
	
	grep -vE "^#" ${configfile} | while IFS=":" read wildcard parameters
	do
		grep -E "${wildcard}" ${tempfile} | while read ddr_name
		do
			debug "Wildcard='${wildcard}' DDR='${ddr_name}' parameters='${parameters}':"
			echo "${ddr_name}:"
			for param in ${parameters}
			do
				echo "   -> ${param}"
				if [ ${apply} -eq 1 ]
				then
					eval "scsimgr set_attr -N ${ddr_name} -a ${param}"
					eval "scsimgr save_attr -N ${ddr_name} -a ${param}"
				else
					echo "        PREVIEW: scsimgr set_attr -N ${ddr_name} -a ${param}"
					echo "        PREVIEW: scsimgr save_attr -N ${ddr_name} -a ${param}"
				fi
			done
		done
	done
	echo
}

function usage
{
	echo
	echo ${version}
	echo
	echo "Usage: $(basename $0) config_file [-apply]"
	echo
	exit 1
}

[ "$1" = "" ] && usage || configfile="$1"
[ "$2" = "-apply" ] && apply=1 || apply=0

if [ ! -f ${configfile} ]
then
	echo "File '${configfile}' missing"
fi

if [ ${apply} -eq 0 ]
then
	echo
	echo "Preview mode. Use -apply to apply changes."
	echo
fi

get_ddr_names
register_ddr_names
set_ddr_params

[ -f ${tempfile} ] && rm ${tempfile}

