#! /bin/sh

#   Oct 28/02 - F.Majaess

#id msgxsvo - filter script to control the amount of data output.

#   AUTHOR  - F. Majaess

#hd PURPOSE - 'msgxsvo' is a filter script used to handle 
#hd           controlling the amount of data output.
#hd           (Intial need for it is to control the amount
#hd            of output propagated from "xsave" calls in 
#hd            "gcmpak.cdk")
#hd

#ex EXAMPLE:
#ex
#ex      xsave OLDEMTY OUTINV NEWGS NEWSLGS input=xsave_input | msgxsvo
#ex

# activate switch to immediate escape to parent shell upon an 
# error condition.

set -e

# Capture lines from the standard input into "Cptr_Fil" local file.

(\rm -f Cptr_Fil || : )
cat > Cptr_Fil

if [ -s "Cptr_Fil" ] ; then
# Provided some input data was captured in "Cptr_Fil"...

# Define "dsply" function which will be called to process the 
# data output...

#  dsply () { set -x ; 
   dsply () { 

    # TrgtFl = Target filename
    #    Fst = first  target line position (optional). 
    #    Snd = Second target line position (optional).

    set +e
    unset TrgtFl Fst Snd ; TrgtFl=$1 Fst=$2 ; Snd=$3 ; 
    set -e

    # Define in "mnlinx" the minimum number of line below which 
    # the whole file contents get output.

    mnlinx=${mnlinx:='200'}
    
    if [ -s "$TrgtFl" ] ; then

     # Provided the input file is not empty ...

     # Set in "Nlines" the number of lines found.
 
     Nlines=`cat ${TrgtFl} | wc -l | sed -e 's/ *//g'`
    #echo "dsply: Nlines=$Nlines mnlinx=$mnlinx TrgtFl=$TrgtFl Fst=$Fst Snd=$Snd"

     if [ "${Nlines}" -gt "${mnlinx}" -a -n "$Fst" -a -n "$Snd" ] ; then

      # Provided the number of lines is above the minimum treshold limit, "Fst" 
      # and "Snd" target line positions is specified, adjust the values of the
      # latter ... then based on the resulting line numbers range decide on
      # whether output the whole file or part of.
      
      Fst=`expr $Fst + 3`
      Snd=`expr $Snd - 3`

      if [ "$Fst" -lt "$Snd" ] ; then
       #echo "dsply: Use sed on $TrgtFl to display lines: 1...${Fst} & ${Snd}...end"
        eval "sed -n -e '1,${Fst}p' ${TrgtFl}" ; echo " ...\n ...\n ..." ; eval "sed -n -e '${Snd},\$p' ${TrgtFl}"
      else
        cat ${TrgtFl}
      fi
     else
       cat ${TrgtFl}
     fi
    fi ;
    }

 # Allow over-riding in "strng" the string marker to search for. 
   
 if [ -n "$1" ] ; then
  strng="$1"
 fi
 strng=${strng:='^0  XSAVE ON-    MODEL OUTPUT'}

 # Obtain the line number(s) matching "strng"
 nocc=`cat Cptr_Fil | sed -n "
 /${strng}/=
 " | tr '\\012' '\\040'`

 # Setup and invoke "dsply" function to handle the data output accordingly.

 nocc="dsply Cptr_Fil $nocc"
# mnlinx=200 ; export mnlinx
# echo "nocc=${nocc}-"
 eval "$nocc"
fi
(\rm -f Cptr_Fil || : )
exit
