#!/bin/sh

# $Header: vmmedia.sh,v 1.4 97/10/23 11:44:18 birk Exp $
#bcpyrght
#***************************************************************************
#* Copyright 1993 - 1998 VERITAS Software Corporation, All Rights Reserved *
#***************************************************************************
#ecpyrght

#
# vmmedia -- change media type and robot type corresponding to selected media
#            in the volume database
#
# Notes on vmmedia:
#	- Changes volume database on current host, unless alternate host provided.
#	- Changes media_type and robot_type only for specified media if media_id 
#		provided
#	- Changes media_type and robot_type only for media corresponding to 
#		old media_type, old robot_type, and robot_number, if provided.
#
# General Algrorithm:
#	1. parse command line options
#	2. call vmquery to obtain list of volumes to change
#	3. call vmchange to make the selected changes

VMCHANGE=/usr/openv/volmgr/bin/vmchange
VMQUERY=/usr/openv/volmgr/bin/vmquery

##
# GENERAL script functions
##

# -----------------------------------------------------------------------------
# Determine if provided type IS a cleaning media type
#
# 	is_cleaning_type <media_type>
#
#	Returns 0 for yes and 1 for no.

is_cleaning_type ()
{
	for t in $CLEAN_MEDIA_TYPES
	do
		if [ "$t$clean_suffix" = "$1" ]
		then
			return 0
		fi
	done
	return 1
}
# -----------------------------------------------------------------------------
# Determine if provided type HAS an associated cleaning media type
#
# 	is_cleaning_type <media_type>
#
#	Returns 0 for yes and 1 for no.

has_cleaning_type ()
{
	for t in $CLEAN_MEDIA_TYPES
	do
		if [ "$t" = "$1" ]
		then
			return 0
		fi
	done
	return 1
}
# -----------------------------------------------------------------------------
# Determine if provided media type is valid
#
# 	is_valid_media_type <media_type>
#
#	Returns 0 for yes and 1 for no.

is_valid_media_type ()
{
	for t in $VALID_MEDIA_TYPES
	do
		if [ "$t" = "$1" ]
		then
			return 0
		fi
	done
	return 1
}
# -----------------------------------------------------------------------------
# Determine if provided robot type is valid
#
# 	is_valid_robot_type <robot_type>
#
#	Returns 0 for yes and 1 for no.

is_valid_robot_type ()
{
	for t in $VALID_ROBOT_TYPES
	do
		if [ "$t" = "$1" ]
		then
			return 0
		fi
	done
	return 1
}
# -----------------------------------------------------------------------------
# Cleanup temp files 
#
# 	cleanup

cleanup ()
{
	rm -f $query_M
	rm -f $query_MT
	rm -f $query_MT_Sorted
	rm -f $query_RT
	rm -f $query_RT_Sorted
	rm -f $query_RN
	rm -f $query_RN_Sorted
	rm -f $query_C
	rm -f $query_C_Sorted
	rm -f $common_RT_MT
	rm -f $common_RT_MT_RN
	rm -f $common_RT_C
	rm -f $common_RT_C_RN
	rm -f $vmchange_err

	return 0
}
# -----------------------------------------------------------------------------
# MAIN / main starts here

# list all types here... let VM infrastructure handle illegals
VALID_MEDIA_TYPES="odiskwm odiskwo t120 8mm 8mm_clean \
  hcart hreel qcart 4mm 4mm_clean dlt dlt_clean hcart_clean \
  hcart2 hcart2_clean dlt2 dlt2_clean 8mm2 8mm2_clean d2 d2_clean \
  dtf dtf_clean site2 site2_clean"
VALID_ROBOT_TYPES="none acs odl tc4 tc8 tl4 tl8 tld tlh tlm ts8 tsd tsh vtl"

#
# Define media types that have associated cleaning media
#   ( implies that if the media type is XXX,
#     there is a media type of XXX_CLN )
#

CLEAN_MEDIA_TYPES='4mm 8mm 8mm2 d2 dlt dlt2 hcart hcart2 dtf site2'
clean_suffix_display='_CLN'	# as indicated in vmquery brief output
clean_suffix_input='_clean'	# for vmquery input media type

progname='vmmedia'

if [ -f /usr/openv/volmgr/version ]
then
	HARDWARE=`head -1 /usr/openv/volmgr/version | cut -f2 -d" "`
else
	echo "$progname: /usr/openv/volmgr/version not found"
	exit 1
fi

case "$HARDWARE" in
    SUN4)
	ECHO="/usr/5bin/echo"
	;;
    SOLARIS)
        ECHO="/bin/echo"
        ;;
    SGI)
        ECHO="/bin/echo"
        ;;
    HP9000-700)
        ECHO="/bin/echo"
        ;;
    HP9000-800)
        ECHO="/bin/echo"
        ;;
    RS6000)
        ECHO="/bin/echo"
        ;;
    ALPHA)
	ECHO="/bin/echo"
	;;
    NCR)
	ECHO="/bin/echo"
	;;
    OLIVETTI)
	ECHO="/bin/echo"
	;;
    SEQUENT)
	ECHO="/bin/echo"
	;;
    PYRAMID)
	ECHO="/bin/echo"
	;;
    *)
        echo "$progname: Unsupported server type $HARDWARE"
        exit 3
        ;;
esac



#
# Temp files used for storing query results
#

query_M="/tmp/vmm_M.$$"			# for media
query_MT="/tmp/vmm_MT.$$"		# for media type
query_MT_Sorted="/tmp/vmm_MT_Sorted.$$"	# for media type - sorted
query_RT="/tmp/vmm_RT.$$"		# for robot type
query_RT_Sorted="/tmp/vmm_RT_Sorted.$$"	# for robot type - sorted
query_RN="/tmp/vmm_RN.$$"		# for robot number
query_RN_Sorted="/tmp/vmm_RN_Sorted.$$"	# for robot number - sorted
query_C="/tmp/vmm_C.$$"			# for cleaning media type
query_C_Sorted="/tmp/vmm_C_Sorted.$$"	# for cleaning media type - sorted
common_RT_MT="/tmp/vmm_Common_RT_MT.$$"	# for RT/MT matches
common_RT_MT_RN="/tmp/vmm_Common_RT_MT_RN.$$"	# for RT/MT/RN matches
common_RT_C="/tmp/vmm_Common_RT_C.$$"	# for RT/C matches
common_RT_C_RN="/tmp/vmm_Common_RT_C_RN.$$"	# for RT/C/RN matches
vmchange_err="/tmp/vmm_vmchange_err.$$"	# for vmchange errors

#
# List the currently "supported" media types in the usage, which
# will probably differ from valid types above
#

Usage="$progname:
        -h <hostname>	(default is this host)
	-v		(verbose; default is silent)
        change single medium:
           -new_mt <media_type> -new_rt <robot_type> -m <media_id>
        change selected media:
           -new_mt <media_type> -new_rt <robot_type> ...
              -old_mt <media_type> -old_rt <robot_type> [-rn <robot_number>]
        parameters:
           media_type: dlt dlt_clean dlt2 dlt2_clean
                       hcart hcart_clean hcart2 hcart2_clean
                       8mm 8mm_clean 4mm 4mm_clean
                       odiskwm odiskwo qcart
           robot_type: none acs odl tc4 tc8 tl4 tl8 tld tlh tlm ts8 tsd tsh"

if [ $# -lt 2 ]
then
	$ECHO "$Usage"
	exit 1
fi

opt_h=0
opt_m=0
opt_new_mt=0
opt_new_rt=0
opt_old_mt=0
opt_old_rt=0
opt_rn=0
opt_v=0
verbose=0

#
# Since main's $* gets clobbered when bourne shell functions are called
# with arguments on an HP700 running 9.0x, stuff the argument list into
# a variable and implement shifts using awk.
#
args=$*

while [ "$args" ]
do
	option=`$ECHO "$args" | awk '{ print $1 }'`
	argument=`$ECHO "$args" | awk '{ print $2 }'`
	next=`$ECHO "$args" | awk '{ print $3 }'`

	if [ "`$ECHO $option | cut -c1`" != "-" ]
	then
		$ECHO "$Usage"
		exit 1
	fi

	case $option in
	-h)	opt_h=1; hostname=$argument
		;;
	-m)	opt_m=1; media_id=$argument
		;;
	-new_mt)	opt_new_mt=1
			if is_valid_media_type $argument
			then
				new_media_type=$argument
			else
				$ECHO "$progname: invalid media type $argument"
				$ECHO "$Usage"
				exit 1
			fi
			;;
	-new_rt)	opt_new_rt=1
			if is_valid_robot_type $argument
			then
				new_robot_type=$argument
			else
				$ECHO "$progname: invalid robot type $argument"
				$ECHO "$Usage"
				exit 1
			fi
			;;
	-old_mt)	opt_old_mt=1
			if is_valid_media_type $argument
			then
				old_media_type=$argument
			else
				$ECHO "$progname: invalid media type $argument"
				$ECHO "$Usage"
				exit 1
			fi
			;;
	-old_rt)	opt_old_rt=1
			if is_valid_robot_type $argument
			then
				old_robot_type=$argument
			else
				$ECHO "$progname: invalid robot type $argument"
				$ECHO "$Usage"
				exit 1
			fi
			;;
	-rn)	opt_rn=1; robot_number=$argument;;
	-v)	opt_v=1; verbose=1;;
	*)	$ECHO "$progname: invalid option $option"
		$ECHO "$Usage"
		exit 1;;
	esac
	if [ "$option" = "-v" ]
	then
		# simulate a "shift"
		args=`$ECHO "$args" | awk '{ for (i=2; i <= NF; i++) printf "%s ", $i }'`
	elif [ -n "$next" ]
	then
		# simulate a "shift 2"
		args=`$ECHO "$args" | awk '{ for (i=3; i <= NF; i++) printf "%s ", $i }'`
	else
		break
	fi
done

#
# Check for required parameters
#

if [ $opt_new_mt -eq 0 ]
then
	$ECHO "$progname: a new media type must be provided"
	$ECHO "$Usage"
	exit 1
elif [ $opt_new_rt -eq 0 ]
then
	$ECHO "$progname: a new robot type must be provided"
	$ECHO "$Usage"
	exit 1
fi

#
# old robot type and old media type must be provided as a pair
#
if [ $opt_old_mt -eq 1 ] && [ $opt_old_rt -eq 0 ]
then
	$ECHO "$progname: old robot type must be provided with old media type"
	$ECHO "$Usage"
	exit 1
elif [ $opt_old_mt -eq 0 ] && [ $opt_old_rt -eq 1 ]
then
	$ECHO "$progname: old media type must be provided with old robot type"
	$ECHO "$Usage"
	exit 1
elif [ $opt_old_mt -eq 1 ] && [ $opt_old_rt -eq 1 ]
then
	old_types_provided=1
else
	old_types_provided=0
	if [ $opt_rn -eq 1 ]
	then
		$ECHO "$progname: old robot and media types must be provided with robot number"
		$ECHO "$Usage"
		exit 1
	fi
fi

if [ $old_types_provided -eq 0 ] && [ $opt_m -eq 0 ]
then
	$ECHO "$progname: either media id or old robot/media types must be provided"
	$ECHO "$Usage"
	exit 1
fi

if [ $opt_h -eq 1 ]
then
	host_arg=" -h $hostname"
else
	host_arg=""
fi

#
# - Build queries to get lists of all selected media.
# - Make the queries.
# - For the case of a single media id, a 'vmchange' is made immediately
#   after a successful query.
# - Volume uniqueness checks will prevent having 2+ different robot types
#   corresponding to one robot number at any one time, so if operating
#   on a single media ID, don't bother with intermediate move to non-robotic.
# - For media selected by some other means (robot type, media type, robot
#   number), output from the queries will get merged to determine which 
#   media match all search patterns.
#
# NOTE: vmquery input parameters are lower case 

if [ $opt_m -eq 1 ]
then
	query_Media_Cmd="$VMQUERY -b$host_arg -m $media_id"
	if [ $verbose -eq 1 ]
	then
		$ECHO "$query_Media_Cmd"
	fi
	$query_Media_Cmd > $query_M 2>&1
	if [ $? -ne 0 ]
	then
		$ECHO "$progname: vmquery failed"
		cat $query_M
		cleanup
		exit 1
	fi

	if [ $verbose -eq 1 ]
	then
		$ECHO "$VMCHANGE -m $media_id -new_mt $new_media_type"
	fi
	$VMCHANGE -m $media_id -new_mt $new_media_type 2>$vmchange_err
	vmchange_stat=$?
	if [ $vmchange_stat -ne 0 ]
	then
		cat $vmchange_err
		cleanup
		exit $vmchange_stat
	fi

	if [ $verbose -eq 1 ]
	then
		$ECHO "$VMCHANGE -m $media_id -new_rt $new_robot_type"
	fi
	$VMCHANGE -m $media_id -new_rt $new_robot_type 2>$vmchange_err
	vmchange_stat=$?
	if [ $vmchange_stat -ne 0 ]
	then
		cat $vmchange_err
		cleanup
		exit $vmchange_stat
	fi

	if [ $verbose -eq 1 ]
	then
		$ECHO "$progname: successful (0): stop/restart vmd to see changes"
	fi
	cleanup
	exit 0
fi


query_Media_Type_Cmd="$VMQUERY -b$host_arg -mt $old_media_type"
if [ $verbose -eq 1 ]
then
	$ECHO "$query_Media_Type_Cmd"
fi
$query_Media_Type_Cmd > $query_MT 2>&1
if [ $? -ne 0 ]
then
	$ECHO "$progname: vmquery failed"
	cat $query_MT
	cleanup
	exit 1
fi
# remove header and "successful"
sed < $query_MT '1,3 d; $d' | sort > $query_MT_Sorted

query_Robot_Type_Cmd="$VMQUERY -b$host_arg -rt $old_robot_type"
if [ $verbose -eq 1 ]
then
	$ECHO "$query_Robot_Type_Cmd"
fi
$query_Robot_Type_Cmd > $query_RT 2>&1
if [ $? -ne 0 ]
then
	$ECHO "$progname: vmquery failed"
	cat $query_RT
	cleanup
	exit 1
fi
# remove header and "successful"
sed < $query_RT '1,3 d; $d' | sort > $query_RT_Sorted


#
# Check to see if an additional query command is needed
# to handle cleaning media.
#

if has_cleaning_type $old_media_type
then
	old_cleaning_media_type="$old_media_type$clean_suffix_input"
	query_Clean_Cmd="$VMQUERY -b$host_arg -mt $old_cleaning_media_type"
	query_Clean_Selected=1
	if [ $verbose -eq 1 ]
	then
		$ECHO "$query_Clean_Cmd"
	fi
	$query_Clean_Cmd > $query_C 2>&1
	if [ $? -ne 0 ]
	then
		$ECHO "$progname: vmquery failed"
		cat $query_C
		cleanup
		exit 1
	fi
	# remove header and "successful"
	sed < $query_C '1,3d; $d' | sort > $query_C_Sorted
else
	query_Clean_Selected=0
fi

if [ $opt_rn -eq 1 ]
then
	query_Robot_Number_Cmd="$VMQUERY -b$host_arg -rn $robot_number"
	query_Robot_Number_Selected=1
	if [ $verbose -eq 1 ]
	then
		$ECHO "$query_Robot_Number_Cmd"
	fi
	$query_Robot_Number_Cmd > $query_RN 2>&1
	if [ $? -ne 0 ]
	then
		$ECHO "$progname: vmquery failed"
		cat $query_RN
		cleanup
		exit 1
	fi
	# remove header and "successful"
	sed < $query_RN '1,3d; $d' | sort > $query_RN_Sorted
else
	query_Robot_Number_Selected=0
fi

#
# Merge query results:
#	old media type query with all others except cleaning type query
#		AND
#	cleaning type query with all others except old media type query
# The result should be one or two lists of volume to change.
#

#
# First, merge robot type and media type.
#

comm -12 $query_RT_Sorted $query_MT_Sorted > $common_RT_MT
if [ $? -ne 0 ]
then
	$ECHO "$progname: comm(1) failed"
	cleanup
	exit 1
fi

if [ "`wc -l < $common_RT_MT`" -eq 0 ]
then
	$ECHO "$progname: warning: no media matches provided media and robot types"
	if [ $verbose -eq 1 ]
	then
		$ECHO "$progname: successful (0): stop/restart vmd to see changes"
	fi
	cleanup
	exit 0
fi


if [ $query_Robot_Number_Selected -eq 0 ]
then

	media_list=`awk '{ ORS=" "; print $1 }' < $common_RT_MT`
	if [ $? -ne 0 ]
	then
		$ECHO "$progname: awk(1) failed"
		cleanup
		exit 1
	fi

	#
	# Change pass 1 - move to non-robotic, relying on the fact that
	# robot coordinates are not cleared
	#

	for medium in $media_list
	do
		if [ $old_robot_type != "none" ]
		then
			if [ $verbose -eq 1 ]
			then
				$ECHO "$VMCHANGE -m $medium -new_rt none"
			fi
			$VMCHANGE -m $medium -new_rt none 2>$vmchange_err
			vmchange_stat=$?
			if [ $vmchange_stat -ne 0 ]
			then
				cat $vmchange_err
				cleanup
				exit $vmchange_stat
			fi
		fi
		if [ $verbose -eq 1 ]
		then
			$ECHO "$VMCHANGE -m $medium -new_mt $new_media_type"
		fi
		$VMCHANGE -m $medium -new_mt $new_media_type 2>$vmchange_err
		vmchange_stat=$?
		if [ $vmchange_stat -ne 0 ]
		then
			cat $vmchange_err
			cleanup
			exit $vmchange_stat
		fi
	done

else

	#
	# Merge robot number with robot type and media type.
	#

	comm -12 $query_RN_Sorted $common_RT_MT > $common_RT_MT_RN
	if [ $? -ne 0 ]
	then
		$ECHO "$progname: comm(1) failed"
		cleanup
		exit 1
	fi

	if [ "`wc -l < $common_RT_MT_RN`" -eq 0 ]
	then
		$ECHO "$progname: warning: no media matches provided media type, robot type, and robot number"
		if [ $verbose -eq 1 ]
		then
			$ECHO "$progname: successful (0): stop/restart vmd to see changes"
		fi
		cleanup
		exit 0
	fi

	media_list=`awk '{ ORS=" "; print $1 }' < $common_RT_MT_RN`
	if [ $? -ne 0 ]
	then
		$ECHO "$progname: awk(1) failed"
		cleanup
		exit 1
	fi

	#
	# Change pass 1 - move to non-robotic, relying on the fact that
	# robot coordinates are not cleared
	#

	for medium in $media_list
	do
		if [ $verbose -eq 1 ]
		then
			$ECHO "$VMCHANGE -m $medium -new_rt none"
		fi
		$VMCHANGE -m $medium -new_rt none 2>$vmchange_err
		vmchange_stat=$?
		if [ $vmchange_stat -ne 0 ]
		then
			cat $vmchange_err
			cleanup
			exit $vmchange_stat
		fi
		if [ $verbose -eq 1 ]
		then
			$ECHO "$VMCHANGE -m $medium -new_mt $new_media_type"
		fi
		$VMCHANGE -m $medium -new_mt $new_media_type 2>$vmchange_err
		vmchange_stat=$?
		if [ $vmchange_stat -ne 0 ]
		then
			cat $vmchange_err
			cleanup
			exit $vmchange_stat
		fi
	done
fi

#
# Repeat the process to handle cleaning tapes transparently
#

if [ $query_Clean_Selected -eq 1 ]
then

	#
	# First, merge robot type and cleaning media type.
	#

	comm -12 $query_RT_Sorted $query_C_Sorted > $common_RT_C
	if [ $? -ne 0 ]
	then
		$ECHO "$progname: comm(1) failed"
		cleanup
		exit 1
	fi

	if [ "`wc -l < $common_RT_C`" -ne 0 ]
	then
		# found match of cleaning media
		found_clean_match=1
	else
		found_clean_match=0
	fi

	if [ $found_clean_match -eq 1 -a $query_Robot_Number_Selected -eq 0 ]
	then

		cln_media_list=`awk '{ ORS=" "; print $1 }' < $common_RT_C`
		if [ $? -ne 0 ]
		then
			$ECHO "$progname: awk(1) failed"
			cleanup
			exit 1
		fi

		for medium in $cln_media_list
		do
			if [ $old_robot_type != "none" ]
			then
				if [ $verbose -eq 1 ]
				then
					$ECHO "$VMCHANGE -m $medium -new_rt none"
				fi
				$VMCHANGE -m $medium -new_rt none 2>$vmchange_err
				vmchange_stat=$?
				if [ $vmchange_stat -ne 0 ]
				then
					cat $vmchange_err
					cleanup
					exit $vmchange_stat
				fi
			fi
			new_cleaning_media_type="$new_media_type$clean_suffix_input"
			if [ $verbose -eq 1 ]
			then
				$ECHO "$VMCHANGE -m $medium -new_mt $new_cleaning_media_type"
			fi
			$VMCHANGE -m $medium -new_mt $new_cleaning_media_type 2>$vmchange_err
			vmchange_stat=$?
			if [ $vmchange_stat -ne 0 ]
			then
				cat $vmchange_err
				cleanup
				exit $vmchange_stat
			fi
		done
	elif [ $found_clean_match -eq 1 -a $query_Robot_Number_Selected -ne 0 ]
	then

		#
		# Merge robot number with robot type and media type.
		#

		comm -12 $query_RN_Sorted $common_RT_C > $common_RT_C_RN
		if [ $? -ne 0 ]
		then
			$ECHO "$progname: comm(1) failed"
			cleanup
			exit 1
		fi

		if [ "`wc -l < $common_RT_C_RN`" -eq 0 ]
		then
			# no cleaning match found after merge with robot number list
			found_clean_match=0
		fi

		if [ $found_clean_match -eq 1 ]
		then

			cln_media_list=`awk '{ ORS=" "; print $1 }' < $common_RT_C_RN`
			if [ $? -ne 0 ]
			then
				$ECHO "$progname: awk(1) failed"
				cleanup
				exit 1
			fi

			for medium in $cln_media_list
			do
				if [ $verbose -eq 1 ]
				then
					$ECHO "$VMCHANGE -m $medium -new_rt none"
				fi
				$VMCHANGE -m $medium -new_rt none 2>$vmchange_err
				vmchange_stat=$?
				if [ $vmchange_stat -ne 0 ]
				then
					cat $vmchange_err
					cleanup
					exit $vmchange_stat
				fi
				new_cleaning_media_type="$new_media_type$clean_suffix_input"
				if [ $verbose -eq 1 ]
				then
					$ECHO "$VMCHANGE -m $medium -new_mt $new_cleaning_media_type"
				fi
				$VMCHANGE -m $medium -new_mt $new_cleaning_media_type 2>$vmchange_err
				vmchange_stat=$?
				if [ $vmchange_stat -ne 0 ]
				then
					cat $vmchange_err
					cleanup
					exit $vmchange_stat
				fi
			done
		fi
	fi
fi

if [ $new_robot_type = "none" ]
then
	#
	# media is already none-robotic (from pass 1), don't need to do it twice
	#

	if [ $verbose -eq 1 ]
	then
		$ECHO "$progname: successful (0): stop/restart vmd to see changes"
	fi
	cleanup
	exit 0
fi

if [ $query_Robot_Number_Selected -eq 0 ]
then
	#
	# Change pass 2 - move to robotic (new robot type), relying on the 
	# fact that robot coordinates were not cleared
	#

	for medium in $media_list
	do
		if [ $verbose -eq 1 ]
		then
			$ECHO "$VMCHANGE -m $medium -new_rt $new_robot_type"
		fi
		$VMCHANGE -m $medium -new_rt $new_robot_type 2>$vmchange_err
		vmchange_stat=$?
		if [ $vmchange_stat -ne 0 ]
		then
			cat $vmchange_err
			cleanup
			exit $vmchange_stat
		fi
	done
else
	#
	# Change pass 2 - move to robotic (new robot type), relying on the 
	# fact that robot coordinates were not cleared
	#

	for medium in $media_list
	do
		if [ $verbose -eq 1 ]
		then
			$ECHO "$VMCHANGE -m $medium -new_rt $new_robot_type"
		fi
		$VMCHANGE -m $medium -new_rt $new_robot_type 2>$vmchange_err
		vmchange_stat=$?
		if [ $vmchange_stat -ne 0 ]
		then
			cat $vmchange_err
			cleanup
			exit $vmchange_stat
		fi
	done
fi

#
# Repeat the move-to-robotic process to handle cleaning tapes transparently
#

if [ $query_Clean_Selected -eq 1 ]
then
	if [ $query_Robot_Number_Selected -eq 0 ]
	then
		for medium in $cln_media_list
		do
			if [ $verbose -eq 1 ]
			then
				$ECHO "$VMCHANGE -m $medium -new_rt $new_robot_type"
			fi
			$VMCHANGE -m $medium -new_rt $new_robot_type 2>$vmchange_err
			vmchange_stat=$?
			if [ $vmchange_stat -ne 0 ]
			then
				cat $vmchange_err
				cleanup
				exit $vmchange_stat
			fi
		done
	else
		for medium in $cln_media_list
		do
			if [ $verbose -eq 1 ]
			then
				$ECHO "$VMCHANGE -m $medium -new_rt $new_robot_type"
			fi
			$VMCHANGE -m $medium -new_rt $new_robot_type 2>$vmchange_err
			vmchange_stat=$?
			if [ $vmchange_stat -ne 0 ]
			then
				cat $vmchange_err
				cleanup
				exit $vmchange_stat
			fi
		done
	fi
fi

if [ $verbose -eq 1 ]
then
	$ECHO "$progname: successful (0): stop/restart vmd to see changes"
fi
cleanup
exit 0
