#!/bin/ksh



PMCONF=/var/perf/pm/config/pmconf
PMCFG_EXT=/var/perf/pm/bin/pmcfg_ext

if [[ -f ${PMCFG_EXT} ]]
then
	# Get current PMExt value if available
	PMExt_Val=`/usr/bin/grep "# PMExt:" ${PMCONF} | /usr/bin/cut -d':' -f2 2>/dev/null`
	if [[ $? -eq 0 ]]
	then
		# Remove "# PMExt" entry from PMCONF files, and copy to tmp PMCONF file
		/usr/bin/grep -v "# PMExt" ${PMCONF} > /tmp/_pmconf_$$ 2>/dev/null
		if [[ $? -eq 0 ]]
		then
			# If PMExt value was set to 2, then add "# PMExt" entry in PMCONF
			if [[ ${PMExt_Val} -eq 2 ]]
			then
				# If PM enhancement was enabled, append PMExt entry in pmconf
				echo "# PMExt" >> /tmp/_pmconf_$$ 2>/dev/null
				if [[ $? -ne 0 ]]
				then
					echo "Unable to append PMExt to copy of pmconf"
				fi
			fi
			/usr/bin/cp /tmp/_pmconf_$$ ${PMCONF}
			if [ $? -ne 0 ]
			then
				echo "Unable to copy updated pmconf file"
			fi
		fi
	fi
fi

# Remove the tmp file if exist
if [[ -e /tmp/_pmconf_$$ ]]
then
	/usr/bin/rm -f /tmp/_pmconf_$$
fi


# Rename PM output files back to its old name.


PMDIR=/var/perf/pm
host=`/usr/bin/uname -n`
DATADIR=${PMDIR}/daily/${host}
PMCONF=${PMDIR}/config/pmconf

if [[ -e ${PMCONF} ]]
then
	# If below string found in PMCONF, revert the filename change and update the PMCONF 
	/usr/bin/grep "# FileNameChange:PMAIX" ${PMCONF} 2>/dev/null >&2
	if [[ $? -eq 0 ]]
	then
		if [[ -d ${DATADIR} ]]
		then
			for old_filename in `/usr/bin/ls ${DATADIR} 2>/dev/null`
			do
				filestart=`echo $old_filename | /usr/bin/cut -d'.' -f 1`
				ispmfile=0
				if [[ ${filestart} == "pm_amsstat" ]]
				then
					new_filename="ams${old_filename#${filestart}}"
					ispmfile=1
				elif [[ ${filestart} == "pm_crosslparstat" ]]
				then
					new_filename="clpar${old_filename#${filestart}}"
					ispmfile=1
				elif [[ ${filestart} == "pm_shrprocstat" ]]
				then
					new_filename="spp${old_filename#${filestart}}"
					ispmfile=1
				fi
				
				if [[ ${ispmfile} -eq 1 ]]
				then
					/usr/bin/mv ${DATADIR}/${old_filename} ${DATADIR}/${new_filename}
					if [[ $? -ne 0 ]]; then
						echo "Failed to rename ${old_filename} to ${new_filename}"
					fi
				fi
			done
		fi
		
		# Updating PMCONF file 
		FailedMsg=""
		/usr/bin/grep -v "# FileNameChange:PMAIX" ${PMCONF} > /tmp/_pmconf_$$ 2>/dev/null
		if [[ $? -eq 0 ]]
		then
			/usr/bin/cp /tmp/_pmconf_$$ ${PMCONF} 
			if [[ $? -ne 0 ]]
			then
				FailedMsg="Failed in copying updated PMCONF"
			fi
			/usr/bin/rm -f /tmp/_pmconf_$$ 2>/dev/null
		else
			FailedMsg="Failed in creating copy of PMCONF"
		fi
		if [[ ${FailedMsg} != "" ]]
		then
			echo "Failed to update the pmconf file with filename change status: "
			echo "${FailedMsg}"
		fi
	fi
fi
# End of IF -e ${PMCONF} block in pmaix.unconfig

	
# Remove pmperfrec service from SRC Subsystem

PMRecService="pmperfrec"
# To disable PM Perf recording
/usr/bin/lssrc -s ${PMRecService} >/dev/null 2>&1
if [[ $? -eq 0 ]]
then
	# Make sure the service is stopped before removing from SRC Subsystem, else the process will be running
	/usr/bin/lssrc -s ${PMRecService} | /usr/bin/tr -s ' ' | /usr/bin/grep ${PMRecService} | /usr/bin/grep -v grep | /usr/bin/grep ' active' >/dev/null 2>&1
	if [ $? -eq 0 ]
	then
		 # Stop the pmperfrec serivce 
		/usr/bin/stopsrc -s ${PMRecService} >/dev/null 2>&1
		# Confirm whether the pmperfrec service is stopped
		/usr/bin/lssrc -s ${PMRecService} | /usr/bin/tr -s ' ' | /usr/bin/grep ${PMRecService} | /usr/bin/grep -v grep | /usr/bin/grep ' active' >/dev/null 2>&1
		if [ $? -ne 0 ]
		then
			echo "Stopped ${PMRecService} service"
		else
			echo "Failed to stop ${PMRecService} service"
		fi
	fi
	
	# Remove to pmperfrec service from SRC Subsystem
	/usr/bin/rmssys -s ${PMRecService} >/dev/null
	if [[ $? -ne 0 ]]
	then
		echo "Failed to remove ${PMRecService} service from SRC subsystem"
		#return 1
	else
		echo "${PMRecService} is removed from SRC subsystem"
	fi
fi

# Check if pmperfrec entry listed in inittab, if yes, remove it
lsitab ${PMRecService} >/dev/null 2>&1
if [[ $? -eq 0 ]]
then
	# Removing pmperfrec entry from inittab
	/usr/sbin/rmitab ${PMRecService} >/dev/null 2>&1
	# Confirm whether pmperfrec entry is removed from inittab
	lsitab ${PMRecService} >/dev/null 2>&1
	if [[ $? -eq 0 ]]
	then
		echo "Failed to remove the entry in /etc/inittab"
	else
		echo "Removed ${PMRecService} entry in /etc/inittab"
	fi
fi
# End of Remove pmperfrec service from SRC Subsystem


# Exit
exit 0

