#!/bin/sh
# @(#) sqlr $Revision: 4.2 $
#
# sqlr [-u user] [-p password] [file]
#
# Integration of SQL/R editor and SQL/R execution module.
# This will setup all necessary information to call the
# SQL/R execution module from editor.
#
# If specifying a database (and password) you don't need
# to give the OPEN DATABASE statement.

# ----------------------------------------------------------------------

# The OS we're running on
OS=`uname -s`
if [ $OS = "HP-UX" ]
then
   OS="$OS`uname -r|cut -d"." -f2`"
fi

# ----------------------------------------------------------------------

# Possible customizon.

# QPATH contains a list of pathes like PATH where sqlrexec looks
# for files.
#QPATH=
#export QPATH

# Preset LANG (if unset)
#if [ -z "$LANG" ]
#then
#   LANG=en_US.iso88591
#   LANG=de_DE.iso88591
#   export LANG
#fi

# If SQL/R is not installed in /opt/sqlr/4.0, this must point to the 
# map directory.
# SQLR_MAP=/opt/sqlr/4.0/share/map

# ----------------------------------------------------------------------

# This is the directory where we find sqlrun and sqlred
bindir=`dirname $0`

# Default arguments
ARGS=""

# This contains the database and password
DB_USER=""
DB_PSWD=""

# Command usage
usage="Usage: $0 [-u user] [-p password] [file]"

#
# handle arguments
#

if [ $# != 0 ]
then
   if [ "$1" = "-help" ]
   then
      echo $usage
      exit 2
   fi

   set -- `getopt mu:p: $*`
   if [ $? != 0 ]
   then
      echo $usage
      exit 2
   fi
fi

args=""

for i in $*
do
   case "$i" in
   -m)   ARGS="-m"; shift;;
   -u)   DB_USER="$2"; shift 2;;
   -p)   DB_PSWD="$2"; shift 2;;
   --)   shift; break;;
   esac
done

#
# launch editor
#

if [ -n "$DB_USER" ]
then
   DB_USER="-u $DB_USER"
fi

if [ -n "$DB_PSWD" ]
then
   DB_PSWD="-p $DB_PSWD"
fi

# If a pipe symbol is in the first column, sqlrexec is processed
# internally through a pipe and output is mapped.

export SQLREXEC
SQLREXEC="|$bindir/sqlrexec -nt $DB_USER $DB_PSWD %s"

exec $bindir/sqlred $ARGS $*
