#!/bin/bash/
#Find the last line number in the Problem Section by finding the next <component line that is after the last
#line  entry in the MyFileToProcess.txt
#Process each section found in MyFileToProcess.txt one line at a time to determine whether or not they are in the downloadable resource file
#by Beginning at the KeyLineNumber which is $1 proceed down until you find a line beginning with <value and process that line to 
#extract the I9 code found in code="xxxx" and check to see if that is in the list of AllI9ProblemCodes.txt
#if it is not in the list, store the beginning and ending line numbers to prepare to delete the problem.
#Delete the offending Problem
#Delete the text files that were created which are
#PatientProblemEntryTemplateRows.txt
#ProblemCodes.txt
#You will need the following files
#   LineNumberDownFunction
#   AllI9ProblemCodes.txt
#   LineNumberUpDownFunction
#   This file FunctionProcessProblemSection
FunctionProcessProblemSection ()
{
local File
local LastLineInProblemSection
local ProblemCode
local BeginProblemSectionCodeLineNumber
local EndProblemSectionLineNumber
local a
local b
local i
local KeyLineNumber
File="$1"
echo "File is $File"
read
#read
  #Find the code that identifies the near beginning of the problem section
  BeginProblemSectionCodeLineNumber="$(grep -n "11450-4" "$File" | awk -F: '{ print $1 }')"
  echo "BeginProblemSectionCodeLineNumber is $BeginProblemSectionCodeLineNumber"
  #Use the LineNumberDownFunction to find the last line in the problem section
  source LineNumberDownFunction
  LineNumberDownFunction $BeginProblemSectionCodeLineNumber "$BeginProblemSectionCodeLineNumber" '</component>' "$File"
  echo "result of LineNumberDownFunction is $TestSectionInformation which has the last line of the problem section in the second entry with a separator that is a colon"
  #read
  #Parse the Variable that comes out from that function to get the last line of the section
  echo "$TestSectionInformation" | awk -F: '{ print $2 }'
  EndProblemSectionLineNumber="$(echo "$TestSectionInformation" | awk -F: '{ print $2 }')"
  echo "$EndProblemSectionLineNumber is the last line in the problem section"
  #read
  #Search the problem section to extract the problem codes by finding the template lines for problems, seaching the next 10 lines for the value line and parsing that to get the problem code
  echo "BeginProblemSectionCodeLineNumber is $BeginProblemSectionCodeLineNumber and EndProblemSectionLineNumber is $EndProblemSectionLineNumber"
  echo "$File"
  grep -n "2.16.840.1.113883.3.249.11.100.8" "$File" | awk -F: '{ print $1 }' >> PatientProblemEntryTemplateRows.txt 
  #head -"$EndProblemSectionLineNumber" "$File" | tail -"$(($EndProblemSectionLineNumber-$BeginProblemSectionCodeLineNumber))" | grep -n "2.16.840.1.113883.3.249.11.100.8" | awk -F: '{ print $1 }' >> PatientProblemEntryTemplateRows.txt 
  #sed -n "$BeginProblemSectionCodeLineNumber","$EndProblemSectionLineNumber"p "$File" | grep -n "2.16.840.1.113883.3.249.11.100.8" | awk -F: '{ print $1 }' >> PatientProblemEntryTemplateRows.txt 
  cat PatientProblemEntryTemplateRows.txt
  echo "Template 2.16.840.1.113883.3.249.11.100.8 row numbers are above"
  if [ -s PatientProblemEntryTemplateRows.txt ]
  then
    for i in $(cat PatientProblemEntryTemplateRows.txt)
      do
      echo "$i"
      head -"$(( $i+10 ))" "$File" \
        | tail \
	 | grep '<value ' \
	  | awk -F'code="' '{ print $2 }' \
	    | awk -F\" '{ print $1}' >> ProblemCodes.txt 
      done
   fi
  echo "Problem codes in the problem section are"
  cat "ProblemCodes.txt"
  read
  #Now find out what I9 problem codes are not in the downloadable resource and print those to a file
  for i in $(cat ProblemCodes.txt) 
    do 
      cat AllI9ProblemCodes.txt \
	|grep -q "$i";echo $?
      cat AllI9ProblemCodes.txt \
	|grep -q "$i"
	if [ "1" = "$?" ]
	then 
	  echo "There was an problem not found in the Downloadable Resource list of I9 problems"
	  echo "$i"
	  echo "the line above should have a single problem number"
	  grep -n "code=\"$i" "$File" | awk -F: '{ print $1 }'
	  echo "the line above should have a line number where the problem number was found"
	  echo "that line number will be set to KeyLineNumber and handed off to the LineUpDownFunction"
	  KeyLineNumber=$(grep -n "code=\"$i" "$File" | awk -F: '{ print $1 }')
	  echo "The KeyNumber to hand the LineUpDownFunction is $KeyLineNumber"
	  read
	  source LineNumberUpDownFunction
	  LineNumberUpDownFunction "$KeyLineNumber" '<entry ' '</entry>' "$File"
	  echo "above is the output from the LineNumberUpDownFunction. It is in variable called SectionInformation"
	  echo "that has the key line number, the line with <entry and the line with </entry and then the File name"
	  echo "separated by colons"
          a=$(echo "$SectionInformation" | awk -F: '{print $2 }')
	  b=$(echo "$SectionInformation" | awk -F: '{print $3 }')
	  sed -n "$a","$b"d "$File"
	  echo "Problem $i was deleted from $File"
	  echo "Confirming Problem was deleted with grep -q"
	  grep -q "$ProblemToDeleted" "$File"
	  echo " Is the problem deleted? dollar? is $?"
	  #read
	  xmlwf "$File"
	  echo "Just checked to see if $File is well formed and dollar? is $?"
	  read 
	  
	fi
    done
   rm -rf PatientProblemEntryTemplateRows.txt
  rm -rf ProblemCodes.txt
 
  }