#!/bin/bash
#<pre><b>
# Script     : mybaddrug
# Version    : 1.0
# Author     : Roland Rashleigh-Berry
# Date       : 07-Jun-2005
# Purpose    : To list which of your own SAS programs do not have a correct 
#              drug name in the standard header.
# SubScripts : none
# Notes      : none
# Usage      : mybaddrug 
#              mybaddrug -f
#================================================================================
# OPTIONS:
#-opt- -------------------------------description--------------------------------
#  f   (switch) fixes the drug name in your standard headers
#===============================================================================
# PARAMETERS:
#-pos- -------------------------------description-------------------------------
# N/A  Do not supply any parameters
#===============================================================================
# AMENDMENT HISTORY:
# init --date-- mod-id ----------------------description------------------------
# 
#===============================================================================
# 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.
#===============================================================================

# Usage message then exit
usage () {
  echo "Usage: mybaddrug -f" 1>&2
  exit 1
}

# options defaults
switchf=0                           # Default switch=off for fixes your program
name


# "case" statement for action to take on selected options
while getopts "f" param ; do
  case $param in
   "?") # bad option supplied
        usage
        ;;
   "f") # (switch) fixes your program name
        switchf=1
        ;;
  esac
done


# shift to bring first parameter to position 1
shift $(($OPTIND - 1))


# No parameters required
if [ $# -gt 0 ] ; then
  echo "Do not supply any parameters" 1>&2
  usage
fi

drug=$(pwd | awk -F/ '{print $6}')


if [ $switchf -eq 0 ] ; then

  grep "^/ Drug  *: .* *" $(myfiles *.sas) | gawk -F: 'index($3,drug)==0'
drug="$drug"

else

  echo "The following files will be fixed:" 1>&2
  for file in $(grep "^/ Drug  *: .* *" $(myfiles *.sas) | gawk -F:
'index($3,drug)==0 {print $1}' drug="$drug")
  do
    echo "$file" 1>&2
    perl -i -pe "s%^/ Drug  *: .* *%/ Drug         : $drug%" ${file}
  done

fi
