#!/bin/bash

# Licensed Materials - Property of IBM
# 5724-I68
#
# © Copyright IBM Corp. 2008
#
# US Government Users Restricted Rights - Use, duplication, or
# disclosure restricted by GSA ADP Schedule Contract with IBM Corp.


# usage: wpcprof.sh        --enable --profilerName=yourkit|jprofiler --serviceType=admin|appsvr|event|queuemgr|scheduler|workflow [--serviceName=unique_Service_Name --port=port_number]
#                          --disable --serviceType=<serviceType> --profilerName=<profilerName>
#                          --start cpu|obj [params]
#                          --stop cpu|obj [params] 
#                          --save [params]
#                          --capturemem [params]
#                          --help
#                          --help <command>


. ${TOP}/bin/get_params.sh

function getDefaultServiceName(){
	svcType=$1;

	if [ "$svcType" = "admin" ]; then
		echo admin_${SHORT_HOSTNAME}
	fi

	if [ "$svcType" = "event" ]; then
		echo eventprocessor_${SHORT_HOSTNAME}
	fi
	
	if [ "$svcType" = "queuemgr" ]; then
		echo queuemanager_${SHORT_HOSTNAME}
	fi
	
	if [ "$svcType" = "workflow" ]; then
		echo workflowengine_${SHORT_HOSTNAME}
	fi
	
	if [ "$svcType" = "scheduler" ]; then
		echo scheduler_${SHORT_HOSTNAME}
	fi

	if [ "$svcType" = "appsvr" ]; then
		echo appsvr_${SHORT_HOSTNAME}
	fi	
}

function profilerEnabledNextSteps(){
	svcType=$1;
	svcName=$2;
	
	printMessage "PROFILER_INFO_PROFILING_ENABLED_FOR_SERVICE_SH"
	printMessage "PROFILER_INFO_RESTART_THE_SERVICE_TO_TAKE_AFFECT_SH"

	read res
	if [ "$res" = "y" ]; then
	    startupScript=`getStartupScriptName $svcType`
	    ${TOP}/bin/go/start/${startupScript} --svc_name=${svcName}
	    unsetParams
	else
	   return 
	fi	
	
}

function profilerDisabledNextSteps(){
	svcType=$1;
	svcName=$2;
	
	printMessage "PROFILER_INFO_PROFILING_DISABLED_FOR_SERVICE_SH"
	printMessage "PROFILER_INFO_RESTART_THE_SERVICE_TO_TAKE_AFFECT_SH"

	read res
	if [ "$res" = "y" ]; then
		startupScript=`getStartupScriptName $svcType`
	    ${TOP}/bin/go/start/${startupScript} --svc_name=${svcName}
	    unsetParams
	else
	   return 
	fi	
	
}

function getStartupScriptName(){
	svcType=$1;
	
	if [ "$svcType" = "admin" ]; then
		echo "start_admin.sh"
	fi

	if [ "$svcType" = "event" ]; then
		echo "start_eventprocessor.sh"
	fi
	
	if [ "$svcType" = "queuemgr" ]; then
		echo "start_queuemanager.sh"
	fi
	
	if [ "$svcType" = "workflow" ]; then
		echo "start_workflowengine.sh"
	fi
	
	if [ "$svcType" = "scheduler" ]; then
		echo "start_scheduler.sh"
	fi
	
	if [ "$svcType" = "appsvr" ]; then
		echo "start_appsvr.sh"
	fi	
}

function verifyParameters(){
	svcType=$1
	if [ "$svcType" != "admin" ] && [ "$svcType" != "event" ] && [ "$svcType" != "queuemgr" ] && [ "$svcType" != "workflow" ] && [ "$svcType" != "scheduler" ] && [ "$svcType" != "appsvr" ]; then
	  printMessage "PROFILER_INFO_INVALID_SERVICE_TYPE_SH"
	  return;
	fi
	
	#if serviceType param is correct, then return an empty string indicating that serviceType is valid	
	${ECHO} ""
	return;
}
function enableProfiling(){
  service=$_CCD_SERVICETYPE
  profiler=$_CCD_PROFILERNAME
  serviceName=$_CCD_SERVICENAME
  
  message=`verifyParameters $service`
  #on some platforms, checking for empty string does not work. Hence this workaround of prefixing with X and comparing with X
  if [ "X${message}" != "X" ]; then
  	  ${ECHO} $message
      return;
  fi
  
  #on some platforms, checking for empty string does not work. Hence this workaround of prefixing with X and comparing with X
  if [ "X${_CCD_SERVICETYPE}" = "X" ] ; then
	printMessage "PROFILER_ERROR_SERVICETYPE_PARAMETER_IS_MISSING_SH"
        return 
  fi

  STATUS=`$JAVA_RT -DTOP=$TOP -DCCD_ETC_DIR=$CCD_ETC_DIR -classpath $CLASSPATH com.ibm.ccd.profiler.client.ProfilerCommandLine --status --serviceType=$service --serviceName=$serviceName`

  if [ "$STATUS" = "ENABLED" ] ; then
	  printMessage "PROFILER_INFO_PROFILING_ALREADY_ENABLED_FOR_SERVICE_SH"
	  return;
  fi

  if [ "$profiler" = "yourkit" ] ; then
      profiler_path=${TOP}/profiler/yourkit
      if [ ! -f "${profiler_path}/libyjpagent.so" ] || [ ! -f "${profiler_path}/yjp-controller-api-redist.jar" ]; then
          printMessage "PROFILER_INFO_PROFILER_LIBRARIES_MISSING_NEXT_STEPS_SH"
          return;
       fi
	  enableYourkit;
   	  profilerEnabledNextSteps $service $serviceName
	  return;
  fi
  
  if [ "$profiler" = "jprofiler" ]; then
      profiler_path=${TOP}/profiler/jprofiler
      if [ ! -f "${profiler_path}/libjprofilerti.so" ] || [ ! -f "${profiler_path}/agent.jar" ] || [ ! -f "${profiler_path}/config.xml" ]; then
          printMessage "PROFILER_INFO_PROFILER_LIBRARIES_MISSING_NEXT_STEPS_SH"
          return;
      fi
    enableJProfiler;
    profilerEnabledNextSteps $service $serviceName
    return;
  fi

  printMessage "PROFILER_INFO_PROFILER_NOT_SUPPORTED_SH"
  return;
}

function enableYourkit(){

  export LD_LIBRARY_PATH=$CCD_YOURKIT_LIB_PATH:$LD_LIBRARY_PATH

  if [ "$service" = "admin" ]; then
	default_port=`${GREP} '^ *profiler_port_admin' ${CCD_ETC_DIR}/default/common.properties | cut -f2 -d "="`
  	port=${_CCD_PORT=$default_port}
	export CCD_ADMIN_JVM_PROFILING_AGENT="-Xrunyjpagent:port=$port"
	export CCD_ADMIN_PROFILER_NAME=$profiler
	export CCD_ADMIN_PROFILER_PORT=$port
  fi

  if [ "$service" = "event" ]; then
	default_port=`${GREP} '^ *profiler_port_event' ${CCD_ETC_DIR}/default/common.properties | cut -f2 -d "="`
  	port=${_CCD_PORT=$default_port}
	export CCD_EVENTPROCESSOR_JVM_PROFILING_AGENT="-Xrunyjpagent:port=$port"
	export CCD_EVENTPROCESSOR_PROFILER_NAME=$profiler
	export CCD_EVENTPROCESSOR_PROFILER_PORT=$port
  fi
  
  if [ "$service" = "queuemgr" ]; then
	default_port=`${GREP} '^ *profiler_port_queuemgr' ${CCD_ETC_DIR}/default/common.properties | cut -f2 -d "="`
  	port=${_CCD_PORT=$default_port}
	export CCD_QUEUEMANAGER_JVM_PROFILING_AGENT="-Xrunyjpagent:port=$port"
	export CCD_QUEUEMANAGER_PROFILER_NAME=$profiler
	export CCD_QUEUEMANAGER_PROFILER_PORT=$port
  fi


  if [ "$service" = "workflow" ]; then
	default_port=`${GREP} '^ *profiler_port_workflow' ${CCD_ETC_DIR}/default/common.properties | cut -f2 -d "="`
  	port=${_CCD_PORT=$default_port}
	export CCD_WORKFLOWENGINE_JVM_PROFILING_AGENT="-Xrunyjpagent:port=$port"
	export CCD_WORKFLOWENGINE_PROFILER_NAME=$profiler
	export CCD_WORKFLOWENGINE_PROFILER_PORT=$port
  fi

  if [ "$service" = "scheduler" ]; then
	default_port=`${GREP} '^ *profiler_port_scheduler' ${CCD_ETC_DIR}/default/common.properties | cut -f2 -d "="`
  	port=${_CCD_PORT=$default_port}
	export CCD_SCHEDULER_JVM_PROFILING_AGENT="-Xrunyjpagent:port=$port"
	export CCD_SCHEDULER_PROFILER_NAME=$profiler
	export CCD_SCHEDULER_PROFILER_PORT=$port
  fi

  if [ "$service" = "appsvr" ]; then
  	default_port=`${GREP} '^ *profiler_port_appsvr' ${CCD_ETC_DIR}/default/common.properties | cut -f2 -d "="`
  	port=${_CCD_PORT=$default_port}

  	if [ "$CCD_APPSVR" = "websphere6" ]; then
		export CCD_APPSVR_PROFILER_NAME=$profiler
		export CCD_APPSVR_PROFILER_PORT=$port

  		#edit server.xml
		#echo "------------ editing WAS config file with profiling agent options -------------"
		CONFIG_PATH=$WAS_PROFILE_HOME/config/cells/$WAS_CELLNAME/nodes/$WAS_NODENAME/servers/$WAS_APPSERVERNAME
		cp $CONFIG_PATH/server.xml $CONFIG_PATH/backup_server.xml
  		${WAS_PROFILE_HOME}/bin/wsadmin.sh -conntype none -lang jacl -f ${TOP}/bin/websphere/modify_jvmargs.jacl $WAS_NODENAME $WAS_APPSERVERNAME "-Xrunyjpagent:port=$port  -Dprofiler_opts=${CCD_APPSVR_PROFILER_NAME}__${CCD_APPSVR_PROFILER_PORT}"
		#echo "-------------completed editing WAS config file ----------------"
  	fi
  	
  	if [ "$CCD_APPSVR" = "weblogic92" ]; then
  		export CCD_APPSVR_JVM_PROFILING_AGENT="-Xrunyjpagent:port=$port"
		export CCD_APPSVR_PROFILER_NAME=$profiler
		export CCD_APPSVR_PROFILER_PORT=$port
	fi
  	
	if [ "$CCD_APPSVR" = "tomcat33" ]; then
  		export CCD_APPSVR_JVM_PROFILING_AGENT="-Xrunyjpagent:port=$port"
		export CCD_APPSVR_PROFILER_NAME=$profiler
		export CCD_APPSVR_PROFILER_PORT=$port
	fi
  	
  fi
}

function enableJProfiler(){
 
 export LD_LIBRARY_PATH=$CCD_JPROFILER_LIB_PATH:$LD_LIBRARY_PATH
 export LIBPATH=$LIBPATH:$CCD_JPROFILER_LIB_PATH
 if [ "$service" = "admin" ]; then
	export CCD_ADMIN_JVM_PROFILING_AGENT="\"-Xrunjprofiler:offline,id=200,config=$CCD_JPROFILER_LIB_PATH/config.xml\" \"-Xbootclasspath/a:$CCD_JPROFILER_LIB_PATH/agent.jar\""
	export CCD_ADMIN_PROFILER_NAME=$profiler
  fi
 
  if [ "$service" = "event" ]; then
	export CCD_EVENTPROCESSOR_JVM_PROFILING_AGENT="\"-Xrunjprofiler:offline,id=201,config=$CCD_JPROFILER_LIB_PATH/config.xml\" \"-Xbootclasspath/a:$CCD_JPROFILER_LIB_PATH/agent.jar\""
	export CCD_EVENTPROCESSOR_PROFILER_NAME=$profiler
  fi
  
  if [ "$service" = "queuemgr" ]; then
	export CCD_QUEUEMANAGER_JVM_PROFILING_AGENT="\"-Xrunjprofiler:offline,id=202,config=$CCD_JPROFILER_LIB_PATH/config.xml\" \"-Xbootclasspath/a:$CCD_JPROFILER_LIB_PATH/agent.jar\""
	export CCD_QUEUEMANAGER_PROFILER_NAME=$profiler
  fi


  if [ "$service" = "workflow" ]; then
	export CCD_WORKFLOWENGINE_JVM_PROFILING_AGENT="\"-Xrunjprofiler:offline,id=203,config=$CCD_JPROFILER_LIB_PATH/config.xml\" \"-Xbootclasspath/a:$CCD_JPROFILER_LIB_PATH/agent.jar\""
	export CCD_WORKFLOWENGINE_PROFILER_NAME=$profiler
  fi

  if [ "$service" = "scheduler" ]; then
	export CCD_SCHEDULER_JVM_PROFILING_AGENT="\"-Xrunjprofiler:offline,id=204,config=$CCD_JPROFILER_LIB_PATH/config.xml\" \"-Xbootclasspath/a:$CCD_JPROFILER_LIB_PATH/agent.jar\""
	export CCD_SCHEDULER_PROFILER_NAME=$profiler
  fi

  if [ "$service" = "appsvr" ]; then
  
  	if [ "$CCD_APPSVR" = "websphere6" ]; then
		export CCD_APPSVR_PROFILER_NAME=$profiler
  		
		#edit server.xml
		#echo "" 
		#echo "------------ editing WAS config file with profiling agent options --------------"
		CONFIG_PATH=$WAS_PROFILE_HOME/config/cells/$WAS_CELLNAME/nodes/$WAS_NODENAME/servers/$WAS_APPSERVERNAME
		cp $CONFIG_PATH/server.xml $CONFIG_PATH/backup_server.xml
		${WAS_PROFILE_HOME}/bin/wsadmin.sh -conntype none -lang jacl -f ${TOP}/bin/websphere/modify_jvmargs.jacl $WAS_NODENAME $WAS_APPSERVERNAME "-Xrunjprofiler:offline,id=205,config=$CCD_JPROFILER_LIB_PATH/config.xml -Xbootclasspath/a:$CCD_JPROFILER_LIB_PATH/agent.jar -Dprofiler_opts=${CCD_APPSVR_PROFILER_NAME}__${CCD_APPSVR_PROFILER_PORT}"
		#echo "------------ completed editing WAS config file -----------------"
		#echo "" 

  	fi
  	
  	if [ "$CCD_APPSVR" = "weblogic92" ]; then
		export CCD_APPSVR_PROFILER_NAME=$profiler
  		export CCD_APPSVR_JVM_PROFILING_AGENT="\"-Xrunjprofiler:offline,id=205,config=$CCD_JPROFILER_LIB_PATH/config.xml\" \"-Xbootclasspath/a:$CCD_JPROFILER_LIB_PATH/agent.jar\""
	fi
  	
  	if [ "$CCD_APPSVR" = "tomcat33" ]; then
		export CCD_APPSVR_PROFILER_NAME=$profiler
  		export CCD_APPSVR_JVM_PROFILING_AGENT="\"-Xrunjprofiler:offline,id=205,config=$CCD_JPROFILER_LIB_PATH/config.xml\" \"-Xbootclasspath/a:$CCD_JPROFILER_LIB_PATH/agent.jar\""
	fi
  fi
}

function disableProfiling(){
  service=$_CCD_SERVICETYPE
  serviceName=$_CCD_SERVICENAME
  profiler=$_CCD_PROFILERNAME

  message=`verifyParameters $service`
  #on some platforms, checking for empty string does not work. Hence this workaround of prefixing with X and comparing with X
  if [ "X${message}" != "X" ]; then
  	  ${ECHO} $message
      return;
  fi

  #on some platforms, checking for empty string does not work. Hence this workaround of prefixing with X and comparing with X
  if [ "X${_CCD_SERVICETYPE}" = "X" ] ; then
	printMessage "PROFILER_ERROR_SERVICETYPE_PARAMETER_IS_MISSING_SH"
	exit
  fi

  STATUS=`$JAVA_RT -DTOP=$TOP -DCCD_ETC_DIR=$CCD_ETC_DIR -classpath $CLASSPATH com.ibm.ccd.profiler.client.ProfilerCommandLine --status --serviceType=$service --serviceName=$serviceName`

  if [ "$STATUS" = "DISABLED" ] ; then
	  printMessage "PROFILER_INFO_PROFILING_ALREADY_DISABLED_FOR_SERVICE_SH"
	  return;
  fi

  if [ "$service" = "admin" ]; then
        unset CCD_ADMIN_JVM_PROFILING_AGENT
	unset CCD_ADMIN_PROFILER_NAME
	unset CCD_ADMIN_PROFILER_PORT
  fi


  if [ "$service" = "event" ]; then
        unset CCD_EVENTPROCESSOR_JVM_PROFILING_AGENT
	unset CCD_EVENTPROCESSOR_PROFILER_NAME
	unset CCD_EVENTPROCESSOR_PROFILER_PORT
  fi

  if [ "$service" = "queuemgr" ]; then
        unset CCD_QUEUEMANAGER_JVM_PROFILING_AGENT
	unset CCD_QUEUEMANAGER_PROFILER_NAME
	unset CCD_QUEUEMANAGER_PROFILER_PORT	
  fi

  if [ "$service" = "workflow" ]; then
        unset CCD_WORKFLOW_JVM_PROFILING_AGENT
	unset CCD_WORKFLOWENGINE_PROFILER_NAME
	unset CCD_WORKFLOWENGINE_PROFILER_PORT
  fi

  if [ "$service" = "scheduler" ]; then
        unset CCD_SCHEDULER_JVM_PROFILING_AGENT
	unset CCD_SCHEDULER_PROFILER_NAME
	unset CCD_SCHEDULER_PROFILER_PORT
  fi
 
  if [ "$service" = "appsvr" ]; then

        unset CCD_APPSVR_JVM_PROFILING_AGENT
	unset CCD_APPSVR_PROFILER_NAME
	unset CCD_APPSVR_PROFILER_PORT
	if [ "$CCD_APPSVR" = "websphere6" ]; then
	CONFIG_PATH=$WAS_PROFILE_HOME/config/cells/$WAS_CELLNAME/nodes/$WAS_NODENAME/servers/$WAS_APPSERVERNAME
	  if [ -f $CONFIG_PATH/backup_server.xml ]; then
	cp $CONFIG_PATH/backup_server.xml $CONFIG_PATH/server.xml
	  fi
	fi

  fi 
  
  profilerDisabledNextSteps $service $serviceName

}

function unsetParams(){
    unset _CCD_SERVICETYPE
    unset _CCD_SERVICENAME
    unset _CCD_PROFILERNAME
    unset _CCD_PORT

  if [ "$service" = "admin" ]; then
        unset CCD_ADMIN_JVM_PROFILING_AGENT
        unset CCD_ADMIN_PROFILER_NAME
        unset CCD_ADMIN_PROFILER_PORT
  fi


  if [ "$service" = "event" ]; then
        unset CCD_EVENTPROCESSOR_JVM_PROFILING_AGENT
        unset CCD_EVENTPROCESSOR_PROFILER_NAME
        unset CCD_EVENTPROCESSOR_PROFILER_PORT
  fi

  if [ "$service" = "queuemgr" ]; then
        unset CCD_QUEUEMANAGER_JVM_PROFILING_AGENT
        unset CCD_QUEUEMANAGER_PROFILER_NAME
        unset CCD_QUEUEMANAGER_PROFILER_PORT
  fi

  if [ "$service" = "workflow" ]; then
        unset CCD_WORKFLOW_JVM_PROFILING_AGENT
        unset CCD_WORKFLOWENGINE_PROFILER_NAME
        unset CCD_WORKFLOWENGINE_PROFILER_PORT
  fi

  if [ "$service" = "scheduler" ]; then
        unset CCD_SCHEDULER_JVM_PROFILING_AGENT
        unset CCD_SCHEDULER_PROFILER_NAME
        unset CCD_SCHEDULER_PROFILER_PORT
  fi

  if [ "$service" = "appsvr" ]; then

        unset CCD_APPSVR_JVM_PROFILING_AGENT
        unset CCD_APPSVR_PROFILER_NAME
        unset CCD_APPSVR_PROFILER_PORT
  fi
}

printMessage()
{
   $JAVA_RT com.ibm.ccd.common.db.LocalizedShellScriptRunner "${SHELL} wpcprof_printmessage.sh $*" "$TOP/bin"
}

#--------------------------------------------
#         Main method
#--------------------------------------------

flag=0

export CCD_YOURKIT_LIB_PATH=${TOP}/profiler/yourkit
export CCD_JPROFILER_LIB_PATH=${TOP}/profiler/jprofiler
for _VAR in "$@"; do
    case ${_VAR} in
        --enable)
        enableProfiling $2 $3
	flag=1
	;;	
    esac

    case ${_VAR} in
        --disable)
        disableProfiling
	flag=1
	;;
    esac
done
if [ $flag = 0 ]; then

$JAVA_RT -DTOP=$TOP -DCCD_ETC_DIR=$CCD_ETC_DIR -classpath $CLASSPATH com.ibm.ccd.profiler.client.ProfilerCommandLine "$@"

fi
