#!/bin/ksh
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# bos72X src/bos/usr/lpp/bos.ahafs/ahafs_lku.sh 1.8 
#  
# Licensed Materials - Property of IBM 
#  
# COPYRIGHT International Business Machines Corp. 2014,2020 
# All Rights Reserved 
#  
# US Government Users Restricted Rights - Use, duplication or 
# disclosure restricted by GSA ADP Schedule Contract with IBM Corp. 
#  
# IBM_PROLOG_END_TAG 
# @(#)91	1.8 src/bos/usr/lpp/bos.ahafs/ahafs_lku.sh, ahafsutils, bos72X, x2020_52A0 12/14/20 11:49:02

AHAERRLOG="/var/adm/ras/liveupdate/logs/ahaerrlog"

function LOG_ERR {
    tstamp=$(/usr/bin/date "+%m/%d/%Y-%H:%M:%S")
    echo "AHAFS ${tstamp} ERROR $@" >> ${AHAERRLOG}
}
function LOG_DEBUG {
    tstamp=$(/usr/bin/date "+%m/%d/%Y-%H:%M:%S")
    echo "AHAFS ${tstamp} DEBUG $@" >> ${AHAERRLOG}
}

rc=0;

if [[ $1 == "orig" ]] then
	AHALOG="/tmp/aha_lku.log"
	# Check if this call is because of an error that occured after our 
	# script had run 
	if [[ $2 == "error" ]] then
		if [[ -e $AHALOG ]] then
			AHADEVNAME=`awk '{print $1}' < $AHALOG`
			AHAFSNAME=`awk '{print $2}' < $AHALOG`
			/usr/sbin/mount -v ahafs $AHADEVNAME $AHAFSNAME
			rm $AHALOG
		fi
		exit $rc
	fi
	
	AHAFSLINE=`mount | awk '$3 == "ahafs"'`
	if [[ $AHAFSLINE == '' ]] then
		exit 0
	fi
	echo $AHAFSLINE > $AHALOG
	AHAFSNAME=`awk '{print $2}' < $AHALOG`
	LOG_DEBUG "Issue: /usr/sbin/umount $AHAFSNAME"
	/usr/sbin/umount $AHAFSNAME >> $AHAERRLOG 2>&1
	if [[ $? -ne 0 ]] then
		rm $AHALOG
		LOG_ERR "Issue: find $AHAFSNAME | awk '/mon\$/' | xargs fuser"
		find $AHAFSNAME | awk '/mon$/' | xargs fuser >> $AHAERRLOG 2>&1
		LOG_ERR "Issue: find $AHAFSNAME | awk '/monFactory\$/' | xargs fuser"
		find $AHAFSNAME | awk '/monFactory$/' | xargs fuser >> $AHAERRLOG 2>&1
		LOG_ERR "Issue: ps -kef"
		ps -kef >> $AHAERRLOG 2>&1

		rc=1
	fi
elif [[ $1 == "surr" ]] then
	CHROOT_DIR="/old"
	AHALOG="$CHROOT_DIR/tmp/aha_lku.log"
	if [[ -e $AHALOG ]] then
		AHADEVNAME=`awk '{print $1}' < $AHALOG`;
		AHAFSNAME=`awk '{print $2}' < $AHALOG`;
		/usr/sbin/chroot $CHROOT_DIR /usr/sbin/mount -v ahafs $AHADEVNAME $AHAFSNAME;
		if [[ $? -ne 0 ]] then
			rc=1
		else
			rm $AHALOG
		fi
	else
		print "Did not find log file. Mount of ahafs ignored."
		exit 0
	fi
else
	rc=1
fi
exit $rc

