#!/bin/bash
#<pre><b>
# Script     : getlayout
# Version    : 2.1
# Author     : Roland Rashleigh-Berry
# Date       : 02-Mar-2007
# Purpose    : Production script to get layout details for a .lis output
#              (Uses SAS).
# SubScripts : getlayout_win
# SubMacros  : %allocr
# Notes      : Displays 5 pieces of information in the following order separated
#              by spaces:
#              paper, lmargin, rmargin, tmargin, bmargin, layout
# Usage      : getlayout filename.lis
#
#===============================================================================
# PARAMETERS:
#-pos- -------------------------------description-------------------------------
#  1   Lis file to get layout for
#===============================================================================
# AMENDMENT HISTORY:
# init --date-- mod-id ----------------------description------------------------
# rrb  24Nov05         -stdio used on sas invocation
# rrb  11Feb07         Call getlayout_win for Windows configurations
# rrb  20Feb07         Call %allocr macro
# rrb  28Feb07         Remove some redundant code
# rrb  02Mar07         Use "&_ptlibref_.." instead of "der."
#===============================================================================
# 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 wrong number of parameters
if [ $# -ne 1 ] ; then
  echo "Usage: getlayout filename.lis     (only one file name)" 1>&2
  exit 1
fi


# if a Windows config then call getlayout_win and exit on return
if [ -n "$CYGWIN" ] ; then
  getlayout_win "$@"
  exit $?
fi


# path
dir=$(dirname $1)
if [ "$dir" == "." ] ; then
  dir=$(pwd)
fi


# file name
file=$(basename $1)


# get report label which will be anything after .lis (usually nothing)
label=${file##*.lis}


# get program name by dropping the .lis* extension
prog=${file%.lis*}


# Feed SAS code into standard input
sas -stdio -sasuser work 2>/dev/null << -----FINISH-----
%allocr
options validvarname=v7 nofmterr noovp nodate nonumber;
%protinfo
%macro prog;
  %if %sysfunc(exist(&_ptlibref_..titles)) %then %do;
    %proginfo(program=$prog,label=$label)
  %end;
  %else %do;
    %global _layout_;
    %let _layout_=&_dflayout_;
  %end;
%mend prog;
%prog
data _null_;
  file stdout;
  put "&_paper_ &_lmargin_ &_rmargin_ &_tmargin_ &_bmargin_ &_layout_";
run;
-----FINISH-----
