#!/bin/bash
#<pre><b>
# Script     : hdr
# Version    : 2.1
# Author     : Roland Rashleigh-Berry
# Date       : 04-Mar-2007
# Purpose    : To create a standard SAS program header
# SubScripts : getname getfields
# Notes      : This shell script generates a SAS program header for a sas
#              program in the current directory and places it at the top of the
#              code if the program exists. If invoked without a following
#              program name then the user will be prompted to enter the name.
#              The drug, protocol and increment will be added to the header
#              using details taken from the current directory.
# Usage      : hdr
#              hdr progname
#===============================================================================
# PARAMETERS:
#-pos- -------------------------------description-------------------------------
#  1   program name (optional)
#===============================================================================
# AMENDMENT HISTORY:
# init --date-- mod-id ----------------------description------------------------
# rrb  18Feb07         fields used now 5, 6, 7, 8, 9 and sas version now 9.1.3
# rrb  04Mar07         "getfields" script used
#===============================================================================
# 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.
#===============================================================================

# prompt for program name if not supplied
if [ $# -lt 1 ]
then
  echo -n "Enter program name: "
  read progname
else
  progname=$1
fi

# make sure we have a ".sas" at the end of the program name
progname=`echo $progname | awk -F. '{print $1 ".sas"}'`

# prompt for program purpose
echo -n "Enter program purpose: "
read purpose

# find out the name of the person invoking this script
author=`getname`

# format today's date in DD-Mon-YYYY form
date=`date '+%d-%b-%Y'`

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


# cat the following to a file until "FINISH" is encountered
cat > $HOME/hdr.tmp << FINISH
/*
/ Program      : $progname
/ Author       : $author ($(whoami))
/ Date         : $date
/ SAS version  : 9.1.3
/ Client       : $client
/ Office       : $office
/ Drug         : $drug
/ Protocol     : $study
/ Increment    : $inc
/ Purpose      : $purpose
/ Notes        : 
/===============================================================================
/ Modification History:
/ -user-id --date-- --mod-id--- ---------------------purpose--------------------
/ 
/=============================================================================*/

FINISH

# if the sas program already exists then add the header to the top
# otherwise just create the sas program out of the generated header
if [ -f $progname ]
then
  cat $HOME/hdr.tmp $progname > $HOME/hdr2.tmp
  rm -f $progname
  cat $HOME/hdr2.tmp > $progname
  rm -f $HOME/hdr2.tmp
else
  cat $HOME/hdr.tmp > $progname
  echo "created $progname"
fi

# delete the temporary file
rm -f $HOME/hdr.tmp
