#!/bin/bash

for running_server_info in $(ps -ef | egrep "Mission|World|Zone"Server | grep '\./' | awk -F" " '{print $2","$NF}' | sed 's|./||' | sort -k2);
do
	#echo ${running_server_info}
	PROCESS_ID=$(echo ${running_server_info}|awk -F"," '{print $1}')
	PROCESS_NAME=$(echo ${running_server_info}|awk -F"," '{print $2}')
	PROCESS_SHORT_NAME=$(echo ${PROCESS_NAME}|sed 's/MissionServer/MS/;s/WorldServer/WS/;s/ZoneServer/ZS/')
	echo "${PROCESS_ID} => ${PROCESS_NAME} (${PROCESS_SHORT_NAME})"

	SCREEN_DIRECTORY="/var/run/screen/S-${USER}"
	SCREEN_NAME="*.${PROCESS_SHORT_NAME}.perf"
	[ -f "${SCREEN_DIRECTORY}/${SCREEN_NAME}" ] && rm -rf ${SCREEN_DIRECTORY}/${SCREEN_NAME}
	screen -S ${PROCESS_SHORT_NAME}.perf -d -m perf top -p ${PROCESS_ID}
done
