#!/bin/bash

IFS=$'\n' 

reach_end_struct=false

for re in `sed  -n "/typedef enum/,/} WaitEvent.*;$/p" ./src/include/utils/wait_event.h | grep -v typedef | grep -v "{"`
do 
IFS=$'\n' 
eve=`echo "$re" | sed 's/,$//'`

if [[ "$re" == *"="* ]]; then
eve=`echo $re | sed 's/ = .*$//'`
fi

if [[ "$re" == *";" ]]; then
  stru=`echo $re | sed 's/^} //' | sed 's/;$//'`
  reach_end_struct=true
else
	if [ "$reach_end_struct" = true ] ; then
	echo $eve > wetempo.txt
	reach_end_struct=false
	else
	echo $eve >> wetempo.txt
	fi
fi

if [ "$reach_end_struct" = true ] ; then
unset IFS
for i in `cat wetempo.txt`
do 
waitxt=`sed  -n "/case $i:/{n;p}" src/backend/utils/activity/wait_event.c | sed 's/^.*event_name = //' | sed 's/;$//'`
noq=`echo $waitxt | sed 's/"//g'`
desc=`sed  -n "/<entry><literal>$noq/,/<\/entry>$/p" doc/src/sgml/monitoring.sgml | sed 's/^.*<\/literal><\/entry>//' | sed 's/^.*<entry>//' | sed 's/<\/entry>$//'`
desc=`echo $desc | sed 's/^ *//'`
echo -e "$stru\t"$i"\t"$waitxt"\t\""$desc\" >> ./src/backend/utils/activity/waiteventnames.txt
done
fi

done

rm wetempo.txt
