#!/bin/bash
#<pre><b>
# Script     : makerun
# Version    : 2.4
# Author     : Roland Rashleigh-Berry
# Date       : 19-Jul-2007
# Purpose    : Production script to create scripts named "runderived",
#              "runreports" and "runsuite" used for running all the programs
#              in a study.
# SubScripts : derorder intitlesds intitlis crtitlesds getfields
# Notes      : You must run this in the programs directory. It will create three
#              scripts that can run all the programs for a study, "runderived"
#              to build the derived datasets, "runreports" to create the reports
#              (excluding any "extra" analysis members) and "runsuite" to run
#              "runderived" and "runreports".
# Usage      : makerun
#===============================================================================
# PARAMETERS:
#-pos- -------------------------------description-------------------------------
# N/A  Do not supply any parameters
#===============================================================================
# AMENDMENT HISTORY:
# init --date-- mod-id ----------------------description------------------------
# rrb  27Sep06         v 1.1 has an email alert added for when the script ended
#                      due to an error during "runderived".
# rrb  16Feb07         v 2.0 has donelist.tmp processing added and reflects the
#                      changed "donepages" script.
# rrb  22Feb07         Extra fields added for client and office and set up
#                      OUTDIRWIN for Windows path style for Cygwin.
# rrb  25Feb07         Use "rm" instead of the safer "rem" in runreports and
#                      for the deletion of program logs etc. in runderived.
# rrb  27Feb07         Use "ssmtp" email program for Cygwin
# rrb  04Mar07         "getfields" script used
# rrb  15Jul07         Exclude "extra" analysis programs
# rrb  19Jul07         Use "intitlis" script instead of "intitlabels"
# rrb  19Jul07         Set up DONELIST as a system environment variable
#===============================================================================
# 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: (makerun) Do not supply any parameters" 1>&2
  exit 2
fi

# run crtitlesds to make sure the titles dataset is there
crtitlesds

# identify user
iam=$(whoami)


# delete any generated scripts already there
rm -f runderived runreports runsuite

# 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.
derorder | awk -F. '{print $1}' > ~/dprogs_$$.tmp


# get a list of reporting sas programs and their lisfile names
# (excluding "extra" analysis programs)
intitlis | grep -v ^x | sed 's%\r$%%' > ~/rproglis_$$.tmp


# get a list of reporting sas programs (excluding "extra" analysis)
intitlesds | grep -v ^x | sed 's%\r$%%' > ~/rprogs_$$.tmp



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


cat > runderived << FINISH
#!/bin/bash
# Script     : runderived
# Author     : This script was automatically generated by the "makerun" script.
# Date       : $datetime
# Client     : $client
# Office     : $office
# Drug       : $drug
# Protocol   : $study
# Inc        : $inc
# Purpose    : To run the programs that build the derived datasets
# SubScripts : crtitlesds rem
# Notes      : If you edit this script to limit the number of datasets built
#              then note that there is a delete of all derived datasets in the
#              der. and stat. libraries near the start using "rem" and a delete
#              of all programs .log, .lst and .chk members using "rm -f". You
#              should comment out the "rem" and "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. It is recommended that for interim
#              runs, you comment out the lines that delete all the stats and der
#              datasets.
#
#              Note that this script was generated by the "makerun" script.
#              Next time "makerun" 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:       ./runderived 1>runderived.log 2>runderived.err
#

# Define error exit routine
errorexit () {

  # email the user telling them the job ended due to errors
  msg="An error forced the derived datasets build script to end"
  iam=\$(whoami)
  sendto=\$(emailaddr \$iam)

  # Use "ssmtp" for Cygwin and "mailx" for Unix
  if [ -n "\$CYGWIN" ] ; then
/usr/sbin/ssmtp -t << --END--
To: \$sendto
Subject: \$msg

\$msg
--END--

  else
    echo "\$msg" | mailx -s "\$msg" \$sendto
  fi
  
  # say when the script ended
  echo "This script ended due to errors at \$(date)"
  echo "This script ended due to errors at \$(date)" 1>&2
  exit 2
}


# identify user
iam=\$(whoami)


# say who is running this script and what date and time
echo "This script was started by \$iam (\$(getname \$iam)) on \$(date)"
echo "This script was started by \$iam (\$(getname \$iam)) on \$(date)" 1>&2



# Delete the "der" and "stat" derived datasets so long
# as they have group write permission.
rem $der/*.sas7bdat
rem $stat/*.sas7bdat


# Re-create the titles and protocol datasets
crtitlesds


# For all programs, delete the .log, .lst and .chk if they exist
# and there is group write permission.
FINISH
#==============================================================
while read progname
do
  echo "rm -f ${progname}.log ${progname}.lst ${progname}.chk" >> runderived
done < ~/dprogs_$$.tmp

cat >> runderived << FINISH


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


while read progname
do
cat >> runderived << FINISH
sasb $progname
if [ \$? -gt 1 ] ; then errorexit ; fi
FINISH
done < ~/dprogs_$$.tmp



cat >> runderived << FINISH


# cat all the .chk files onto an empty runderived.chk
rm -f runderived.chk
FINISH


while read progname
do
  echo "cat ${progname}.chk >> runderived.chk" >> runderived
done < ~/dprogs_$$.tmp


cat >> runderived << FINISH


# email the user telling them the job has ended
msg="the derived datasets build script has finished"
iam=\$(whoami)
sendto=\$(emailaddr \$iam)

# Use "ssmtp" for Cygwin and "mailx" for Unix
if [ -n "\$CYGWIN" ] ; then
/usr/sbin/ssmtp -t << --END--
To: \$sendto
Subject: \$msg

\$msg
--END--
else
  echo "\$msg" | mailx -s "\$msg" \$sendto
fi


# say when the script ended
echo "This script finished at \$(date)"
echo "This script finished at \$(date)" 1>&2
FINISH


# Make "runderived" an executable file
chmod +x runderived






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


cat > runreports << FINISH
#!/bin/bash
# Script     : runreports
# Author     : This script was automatically generated by the "makerun" script.
# Date       : $datetime
# Client     : $client
# Office     : $office
# Drug       : $drug
# Protocol   : $study
# Inc        : $inc
# Purpose    : To run the programs that create reports
# SubScripts : crtitlesds donepages
# Notes      : If you edit this script to limit the number of reports produced
#              then note that there is a delete of all programs .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 "makerun" script.
#              Next time "makerun" 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:       ./runreports 1>runreports.log 2>runreports.err
#


# identify user
iam=\$(whoami)

# say who is running this script and what date and time
echo "This script was started by \$iam (\$(getname \$iam)) on \$(date)"
echo "This script was started by \$iam (\$(getname \$iam)) on \$(date)" 1>&2


# Create the titles and protocol datasets
crtitlesds


# Set up an environment variable for routing the output
OUTDIR=\$PWD

# convert to a Windows path style for Cygwin
OUTDIRWIN=\$(echo "\$OUTDIR" | sed -e 's%^/cygdrive/%%' \
-e 's%\(^.\)%\1:%' -e 's%/%\\\\%g')

export OUTDIR OUTDIRWIN


# Delete the "donelist" files
rm -f \$OUTDIR/donelist.txt \$OUTDIR/donelist.tmp

# Set up an environment variable for the donelist temporary file
DONELIST=donelist.tmp
export DONELIST



# 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 " " >> runreports
      fi
      echo -n "rm -f ${progname}.log ${progname}.lst ${progname}.chk ${lisfile}" >> runreports
      holdprog=$progname
    else
       echo -n " ${lisfile}" >> runreports
    fi
  fi
done < ~/rproglis_$$.tmp
echo " " >> runreports



cat >> runreports << FINISH


# Run all programs using sasb
FINISH


while read progname
do
if [ -f "${progname}.sas" ] ; then
echo "sasb $progname" >> runreports
fi
done < ~/rprogs_$$.tmp



cat >> runreports << FINISH


# cat all the .chk files onto an empty runreports.chk
rm -f runreports.chk
FINISH

while read progname
do
if [ -f "${progname}.sas" ] ; then
echo "cat ${progname}.chk >> runreports.chk" >> runreports
fi
done < ~/rprogs_$$.tmp


cat >> runreports << FINISH


# add number of pages and sort "donelist" into order
donepages \$OUTDIR/donelist.tmp | sort > \$OUTDIR/donelist.txt


# email the user telling them the job has ended
msg="the report production script has finished"
iam=\$(whoami)
sendto=\$(emailaddr \$iam)

# Use "ssmtp" for Cygwin and "mailx" for Unix
if [ -n "\$CYGWIN" ] ; then
/usr/sbin/ssmtp -t << --END--
To: \$sendto
Subject: \$msg

\$msg
--END--
else
  echo "\$msg" | mailx -s "\$msg" \$sendto
fi


# say when the script ended
echo "This script finished at \$(date)"
echo "This script finished at \$(date)" 1>&2
FINISH


# Make "runreports" an executable file
chmod +x runreports





#####################################################
#####################################################
####               RUNSUITE SCRIPT               ####
#####################################################
#####################################################

cat > runsuite << FINISH
#!/bin/bash
# Script     : runsuite
# Author     : This script was automatically generated by the "makerun" script.
# Date       : $datetime
# Client     : $client
# Office     : $office
# Drug       : $drug
# Protocol   : $study
# Inc        : $inc
# Purpose    : To run the scripts "runderived" and "runreports" that build the
#              derived datasets and create the reports.
# SubScripts : runderived runreports
# Notes      : You should not run this script directly as it is normal to use
#              "fullrunsuite" which will create fresh copies of this script as
#              well as "runderived" and "runreports" as well as routing standard
#              output and error output to files.
#
# Usage:       ./runsuite 1>runsuite.log 2>runsuite.err
#


# identify user
iam=\$(whoami)


# say who is running this script and what date and time
echo "This script was started by \$iam (\$(getname \$iam)) on \$(date)"
echo "This script was started by \$iam (\$(getname \$iam)) on \$(date)" 1>&2


# build the derived datasets and exit if there was an error
./runderived
if [ \$? -gt 0 ] ; then
  # say the script ended due to errors
  echo "This script ended due to an error in \"runderived\" at \$(date)"
  echo "This script ended due to an error in \"runderived\" at \$(date)" 1>&2
  exit 2
fi


# create the reports
./runreports


# say when the script ended
echo "This script finished at \$(date)"
echo "This script finished at \$(date)" 1>&2
FINISH


# Make "runsuite" an executable file
chmod +x runsuite









# Delete the temporary files now we are finished
rm -f ~/dprogs_$$.tmp ~/rprogs_$$.tmp ~/rproglis_$$.tmp
