
# UNISRC_ID: @(#)gp_list.sh	27.4	85/03/10  
#
#  Script name is /usr/contrib/bin/gp_list.  It is called by
#  /usr/contrib/bin/profile or may be called on its own.
#
#  This script may call /usr/contrib/bin/awk_percent and/or
#  /usr/contrib/bin/awk_shorten to further format the profiler output data.
#
#  The first test is to see if the number of arguments given is outside
#  of the bounds.  Those bounds are 2 and 5.  If the number is outside,
#  a usage message is printed.
#
if  [ $# -le 2 ] 
  then

    echo "usage: $0 -t results_file link_map [ -p | -p -s ] |"
    echo "       $0 results_file link_map destination_file [ -p | -p -s ]"
    exit 1
  fi

if  [ $# -gt 5 ] 
  then

    echo "usage: $0 -t results_file link_map [ -p | -p -s ] |"
    echo "       $0 results_file link_map destination_file [ -p | -p -s ]"
    exit 1
  fi

#  The next test if for the terse mode.  This mode is useful if you 
#  want the output to pipe somewhere else.  It simply writes to standard
#  out.  If the first argument is "-t" then we're using terse mode.  The
#  next test is for additional filters.  There are two additional filters
#  which may be requested beyond those absolutely necessary.  The first
#  filter is one which reformats the profiler output so that percentages
#  of COUNT1 are shown.  The second filter takes this output and removes
#  all lines whose percentage is 0.  The first filter is requested with
#  the "-p" option and the second with the "-s" option.  The test goes 
#  by seeing if there are 5 arguments.  If so, then the 4th and 5th must
#  be -p and -s respectively.  If not, a usage message is printed.
#  If they are, then the profiler raw data is run through all of the 
#  scripts.
#
if [ $1 = '-t' ]
  then

      if  [ $# -eq 5 ] 
        then 

          if [ $4 = '-p' ] 
            then 

              if [ $5 = '-s' ]
                then 

                   echo "Profiler raw data ($2) being formatted using"
		   echo "link map ($3) and filters to generate percentage"
		   echo "and shorten the output."

		   /usr/contrib/bin/gplist $2 | sort -nr | profmt -m $3 | \
		     awk -f /usr/contrib/bin/awk_percent | \
		     awk -f /usr/contrib/bin/awk_shorten
                else
	          echo "usage: $0 -t results_file link_map [ -p | -p -s ]"
                fi
             else 
	       echo "usage: $0 -t results_file link_map [ -p | -p -s ]"
             fi

#  If there were only 4 arguments, then the 4th must be -p.  If it is,
#  then the profiler data is run through the awk script which generates
#  percentages of COUNT1.  Otherwise, a usage message is printed.
#
        else if [ $# -eq 4 ] 
	  then

            if  [ $4 = '-p'  ] 
	      then

                 echo "Profiler raw data ($2) being formatted using"
		 echo "link map ($3) and filter to generate percentage."

		 /usr/contrib/bin/gplist $2 | sort -nr | profmt -m $3 | \
		    awk -f /usr/contrib/bin/awk_percent
              else
	        echo "usage: $0 -t results_file link_map [ -p | -p -s ]"

              fi

#  If there were only 3 arguments, then neither of the addtional filters
#  has been requested and the raw data is run through the necessary filters
#  and then printed to standard out.
#
	  else if [ $# -eq 3 ]
	    then

               echo "Profiler raw data ($2) being formatted using"\
                    "link map ($3)."

	      /usr/contrib/bin/gplist $2 | sort -nr | profmt -m $3

            else
	      echo "usage: $0 -t results_file link_map [ -p | -p -s ]"
            fi
          fi
        fi

#  If the first argument wasn't a -t, then the script will send output
#  to a specified file.  If the additional filters are requested, new
#  files will be generated, based upon the name of the specified output
#  file.  Specifically, the file name appended with ".p" will be used for
#  the percentage data, and the file name appended with ".s" will be
#  used for the shortened version.  Therefore, a warning message that
#  the specified file name must be 12 characters or less is printed and
#  the raw data is run through the first necessary filters.  This data
#  is written to the specified file.
#

    else if [ $# -ge 3 ]
      then

	echo "Destination_file name ($3) must be less than 13 characters"
        echo " "
        echo "Profiler raw data ($1) being formatted using link map ($2)"
        echo " and results written to $3."
        echo " "

	/usr/contrib/bin/gplist $1 | sort -nr | profmt -m $2 > $3

#  If there were 4 or 5 arguments passed, then one or both of the additional
#  filters has been requested.  If the value of the 4th argument is -p, 
#  then the percentage generator script is used and the results are printed
#  to the requested file name appended with .p.  If the argument isn't 
#  -p then a usage message is printed.
#
        if  [ $# -ge 4 ]
	  then

	    if  [ $4 = '-p'  ] 
              then

                name=$3.p

		echo "Profiler data being further formatted to contain"\
                     "percentages of total"
		echo " COUNT1's."
                echo " "

		awk -f /usr/contrib/bin/awk_percent $3 > $name

#  If there were 5 arguments, and the 5th is -s the output file from
#  the percentage generator is passed through the shortening script.
#  The output is printed to the specified file appended with .s.  Otherwise
#  a usage message is printed.
#
	        if [ $# -eq 5 ] 
	          then
	      
		    if [ $5 = '-s' ]
		      then

                        name=`basename $name .p`.s
			echo "Profiler data being shortened by removing" \
                             "data for routines 0% of the total"
			echo " COUNT1."
                        echo " "

			awk -f /usr/contrib/bin/awk_shorten $3.p > $name

                      else 
	                echo "usage: $0 results_file link_map destination_file"\
                             " [ -p | -p -s ]"
                      fi
                   fi

               else
	         echo "usage: $0 results_file link_map destination_file "\
	              "[ -p | -p -s ]"
               fi
           fi
       fi
  fi
