#!/bin/bash
#
# Configures a zfcp lun.
#
# The hardware description should look like
# <type>-<bus>-<devid>
# e.g.
# 'hwup-zfcp lun-zfcp-0.0.f900'
# 

SCRIPTNAME=${0##*/}
HWDESC=$1

# Read in common functions
. ./scripts/functions
test -r ./config && . ./config

# Check for variables
if test -z "$SYSFS"; then
    message "sysfs not mounted, aborting."
    exit 1
fi

if [ -z "$HWD_DEVICEPATH" ]; then
    message "no device path given, aborting."
    exit 1
fi

# Read in the configuration file
. ./hwcfg-${HWDESC}

# Set sysfs paths
_zfcp_dir=${HWD_DEVICEPATH}

# Check whether a device ID was given
if [ -z "$ZFCP_LUNS" ]; then
    # Nothing to configure
    message "$SCRIPTNAME: No zfcp luns available for configuration"
    exit 1;
fi

# Check whether the adapter is available
if [ ! -d "$_zfcp_dir" ]; then
    message "$SCRIPTNAME: zfcp adapter ${HWD_BUSID} is not available"
    exit 1
fi

# Check whether the adapter is configured
_zfcp_online=$(cat "${_zfcp_dir}/online")
if [ "$_zfcp_online" = "0" ]; then
    message "$SCRIPTNAME: zfcp adapter ${HWD_BUSID} is not configured"
    exit 1
fi

# Configure all wwpn/lun settings
for _zfcp_dev in $ZFCP_LUNS; do
    _zfcp_wwpn=`echo $_zfcp_dev | sed -n 's/:.*//p'`
    _zfcp_lun=`echo $_zfcp_dev | sed -n 's/.*://p'`

    # Check whether the wwpn is already configured
    _zfcp_wwpn_dir="${_zfcp_dir}/${_zfcp_wwpn}"
    if [ ! -d "${_zfcp_wwpn_dir}" ]; then
	echo "$_zfcp_wwpn" > ${_zfcp_dir}/port_add
    fi
    # Re-check whether it exists now
    if [ ! -d "${_zfcp_wwpn_dir}" ] ; then
	message "$SCRIPTNAME: No WWPN ${_zfcp_wwpn} for zfcp adapter ${HWD_BUSID}"
    else
        # Check and configure the zfcp-lun
	if [ ! -d "${_zfcp_wwpn_dir}/${_zfcp_lun}" ] ; then
	    echo "$_zfcp_lun" > ${_zfcp_wwpn_dir}/unit_add
	fi
    fi
done

# EOF
