#!/bin/sh

TEMPDIR=/tmp

PATH=${PATH}:/usr/openv/gr/bin:/usr/openv/netbackup/bin:/usr/openv/netbackup/bin/goodies:/usr/openv/netbackup/bin/admincmd:/usr/openv/volmgr/bin:/usr/openv/volmgr/bin/goodies

unfreeze_force=0

scratch_pool=`vmpool -listscratch | tail -1`
scratch_num=`vmpool -listall -bx \
	| awk " \\$1 ~ /^$scratch_pool\$/ { print \\$2 }"`

if [ -n "$1" ]
then
	TAPES=$@
else
	TAPES=''
	TAPES_loaded_nonscratch=`vmquery -a -w | awk "\\$11 ~ /_TLD\$/ \
		&& \\$12 !~ /^$scratch_pool\$/ && \\$3 !~ /_CLN\$/ { print \\$1 }"`
	if [ -n "$TAPES_loaded_nonscratch" ]
	then
		for tape in $TAPES_loaded_nonscratch
		do
			printf "Considering ${tape} ..."
			status=`vmquery -m ${tape} | awk -F: ' $1 ~ /^status$/ \
		    { sub("[      ]*0x", "") ; print $NF } '`
			if [ -z "$status" ]
			then
				TAPES="$TAPES $tape"
				echo " in (unassigned)."
			else
				bpmedialist -m ${tape} > ${TEMPDIR}/${tape} 2>/dev/null
				if [ ! -s ${TEMPDIR}/${tape} ]
				then
					TAPES="$TAPES $tape"
					echo " in (no media DB entry)."
				elif [ -n "`grep FROZEN ${TEMPDIR}/${tape} | grep EXPIRED`" ]
				then
					TAPES="$TAPES $tape"
					echo " in (frozen and expired)."
				else
					echo " out."
				fi
				rm ${TEMPDIR}/${tape}
			fi
		done
	fi
fi

if [ -z "$TAPES" ]
then
	echo "Nothing to do!"
	exit 0
fi

echo "Potentially reclaiming `echo $TAPES | wc -w` media: $TAPES"

catalog_pools=`vmpool -listall -bx | awk 'tolower($0) ~ /catalog/ \
	{ print $1 }'`

for tape in $TAPES
do
	bpmedialist -m ${tape} > ${TEMPDIR}/${tape}
	frozen=0
	[ -n "`grep FROZEN ${TEMPDIR}/${tape}`" ] && frozen=1
	cat ${TEMPDIR}/${tape}
	echo

	if [ $frozen -eq 1 ]
	then
		if [ $unfreeze_force -eq 0 ]
		then
			printf "Unfreeze ${tape}? "
			printf "([y]/n/f) "
			read confirm
			[ "`echo $confirm | egrep '^[fF]'`" ] && unfreeze_force=1
			if [ ! "`echo $confirm | egrep '^[nN]'`" ];
			then
				unfreeze=1
			fi
		else
			unfreeze=1
		fi
		if [ $unfreeze -eq 1 ]
		then
			host=`awk '$1 ~ /^Server$/ { print $NF }' ${TEMPDIR}/${tape}`
			echo bpmedia -unfreeze -m $tape -h $host -v
			bpmedia -unfreeze -m $tape -h $host -v
		fi
	fi

	vmquery -m ${tape} > ${TEMPDIR}/${tape}
	inpool=`awk -F: ' $1 ~ /^volume pool$/ {
		sub("^ *", "", $NF)
		sub(" \(.*$", "", $NF)
		print $NF
	} ' ${TEMPDIR}/${tape}`
	inpool_num=`awk -F: ' $1 ~ /^volume pool$/ {
		sub("^[^(]*", "")
		gsub("[()]", "")
		print $NF
	} ' ${TEMPDIR}/${tape}`
 	status=`awk -F: ' $1 ~ /^status$/ {
		sub("[      ]*0x", "")
		print $NF
	} ' ${TEMPDIR}/${tape}`

	cat ${TEMPDIR}/${tape}

	do_something=0

	if [ -z "$status" ];
	then
		printf "${tape} is already deassigned"
		if [ "$inpool_num" -eq "$scratch_num" ];
		then
			printf " and is already in ${scratch_pool}. "
		else
			printf "; move it to ${scratch_pool}? "
			do_something=1
		fi
	else
		printf "Deassign ${tape} "
		do_something=1
		if [ "$inpool_num" = "$scratch_pool" ];
		then
			printf "(and leave it in ${scratch_pool})? "
		else
			printf "and move it to ${scratch_pool}? "
		fi
	fi
	
	if [ "$do_something" -gt 0 ];
	then
		printf "([y]/n) "
		read confirm
		if [ ! "`echo $confirm | egrep '^[nN]'`" ];
		then
			[ -z "$status" ] && status=0
			echo vmquery -deassignbyid ${tape} ${inpool_num} ${status}
			vmquery -deassignbyid ${tape} ${inpool_num} ${status}
			echo vmchange -m ${tape} -p ${scratch_num}
			vmchange -m ${tape} -p ${scratch_num}
			for pool in $catalog_pools
			do
				if [ "$inpool" = "$pool" ]
				then
					printf "$tape appears to have been in catalog pool $pool, label it? ([y]/n) "
					read confirm
					if [ ! "`echo $confirm | egrep '^[nN]'`" ];
					then
						# XXX read density
						nohup bplabel -m $tape -d hcart2 -o -p $scratch_pool &
					fi
				fi
			done
			echo Altered ${tape}...
			vmquery -m ${tape}
		fi
	else
		printf "\n"
	fi

	rm ${TEMPDIR}/${tape}
	
done

