#!/bin/bash

touch oidResult.txt
rm oidResult.txt

echo "This script will gather all the OIDs in a single file or all files in /usr/local/nmis8/models.  It will them compare them to nmis8/mibs/nmis_mibs.oid and notify if any are not found there."
echo ""
echo -n "Enter the file name to evaluate or press ENTER to evaluate all files in /usr/local/nmis8/models: "
read ACTION

if [[ $ACTION != "" ]]; then
	fgrep "'oid' =>" /usr/local/nmis8/models/$ACTION > temp.oidsearch.txt
else
	fgrep "'oid' =>" /usr/local/nmis8/models/* > temp.oidsearch.txt
fi

> oidParse.txt
> parsedNmisOid.txt
echo "Processing nmis_nmibs.oid..."
cat /usr/local/nmis8/mibs/nmis_mibs.oid | while read i; do
	VAR=$(echo $i | awk '{print $1}' | tr '"' ' ')
	echo $VAR >> parsedNmisOid.txt
done

echo " Processing all files in nmis8/models..."
cat temp.oidsearch.txt | while read i; do
	oidA=$( echo $i | egrep -o '=.*' | awk '{print $2}' | awk -F "'" '{print $2}' | egrep -v '^1\.|^\.1\.' | awk -F '.' '{print $1}')
	if [[ $oidA == "" ]]; then
		oidA=$( egrep -o '=.*' temp.oidsearch.txt | awk '{print $2}' | awk -F '"' '{print $2}' | egrep -v '^1\.|^\.1\.' | awk -F '.' '{print $1}')
	fi
	if ! [[ $oidA == "" ]]; then
	echo $oidA >> oidParse.txt
	fi
	modelOid+=($oidA)
done
sort oidParse.txt | uniq > oidFinalParse.txt

echo "Searching for model OIDs in nmis_mibs.oid..."
cat oidFinalParse.txt | while read i; do
	touch state.txt
	rm state.txt
	#echo "$i from models"
	cat parsedNmisOid.txt | while read j;  do
		#echo "$i $j"
		if [[ "$i" == "$j" ]]; then 
			#echo "MATCH!!!!!!!!!!!!!!!!!!!!!!!!!!!"
			touch state.txt
		fi
		
	done
	if ! [[ -f state.txt ]]; then 
	echo "$i is not found in nmis_mibs.oid"
	fgrep $i temp.oidsearch.txt
	echo ""
	echo "$i is not found in nmis_mibs.oid" >> oidResult.txt
	fgrep $i temp.oidsearch.txt >> oidResult.txt
	echo "" >> oidResult.txt
	fi
done 

if [[ -f oidResult.txt ]]; then
	echo ""
	echo "All results can be found in the file oidResult.txt"
else
	echo ""
	echo "All OIDs were round to be in nmis8/models/nmis_mibs.oid"
fi

rm temp.oidsearch.txt
rm parsedNmisOid.txt
rm oidParse.txt
rm oidFinalParse.txt
touch state.txt
rm state.txt

