#!/bin/bash
#<pre><b>
# Script     : crindex_win
# Version    : 2.0
# Author     : Roland Rashleigh-Berry
# Date       : 07-Feb-2007
# Purpose    : To create an index of macros or scripts with standard headers
#              (uses SAS).
# SubScripts : justhdr
# Notes      : WINDOWS VERSION
#              Output goes to standard output. This script gets called from the
#              crindex script if it detects you are running a Windows version of
#              SAS. This is for Linux on Windows calling SAS installed on
#              Windows. You need to set up and export a system environment
#              variable in your .bashrc named HOMEW like this:
#                    HOMEW=C:\\cygwin\\home\\rrash    # for Cygwin
#                    export HOMEW
#              ...where "rrash" is your user id (it might be "Default")
# Usage      : crindex_win * > @index.txt
#              crindex_win *.sas > @index.txt
#===============================================================================
# PARAMETERS:
#-pos- -------------------------------description-------------------------------
#  1   File(s) or file pattern (combination)
#===============================================================================
# AMENDMENT HISTORY:
# init --date-- mod-id ----------------------description------------------------
# rrb  07Feb07         Two more columns given to member name
#===============================================================================
# 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.
#===============================================================================

# Put out a usage message if not enough parameters supplied
if [ $# -lt 1 ] ; then
  echo "Usage: crindex_win filename(s)" 1>&2
  exit 1
fi


# extract just the headers
justhdr $@ > $HOME/justhdr_$$.tmp


# Write SAS code to a temporary file
cat > $HOME/crindex_$$.sas << -----FINISH-----
options validvarname=v7 nofmterr;
filename _outfile "$HOMEW\crindex_$$.tmp";
data _crindex;
  length name $ 18 subgroup $ 16 text $ 80 line $ 200;
  retain fchar name subgroup " ";
  infile "$HOMEW\justhdr_$$.tmp" pad eof=eof;
getname:
  input @1 line \$char200.;
  if _n_=1 then fchar=substr(line,1,1);
  if substr(line,1,9) NE fchar||' Program'
  and substr(line,1,8) NE fchar||' Script' then goto getname;

  name=left(scan(line,2,':'));
  text=" ";
  subgroup=" ";
  text=" ";
  seq=0;
prepurp:
  input @1 line \$char200.;
  if substr(line,1,9) NE fchar||' Purpose' then goto prepurp;
  subgroup="Purpose";
  text=left(substr(line,index(line,':')+1));
  seq=seq+1;
  output;
  input @1 line \$char200.;
purpose:
  if substr(line,3,4) ne "    " then goto preusage;
  text=left(substr(line,3));
  seq=seq+1;
  output;
  input @1 line \$char200.;
  goto purpose;
preusage:
  if substr(line,1,7) EQ fchar||' Usage' then goto usestart;
  input @1 line \$char200.;
  goto preusage;
usestart:
  subgroup='Usage';
  text=left(substr(line,index(line,':')+1));
  seq=seq+1;
  output;
  input @1 line \$char200.;
usage:
  if substr(line,1,8)=fchar||"=======" then goto endxxx;
  text=left(substr(line,2));
  seq=seq+1;
  output;
  input @1 line \$char200.;
  goto usage;
endxxx:
  return;
  drop line;
eof:
  stop;
run;
proc sort data=_crindex;
  by name subgroup seq;
run;
data _null_;
  file _outfile notitles noprint;
  set _crindex;
  by name subgroup;
  if _n_ EQ 1 then do;
    put @1 'Index of members in this directory with standard headers';
    put @1 '========================================================';
    put @1 '(this list was generated by the crindex script)';
  end;
  if first.name then put / @1 name @20 '- ' text;
  else if first.subgroup then put / @22 subgroup +(-1) ": " text;
  else put @22 text;
run;
-----FINISH-----

# run the code
sas -nosplash -nologo -icon -log "$HOMEW" -sysin "$HOMEW\crindex_$$.sas"

# if output file exists then cat it and delete it
if [ -f $HOME/crindex_$$.tmp ] ; then
  cat $HOME/crindex_$$.tmp 
  rm -f $HOME/crindex_$$.tmp 
fi

# tidy up
rm -f $HOME/justhdr_$$.tmp $HOME/crindex_$$.sas $HOME/crindex_$$.log
