#!/bin/sh
#
# $Id: oob_create_dsn.sh 666 2005-05-19 09:15:30Z martin $
#
#  Copyright (c) 1999-2005 Easysoft Ltd. All rights reserved.
#
if [ "$ECHO" = "" ];then
  echo "error - ECHO not defined, expect it to be exported"
  exit 2
fi
if [ "$TESTEXISTS" = "" ];then
  echo "error - TESTEXISTS not defined, expect it to be exported"
  exit 2
fi
#
# Check for stty
#
stty 1>/dev/null 2>/dev/null
if [ $? -eq 0 ];then
  ECHOOFF="stty -echo"
  ECHOON="stty echo"
else
  ECHOOFF=""
  ECHOON=""
fi
#
if [ $# -ne 2 ];then
  cmdlinestr="incorrect number of parameters"
  cmdline_error=1
else
  INSTALLPATH=$1
  UNIXODBCFILE=$2
fi
#
# If command line error
#
if [ "$cmdline_error" = "1" ];then
  $ECHO "\"$*\""
  $ECHO "Invalid command line, $cmdlinestr"
  $ECHO "Usage:"
  $ECHO "oob_create_dsn install_path uodbc_file"
  exit 1
fi
#
OOBPING="$INSTALLPATH/bin/oobpings"
if [ ! -x "$OOBPING" ];then
  $ECHO "Cannot find executable $INSTALLPATH/bin/oobpings"
  $ECHO "Aborting OOB DSN Creation"
  exit 1
fi
#
if [ ! "$TESTEXISTS" "$UNIXODBCFILE" ];then
  $ECHO "Cannot find $UNIXODBCFILE"
  $ECHO "Aborting OOB DSN Creation"
  exit 1
fi
UNIXODBCPATH=`cat $UNIXODBCFILE`
ODBCINST="$UNIXODBCPATH/bin/odbcinst"
if [ ! -x "$ODBCINST" ];then
  $ECHO "Cannot find an executable $ODBCINST"
  $ECHO "Aborting OOB DSN Creation"
  exit 1
fi
ISQL="$UNIXODBCPATH/bin/isql.sh"
if [ ! -x "$ISQL" ];then
  ISQL="$UNIXODBCPATH/bin/isql"
  if [ ! -x "$ISQL" ];then
    $ECHO "Cannot find an executable isql program"
    $ECHO "WARNING: This script will be unable to test the created DSN"
    ISQL=""
  fi
  LDPATHS="$UNIXODBCPATH/lib:/usr/local/easysoft/lib"
  LD_LIBRARY_PATH="$LD_LIBRARY_PATH:$LDPATHS"
  export LD_LIBRARY_PATH
  SHLIB_PATH="$SHLIB_PATH:$LDPATHS"
  export LIBPATH
  LIBPATH="$LIBPATH:$LDPATHS"
  export LIBPATH
  DYLD_LIBRARY_PAYH="$DYLD_LIBRARY_PATH:$LDPATHS"
  export DYLD_LIBRARY_PATH
fi
cat <<EOF
Test Connection to OOB Server
=============================

If you have installed the OOB Server on another machine you can now
interactively create a working DSN on this machine which will connect
to it. However, you should make sure before you continue with this DSN
creation:

o you have licensed your OOB Server.
o you have available a username and password to logon to the remote machine.
o you have (or can create during this process) a SYSTEM DSN on the remote
  machine.
o you have available any passwords you need to login to your database

You can elect to skip this process and create the DSN yourself afterwards -
this is not an essential part of the installation, it is just a script
to help you set up your first DSN and test it.

EOF
answer="hoho"
while [ \( "$answer" != "y" \) -a \( "$answer" != "n" \) ]
do
  $ECHO "Do you have a remote OOB Server installed we can access?\n(q=quit this step) [y] (y/n): \c"
  read answer
  if [ "$answer" = "" ];then
    answer="y"
  fi
  if [ \( "$answer" = "n" \) -o \( "$answer" = "q" \) ];then
    sysconfdir=`$ODBCINST -j | grep SYSTEM | awk -F: '{print $2}'`

    cat <<EOF

A demo data source has been created in $sysconfdir
which you may use as an example for creating your own post installation.

EOF
    exit 0
  fi
done
PORT="8888"
answer=""
while [ "$answer" = "" ]
do
  cat <<EOF

Enter the name (or IP address) of the machine where the OOB Server is
installed. If you have installed the OOB Server using a non-default port
(the default is 8888) then enter server:port e.g. myserver:9999.

EOF
  $ECHO "Server (q=quit) : \c"
  read answer
  if [ "$answer" = "q" ];then
    exit 0
  fi
  if [ "$answer" != "" ];then
    server=`$ECHO $answer | awk -F: '{print $1}'`
    port=`$ECHO $answer | awk -F: '{print $2}'`
    if [ "$port" = "" ];then
      port=$PORT
    fi
    #$ECHO "$server, \"$port\""
    #$ECHO "Attempting to ping the OOB Server..."
    $ECHO "Using $OOBPING -h $server -t $port"
    $ECHO "==============================================================="
    $OOBPING -h $server -t $port
    result=$?
    $ECHO "==============================================================="
    if [ $result -ne 0 ];then
      cat <<EOF

Failed to ping the OOB Server. Please examine the output above.
The Server should be the name (or IP address) of the machine where the OOB
Server is installed. If you enter a name then this name must be able to be
resolved into an IP address using either DNS or a local hosts file.
If you got an error mentioning gethostbyname then the system failed to
resolve $server into an IP Address.

If you got a connection refused error then perhaps you specified the
wrong machine or the wrong port.

If you cannot resolve this issue you can enter q to quit this part of
the process.
EOF
    answer=""
    fi
  fi
done
cat <<EOF

ServerPort = $server:$port

The second step is to authenticate with the OOB Server. You need a valid
username for the operating system running on the server $server.
This user needs to have permission to logon locally to $server i.e. this
user should be able to logon to $server at the console of that machine
(although you are not actually required to do this here).
We also need the password for this user.

EOF
logonuser=""
logonauth=""
loggedon="n"
while [ "$loggedon" = "n" ]
do
  $ECHO "Please enter a valid username and password for machine $server"
  if [ "$logonuser" != "" ];then
    $ECHO "Username (enter q to abort) [$logonuser] : \c"
  else
    $ECHO "Username (enter q to abort) : \c"
  fi
  read answer
  if [ "$answer" = "q" ];then
    $ECHO "DSN creation aborted at user request"
    exit 0
  elif [ "$answer" != "" ];then
    logonuser="$answer"
  fi
  $ECHO "Password : \c"
  $ECHOOFF
  read logonauth
  $ECHOON
  $ECHO
  $ECHO "Running $OOBPING -h $server -t $port -u \"$logonuser\" -p \"XXXX\""
  $ECHO "==============================================================="
  $OOBPING -h $server -t $port -u "$logonuser" -p "$logonauth"
  result=$?
  $ECHO "==============================================================="
  if [ $result -eq 0 ];then
    $ECHO "Test Successful"
    loggedon="y"
  else
    cat <<EOF

Failed to authenticate user $logonuser on $server.
Please examine the output above to identify the problem. If you get an
error such as "Authentication failure (error number 1326)" you should
look the error number up in the OOB FAQ at
/usr/local/easysoft/oob/doc/FAQ.txt or online at http://www.easysoft.com
in the FAQ search for the product ODBC-ODBC Bridge. Common issues with
authenticating in MS Windows are also listed in the file auth.txt

If you get error -1 then the OOB Server is on UNIX and either the
username or the password are incorrect.

EOF

    answer="hoho"
    while [ \( "$answer" != "y" \) -a \( "$answer" != "n" \) ]
    do
      $ECHO "Would you like to view auth.txt now [y] (y/n): \c"
      read answer
      if [ "$answer" = "" ];then
        answer="y"
      fi
      if [ "$answer" = "y" ]; then
        cat auth.txt
      fi
    done
  fi
done
cat <<EOF

Successfully connected and authenticated with:

ServerPort = $server:$port
LogonUser = $logonuser
LogonAuth = XXXXXX

The third step is to define the target datasource on $server to connect
to and any database authentication (if required). In OOB connection attribute
terms these are called TargetDSN, TargetUser and TargetAuth. The TargetDSN
is the name of a SYSTEM DSN (it must be a SYSTEM DSN, not a USER DSN) on
$server. This data source on $server may also need database
authentication i.e. a database username and password (your database 
administrator should have supplied these to you).
EOF
targetdsn=""
targetuser=""
targetauth=""
connected="n"
while [ "$connected" = "n" ]
do
  cat <<EOF

Please enter the name of the SYSTEM DSN on $server and (if required) a
database username and password (just hit enter on the database username
and password if you do not need them e.g. if the target DSN is a MS
SQL Server DSN using trusted connections).

EOF
  if [ "$targetdsn" != "" ];then
    $ECHO "Target DSN (q=quit) [$targetdsn] : \c"
  else
    $ECHO "Target DSN (q=quit) : \c"
  fi
  read answer
  if [ "$answer" = "q" ];then
    $ECHO "DSN creation aborted at user request"
    exit 0
  elif [ "$answer" != "" ];then
    targetdsn="$answer"
  fi
  if [ "$targetuser" != "" ];then
    $ECHO "Database Username [$targetuser] : \c"
  else
    $ECHO "Database Username : \c"
  fi
  read answer
  if [ "$answer" != "" ];then
    targetuser="$answer"
  fi
  $ECHO "Database Password : \c"
  $ECHOOFF
  read targetauth
  $ECHOON
  $ECHO
  string1="ServerPort=$server:$port;LogonUser=$logonuser"
  string3="LogonAuth=$logonauth"
  if [ "$targetuser" != "" ];then
    string2="TargetDSN=$targetdsn;UID=$targetuser"
    string4="PWD=$targetauth"
    string5="PWD=XXXXXX"
  else
    string2="TargetDSN=$targetdsn"
    string4=""
    string5=""
  fi
  constr="$string1;$string3;$string2;$string4"
  $ECHO "Running $OOBPING -d \"$string1;LogonAuth=XXXXXX;$string2;$string5\""
  $ECHO "==============================================================="
  $OOBPING -d "$constr"
  result=$?
  $ECHO "==============================================================="
  if [ $result -eq 0 ];then
    $ECHO "Full connection successful"
    connected="y"
  else
    cat <<EOF

WARNING:

$OOBPING -d \"$string1;LogonAuth=XXXXXX;$string2;$string5\" failed

Examine the output above to determine the problem.
If you cannot determine the problem now you can abort the creation of
the DSN here and perform this step post installation.

EOF
  fi
done
cat <<EOF

Server = $server:$port

LogonUser = $logonuser
LogonAuth = XXXXXX

TargetDSN = $targetdsn
TargetUser = $targetuser
TargetAuth = XXXXXX

We have a full connection to the remote machine $server and the remote
data source "$targetdsn". This script can now create a local OOB DSN which
you can use later to connect from your ODBC applications.

EOF
answer="hoho"
while [ \( "$answer" != "y" \) -a \( "$answer" != "n" \) ]
do
  $ECHO "Do you want to create a DSN you can use later? [y] (y/n) : \c"
  read answer
  if [ "$answer" = "" ];then
    answer="y"
  fi
  if [ "$answer" = "n" ];then
    cat <<EOF

You may want to make a note of the attributes above for when you create
a DSN yourself.

Exiting DSN creation.

EOF
    exit 0
  fi
done
dsnname=""
while [ "$dsnname" = "" ]
do
  $ECHO "Enter a name for this DSN : \c"
  read dsnname
done
file="dsn_$$"
$ECHO "[$dsnname]" > $file
$ECHO "Driver = OOB" >> $file
$ECHO "ServerPort = $server:$port" >> $file
$ECHO "LogonUser = $logonuser" >> $file
$ECHO "LogonAuth = $logonauth" >> $file
$ECHO "TargetDSN = $targetdsn" >> $file
if [ "targetuser" != "" ];then
  $ECHO "TargetUser = $targetuser" >> $file
  $ECHO "TargetAuth = $targetauth" >> $file
fi
ODBCSEARCH="ODBC_SYSTEM_DSN"        # create system dsn
export ODBCSEARCH
$ODBCINST -s -i -f $file
result=$?
if [ "$result" -ne 0 ];then
  $ECHO "Failed to create DSN"
  $ECHO "The DSN entry we would have created can be found in $file"
  exit 0
fi
if [ "$ISQL" = "" ];then

We did not find an executable isql so this script is not going to test
the DSN.

else
  cat <<EOF

We can now attempt to demonstrate the OOB Client by retrieving a list
of tables from $server and your remote DSN $taragetdsn.

EOF
  answer="hoho"
  while [ \( "$answer" != "y" \) -a \( "$answer" != "n" \) ]
  do
    $ECHO "Do you want to attempt to get a table list back [y] (y/n) : \c"
    read answer
    if [ "$answer" = "" ];then
      answer="y"
    fi
    if [ "$answer" = "n" ]; then
      cat <<EOF

You can use
$ISQL -v $targetdsn
or
$ISQL -v $targetdsn $targetuser $targetauth

to issue SQL to your remote DSN.

DSN creation complete.

EOF
      exit 0
    fi
  done
  helpfile="tables_$$.sql"
  $ECHO "help\n\n" > $helpfile
  $ISQL -v $dsnname $targetuser $targetauth < $helpfile
  $ECHO				# get past isql prompt
fi