#!/bin/bash
#<pre><b>
# Script     : makemyrun
# Version    : 1.1
# Author     : Roland Rashleigh-Berry
# Date       : 19-Jul-2007
# Purpose    : Create scripts named "runder$USER" and "runrep$USER" for running 
#              all the programs of a user to build derived datasets and produce
#              reports.
# SubScripts : intitlesds intitlis suffix owner sortnpref getfields
# Notes      : You must run this in the programs directory. It will create a
#              script to run only your own programs.
# Usage      : makemyrun 
#===============================================================================
# PARAMETERS:
#-pos- -------------------------------description-------------------------------
# N/A  Do not supply any parameters
#===============================================================================
# AMENDMENT HISTORY:
# init --date-- mod-id ----------------------description------------------------
# rrb  28Feb07         "client" and "office" fields added to headers
# rrb  04Mar07         "getfields" script used
# rrb  19Jul07         Use "intitlis" script instead of "intitlabels"
#===============================================================================
# This is public domain software. No guarantee as to suitability or accuracy is
# given or implied. User uses this code entirely at their own risk.
#===============================================================================

# No parameters are required
if [ $# -gt 0 ] ; then
  echo "Error: (makemyrun) Do not supply any parameters" 1>&2
  exit 2
fi

# strip out information returned by the "getfields" script
client=$(getfields | cut -d/ -f1)
office=$(getfields | cut -d/ -f2)
drug=$(getfields | cut -d/ -f3)
study=$(getfields | cut -d/ -f4)
inc=$(getfields | cut -d/ -f5)


# datetime
datetime=$(date '+%e-%b-%Y at %H:%M:%S')


# the "der" directory
der=$(pwd | gawk -F/ '{OFS="/";$5="dat";$9="der";print $0}')


# the "stat" directory
stat=$(pwd | gawk -F/ '{OFS="/";$5="dat";$9="stat";print $0}')


# List all the valid derived dataset build programs in order they
# will be run out to a file but drop the file extension.
ls -1 [sd]*[0-9]_*.sas | grep '^[sd][^_]*[0-9]_.*\.sas$' | owner | \
 gawk '$1=="'$USER'" {print $2}' | sortnpref | gawk -F. '{print $1}' > $HOME/dprogs.tmp


# get a list of reporting sas programs and their labels
intitlis | gawk '{print $1 ".sas " $2}' | owner | \
gawk '$1=="'$USER'" {print $2, $3}' | sed 's%\.sas%%'  > $HOME/rproglis.tmp


# get a list of reporting sas programs
intitlesds | suffix .sas | owner | gawk '$1=="'$USER'" {print $2}' | \
 sed 's%\.sas%%' > $HOME/rprogs.tmp



#####################################################
#####################################################
####           DERIVED DATASETS BUILD            ####
#####################################################
#####################################################


cat > runder$USER << FINISH
#!/bin/bash
# Script     : runder$USER
# Author     : This script was automatically generated by the "makemyrun" script
# Date       : $datetime
# Client     : $client
# Office     : $office
# Drug       : $drug
# Protocol   : $study
# Inc        : $inc
# Purpose    : To run programs owned by $USER that build the derived datasets
# SubScripts : crtitlesds
# Notes      : If you edit this script to limit the number of datasets built
#              then note that there is a delete of all program .log, .lst and
#              .chk members by using "rm -f".  You should comment out the
#              "rm -f " lines that do not apply by putting a hash in front of
#              these commands as is the case with all the lines in this header. 
#
#              Note that this script was generated by the "makemyrun" script.
#              Next time "makemyrun" is run, it will overwrite this member.
#
#              Note that to run this script you must use the command exactly
#              as shown in the usage notes below with the study programs
#              directory the current directory
#
# Usage:       ./runder$USER 
#


# For all programs, delete the .log, .lst and .chk if they exist
FINISH
#==============================================================
while read progname
do
  echo "rm -f ${progname}.log ${progname}.lst ${progname}.chk" >> runder$USER
done < $HOME/dprogs.tmp

cat >> runder$USER << FINISH


# Run all programs using sasb and exit if a
# non-zero return code is detected.
FINISH


while read progname
do
cat >> runder$USER << FINISH
sasb $progname
if [ \$? -gt 0 ] ; then exit 2 ; fi
FINISH
done < $HOME/dprogs.tmp



cat >> runder$USER << FINISH


# cat all the .chk files onto an empty runder$USER.chk
rm -f runder$USER.chk
FINISH


while read progname
do
  echo "cat ${progname}.chk >> runder$USER.chk" >> runder$USER
done < $HOME/dprogs.tmp



# Make "runder$USER" an executable file
chmod +x runder$USER






#####################################################
#####################################################
####                CREATE REPORTS               ####
#####################################################
#####################################################


cat > runrep$USER << FINISH
#!/bin/bash
# Script     : runrep$USER
# Author     : This script was automatically generated by the "makemyrun" script
# Date       : $datetime
# Client     : $client
# Office     : $office
# Drug       : $drug
# Protocol   : $study
# Inc        : $inc
# Purpose    : To run a programs owned by $USER that create reports
# SubScripts : crtitlesds rem
# Notes      : If you edit this script to limit the number of reports produced
#              then note that there is a delete of all program .log, .lst, .chk 
#              and .lis members using "rm -f". You should comment out the "rm -f"
#              lines that do not apply by putting a hash in front of these
#              commands as is the case with all the lines in this header.
#
#              Note that this script was generated by the "makemyrun" script.
#              Next time "makemyrun" is run, it will overwrite this member.
#
#              Note that to run this script you must use the command exactly
#              as shown in the usage notes below with the study programs
#              directory the current directory.
#
# Usage:       ./runrep$USER 
#



# Create the titles and protocol datasets
crtitlesds


# For all programs, delete the .log, .lst, .chk and .lis* if they exist
FINISH


holdprog=
while read line
do
  progname=$(echo $line | gawk -F' ' '{print $1}')
  lisfile=$(echo $line | gawk -F' ' '{print $2}')
  if [ -f "${progname}.sas" ] ; then
    if [ "$progname" != "$holdprog" ] ; then
      if [ -n "$holdprog" ] ; then
        echo " " >> runrep$USER
      fi
      echo -n "rm -f ${progname}.log ${progname}.lst ${progname}.chk ${lisfile}" >> runrep$USER
      holdprog=$progname
    else 
       echo -n " ${lisfile}" >> runrep$USER
    fi
  fi
done < $HOME/rproglis.tmp
echo " " >> runrep$USER



cat >> runrep$USER << FINISH


# Run all programs using sasb
FINISH


while read progname
do
if [ -f "${progname}.sas" ] ; then
echo "sasb $progname" >> runrep$USER
fi
done < $HOME/rprogs.tmp



cat >> runrep$USER << FINISH


# cat all the .chk files onto an empty runrep$USER.chk
rm -f runrep$USER.chk
FINISH

while read progname
do
if [ -f "${progname}.sas" ] ; then
echo "cat ${progname}.chk >> runrep$USER.chk" >> runrep$USER
fi
done < $HOME/rprogs.tmp




# Make "runrep$USER" an executable file
chmod +x runrep$USER





# Delete the temporary files now we are finished
rm -f $HOME/dprogs.tmp $HOME/rprogs.tmp $HOME/rproglis.tmp
