#!/bin/bash
#<pre><b>
# Script     : crtitlesds
# Version    : 2.4
# Author     : Roland Rashleigh-Berry
# Date       : 20-Jul-2007
# Purpose    : Production script to create the titles dataset from the
#              .titles files and the protocol dataset from the protocol.txt file
#              (uses SAS).
# SubScripts : vtitles alltitles checktitles intnop getfields crtitlesds_win
#              lisremap
# SubMacros  : %allocw %crprotds %crtitlesds
# Notes      : none
# Usage      : crtitlesds
#===============================================================================
# PARAMETERS:
#-pos- -------------------------------description-------------------------------
# N/A  Do not supply any parameters
#===============================================================================
# AMENDMENT HISTORY:
# init --date-- mod-id ----------------------description------------------------
# rrb  24Nov05         -stdio used for sas invocation
# rrb  11Feb07         Call crtitlesds_win for Windows configurations
# rrb  20Feb07         Call %allocw macro
# rrb  25Feb07         New fields
# rrb  02Mar07         Use "&_ptlibref_.." instead of "der."
# rrb  04Mar07         "getfields" script used
# rrb  20Jul07         "lisremap" called
#===============================================================================
# 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 allowed
if [ $# -gt 0 ] ; then
  echo "Error: (crtitlesds) Do not supply any parameters" 1>&2
  exit 1
fi

# if a Windows config then call crtitlesds_win and exit on return
if [ -n "$CYGWIN" ] ; then
  crtitlesds_win
  exit $?
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')

# check the protocol.txt file is there
if [ ! -f protocol.txt ] ; then
  echo "Error: File \"protocol.txt\", the protocol information file, is missing" 1>&2
fi 

# verify the titles files have a correctly matching program entry
vtitles

# create ALL.TITLES
alltitles


# Feed SAS code into standard input
sas -stdio -sasuser work 2>/dev/null << -----FINISH-----
%allocw
options validvarname=V7 nofmterr noovp;
%crprotds(protocol.txt,&_ptlibref_..protocol)
%crtitlesds(ALL.TITLES,&_ptlibref_..titles)
proc sort nodupkey data=&_ptlibref_..titles(where=(type='T' and number=1 and sascode ne " "))
           out=titles(keep=filename sascode);
  by filename;
run;

data _null_;
  set titles;
  file _fileout filevar=filename new notitles noprint;
  put @1 "/*";
  put @1 "/ Program      : " filename;
  put @1 '/ Author       : Automatically generated by the "crtitlesds" script';
  put @1 "/ Date         : $datetime";
  put @1 "/ Client       : $client";
  put @1 "/ Office       : $office";
  put @1 "/ Drug         : $drug";
  put @1 "/ Protocol     : $study";
  put @1 "/ Increment    : $inc";
  put @1 "/ Notes        : Do not edit this member as it will get overwritten next time";
  put @1 '/                "crtitlesds" is run. If you wish to change the code you must';
  put @1 "/                change it in the .titles member.";
  put @1 "/==============================================================================*/";
  put;
  put @1 sascode;
run;
-----FINISH-----


# List output file remappings using "lisfile:" entries
lisremap


# Check titles for errors
checktitles


# Check for missing sas programs
intnop > ~/intnop_$$.tmp
if [ -s ~/intnop_$$.tmp ] ; then
  echo "Warning: (crtitlesds) Titles entries exist for the following program(s) but no program yet exists" 1>&2
  cat ~/intnop_$$.tmp 1>&2
fi
rm -f ~/intnop_$$.tmp
