
# UNISRC_ID: @(#)instr.sh	27.1	85/03/10  
#  FILE NAME instr
#
#  This shell script is used to instrument and run a program at either the
#  routine or statment level for optimization profiling using the SOFTOOL
#  Instrumenter.  If it is called to instrument at the routine level, make
#  is used on the program to re-generate all '.o' files after instrumenting
#  the source at the routine level.  The program can be optionally run to
#  generate the profiling reports.  
#
#  If the script is run to instrument at the statement level, it must be
#  called with a file as an argument which contains the names of all the
#  routines which should be instrumented to this level.  In this case,
#  make is again used, this time to instrument at the statement
#  level and recompile the requested routines.
#
#  A run time control file for the optimization instrumenter is generated
#  from the list of routines passed to 'instr'.
#
#  Finally, the program can optionally be run, generating the profiling
#  reports.
#
#  In either case, if the program is run, the profiling reports are generated
#  in a file called 'runtm.L'.  This name is changed to 'ALL.rprofile' in
#  the case of a routine level run, and 'ALL.sprofile' in the case of a
#  statement level run.
#
#  SYNTAX:  instr [y|n] [ [r] | [s file] ]
#
#  Where: [y|n] tells the script to run the program after instrumentation
#           and compilation and linking are complete;
#         [r|s] specifies instrumentation at the routine or statement level;
#         [file] is a file containing the names of routines which are to 
#           be instrumented to the statement level
#           (names DO NOT end in '.f' or '.o').
#
#  Added capability: 2/27/83
#
#  If the Softool INSTRUMENTER is not present in the system in the default
#  location (/usr/softool/pedir) this script will not execute.  It will 
#  terminate with an error message.
#
if [ ! -x /usr/softool/pedir/INSTPREP ]
  then
    echo "Softool Instrumenter not available; instr cannot execute."
    exit 1

 else
    if [ -d SCCS ]
      then
         PROJECTDIR=`pwd`
      else
         echo {PROJECTDIR:=`pwd`} >> make.log
      fi

    export PROJECTDIR

    if [ ! -s file.list ]
      then
        get file.list 1>>make.log  2>>make.err
    fi

    if [ $# -le 1 ]
      then
        echo "usage: instr [y|n] [ [r] | [s file] ]"
    
    elif [ $2 = "r" ]
     then
       rm -f make.log make.err compile.err *.obj 
    
       while read i
         do
           dir=`dirname $i`
           rm -f $dir/`basename $i .f`.o
         done < file.list 1>>make.log 2>>make.err
    
       echo "beginning routine level instrumentation make process" >> make.log
    
       make -r OPERATION1="OPERATION1=/usr/contrib/bin/f_rinstr" FFLAGS="FFLAGS="\
               NAME=ALL.rinstr LIBS=-linstropt1 1>>make.log 2>>make.err
       cp /usr/contrib/bin/rout.ctl runtm.ctl
       if test ! -s compile.err
         then
           if [ $1 = "y" ]
             then 
               echo "beginning program execution"
               ALL.rinstr
               mv runtm.L ALL.rprofile
           fi
       fi
    
     elif [ $2 = "s" ]
       then 
         rm -f make.log make.err compile.err tmp_rmfiles tmp.remove
	 awk -f /usr/contrib/bin/awk_sctl $3
	 awk -f /usr/contrib/bin/awk_rm $3
    
         while read i
           do
              grep $i.f file.list >> tmp.remove
           done < tmp_rmfiles
         
	 ex tmp.remove < /usr/contrib/bin/remove.ed 1>> make.log 2>> make.err
    
         while read i
           do
             rm -f $i 1>>make.log 2>>make.err
           done < tmp.remove
        
         rm -f *.obj
         echo "beginning statement level instrumentation make process" \
                >> make.log
    
	 make -r OPERATION1="OPERATION1=/usr/contrib/bin/f_sinstr"\
                 FFLAGS="FFLAGS=" NAME=ALL.sinstr LIBS=-linstropt2\
                   1>>make.log 2>>make.err
         cp stmt.ctl runtm.ctl
         if test ! -s compile.err
           then
             if [ $1 = "y" ]
               then 
                 echo "beginning program execution"
                 ALL.sinstr
                 mv runtm.L ALL.sprofile
             fi
          fi
     else
       echo "usage: instr [y|n] [ [r] | [s file] ]"
    rm -f tmp_rmfiles tmp.remove
    fi
fi
