#!/bin/bash

# script file to get OID numbers with REST selected
#create temporary files
file1="/tmp/file1"
file2="/tmp/file2"
file3="/tmp/file3"
file4="/tmp/file4"
OIDFILE="/tmp/rest_event_oid"
OIDFILE1="/tmp/oid1"
OIDFILE2="/tmp/oid2"
cat /etc/fiad-conf/mmevent.conf | sed 's/ //g' >$file4
cat $file4 | sed 's/{//g' >$file3
cat /epygi/etc/mmevent-static.conf | sed 's/ //g' >$file4
sed 's/section/section /g' "$file3" > $file1
sed 's/section/section /g' "$file4" > $file2

# Get unique section names from file1 where the 7th bit of ACTION is 1, in case when REST seleted
section_names=$(grep -B 1 -e "ACTION=[1][3-9][0-9]" -e "ACTION=[1][2][8-9]" -e "ACTION=[2][0-4][0-9]" -e "ACTION=[3][8][4-9]" -e "ACTION=[3][9][0-9]" -e "ACTION=[4-5][0-9][0-9]" "$file1" | grep "section" | awk '{print $2}')
isECQX=$(getproductname | grep "ecQX" )
if [ "$isECQX" = "" ] ; then
    section_names=$(echo "$section_names" | sed 's/LicenseClienterror//g')
    section_names=$(echo "$section_names" | sed 's/LicenseClientsuccess//g')
fi
echo "{" >$OIDFILE1
echo " \"OIDS\":[" >>$OIDFILE1

# Iterate through the section names and extract the corresponding OID from file2
for section_name in $section_names; do
    oid=$(awk -v section_name="$section_name" '/^section/ {flag=0} $0 ~ section_name {flag=1} flag && /OID=/ {print $NF; exit}' "$file2")
    if [ -n "$oid" ]; then
        echo "$oid," >> $OIDFILE1
    fi
done
if [ -n "$oid" ]; then
    sed '$d' "$OIDFILE1" > "$OIDFILE2"
    echo "$oid" >> $OIDFILE2
    echo "]" >>$OIDFILE2
    echo "}" >>$OIDFILE2
    sed 's/OID=1\.3\.6\.1\.4\.1\.16459\.0\.//g' "$OIDFILE2" > $OIDFILE
else
    echo "{" >$OIDFILE
    echo " \"OIDS\":[" >>$OIDFILE
    echo "]" >>$OIDFILE
    echo "}" >>$OIDFILE
fi
cat $OIDFILE
rm -f $file1 $file2 $file3 $file4 $OIDFILE1 $OIDFILE2 $OIDFILE
exit 0
