#!/bin/bash
#<pre><b>
# Script     : makexrun
# Version    : 1.1
# Author     : Roland Rashleigh-Berry
# Date       : 19-Jul-2007
# Purpose    : Script to create script named "runxreports" used for running all
#              the "extra analysis" programs in a study.
# SubScripts : intitlesds intitlis crtitlesds getfields
# Notes      : You must run this in the programs directory
# Usage      : makexrun
#===============================================================================
# PARAMETERS:
#-pos- -------------------------------description-------------------------------
# N/A  Do not supply any parameters
#===============================================================================
# AMENDMENT HISTORY:
# init --date-- mod-id ----------------------description------------------------
# 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: (makexrun) 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 runxreports

# 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}')


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


# get a list of extra analysis reporting programs
intitlesds | grep ^x | sed 's%\r$%%' > ~/rprogs_$$.tmp



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


cat > runxreports << FINISH
#!/bin/bash
# Script     : runxreports
# Author     : This script was automatically generated by the "makexrun" script.
# Date       : $datetime
# Client     : $client
# Office     : $office
# Drug       : $drug
# Protocol   : $study
# Inc        : $inc
# Purpose    : To run the programs that create extra analysis 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 "makexrun" script.
#              Next time "makexrun" 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:       ./runxreports 1>runxreports.log 2>runxreports.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/xdonelist.txt \$OUTDIR/xdonelist.tmp

# Set up an environment variable for the donelist temporary file
DONELIST=xdonelist.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 " " >> runxreports
      fi
      echo -n "rm -f ${progname}.log ${progname}.lst ${progname}.chk ${lisfile}" >> runxreports
      holdprog=$progname
    else
       echo -n " ${lisfile}" >> runxreports
    fi
  fi
done < ~/rproglis_$$.tmp
echo " " >> runxreports



cat >> runxreports << FINISH


# Run all programs using sasb
FINISH


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



cat >> runxreports << FINISH


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

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


cat >> runxreports << FINISH


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


# email the user telling them the job has ended
msg="the extra analysis 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 "runxreports" an executable file
chmod +x runxreports







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