#!/bin/sh
#
#  Copyright (c) 1999-2021 Easysoft Ltd. All rights reserved.
#
#  $Id: uodbc 61 2005-07-12 10:02:43Z martin $
#
#  This script is used to install a binary version of unixODBC, ODBC drivers
#  and ODBC datasources.
#
#  Usage:
#  uodbc install tar_file_stem destination product_long product_short needdm_flag do_linkpath_flag
#		- install unixODBC
#  uodbc driver template product_long product [quiet]
#		- install ODBC driver in to unixODBC
#  uodbc dsn template product_long product [overwrite]
#		- install DSN in to unixODBC
#
#  Exit status:
#  0 - OK
#  1 - OK, unixODBC installed
#  2 - command line error (e.g. invalid operation)
#  3 - unexpected internal script error
#  4 - file not found
#  5 - directory not found
#  6 - path expected to be a directory is not
#  7 - permission denied
#  8 - file not executable
#  9 - environment variable not defined
#
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
INSTALLED_UNIXODBC=""        	# installed unixODBC DM?
PATHSTOSEARCH="/usr/local/easysoft/unixODBC /usr/local /usr/local/unixODBC /opt/unixODBC /usr"
if [ "$ECHO" = "" ];then
  echo "error - ECHO not defined, expect it to be exported"
  exit 2
fi
#
# What is running?
#
###bz 1890, $ECHO "uodbc install script running..."
#
# Check command line
#
cmdline_error=""
if [ $# -lt 1 ];then
  cmdlinestr="No action specified"
  cmdline_error="1"
else
  if [ \( $1 != "install" \) -a \( $1 != "driver" \) -a \( $1 != "dsn" \) ];
  then
    cmdlinestr="Invalid operation: $1"
    cmdline_error="1"
  else
    OPERATION="$1"
  fi
fi
if [ "$OPERATION" = "dsn" ];then
  if [ \( $# -ne 4 \) -a \( $# -ne 5 \) ];then
    cmdlinestr="Invalid number of parameters for dsn operation"
    cmdline_error="1"
  else
    TEMPLATE_FILE="$2"
    PRODUCTLONG="$3"
    PRODUCT="$4"
    if [ \( $# -gt 4 \) ];then
       OVERWRITE="$5"
    else
       OVERWRITE="1"        # By default overwrite existing dsn
    fi
    UNIXODBCPATH=`cat /tmp/uodbc.tmp`
    LD_LIBRARY_PATH="$LD_LIBRARY_PATH:$UNIXODBCPATH/lib"
    export LD_LIBRARY_PATH
    LIBPATH="$LIBPATH:$UNIXODBCPATH/lib"
    export LIBPATH
    SHLIB_PATH="$SHLIB_PATH:$UNIXODBCPATH/lib"
    export SHLIB_PATH
  fi
fi
#
# If a command line error output usage message
#
if [ "$cmdline_error" = "1" ];then
  $ECHO "Invalid command line, $cmdlinestr"
  $ECHO "Usage:"
  $ECHO "uodbc dsn template product_long product [overwrite]"
  exit 2
fi

#
# Install ODBC DSN
# ================
#
if [ "$OPERATION" = "dsn" ];then
  if [ ! "$TESTEXISTS" "$TEMPLATE_FILE" ];then
    $ECHO "Template file $TEMPLATE_FILE not found"
    exit 4
  fi
  if [ "$UNIXODBCPATH" = "" ];then
    $ECHO "Internal error."
    $ECHO "Do not know where to find odbcinst command to install the driver"
    exit 3
  fi
  if [ ! -x "$UNIXODBCPATH/bin/odbcinst" ];then
    $ECHO "odbcinst command not executable"
    exit 8
  fi
  #
  # Find our where the system odbcinst.ini file is located
  #
  ODBCINI=`$UNIXODBCPATH/bin/odbcinst -j | grep SYSTEM | awk -F: '{print $2}' | awk '{print $1}'`
  if [ "$TESTEXISTS" "$ODBCINI" ];then
    if [ -w "$ODBCINI" ];then
      INIWRITABLE="y"
    else
      INIWRITABLE="n"
    fi
  else
    DCHK=`dirname "$ODBCINI"`
    if [ -w "$DCHK" ];then
      INIWRITABLE="y"
    else
      INIWRITABLE="n"
    fi
  fi
  if [ "$INIWRITABLE" != "y" ];then
    cat <<EOF

**WARNING**
The $ODBCINI file is not writable by you.
You will need to add the DSN manually after this install using the template
$TEMPLATE_FILE which looks like:

EOF
    cat $TEMPLATE_FILE
    $ECHO
    $ECHO "Press the return key to continue"
    read press
    exit 0
  fi
  #
  # Find the name of the DSN
  #
  DSN=`grep '\[*\]' "$TEMPLATE_FILE"`
  if [ "$DSN" = "" ];then
    DSN="demo"
  fi
  #
  # If datasource does exist and we are not overwriting
  #
  if [ "$OVERWRITE" = "0" ];then
      # search system datasources
      ODBCSEARCH="ODBC_SYSTEM_DSN"        # create system dsn
      export ODBCSEARCH
      # strip leading and trailing square brackets
      DSN=`echo $DSN | sed -e 's/\[\(.*\)]/\1/g'`
      # Need to strip [] from DSN name
      SEARCHDSN=`$UNIXODBCPATH/bin/odbcinst -s -q | grep "\[$DSN]"`
      if [ "$SEARCHDSN" = "[$DSN]" ];then
        # datasource already exists
	answer="hoho"
	while [ \( "$answer" != "n" \) -a \( "$answer" != "y" \) ]
	do
	  $ECHO "Datasource [$DSN] already installed as:"
	  $ECHO "=========="
	  "$UNIXODBCPATH/bin/odbcinst" -q -s -n "$DSN"
	  $ECHO "=========="
	  $ECHO "Do you want to replace it? (y/n) [y]: \c"
	  read answer
	  if [ "$answer" = "" ];then
	      answer="y"
	  fi
	  if [ "$answer" = "n" ];then
	      exit 0;
	  fi
	done
      fi
  fi

  cat <<EOF
You can now install a $DSN data source for unixODBC and
$PRODUCTLONG.
However, unixODBC currently requires an odbc.ini file to exist before a
datasource can be added. This install will create $ODBCINI if you
request the datasource to be created.

EOF
  # if the product said don't overwrite but the installing person overruled
  if [ "$OVERWRITE" != "0" ];then
    answer="hoho"
    while [ "$answer" != "y" -a "$answer" != "n" ]
    do
      $ECHO "Install unixODBC/$PRODUCTLONG data source (y/n) [y]: \c"
      read answer
      if [ "$answer" = "" ];then
	answer="y"
      fi
    done
  else
    answer="y"
  fi
  if [ "$answer" = "y" ];then
    if [ ! "$TESTEXISTS" "$ODBCINI" ];then
      $ECHO "" > "$ODBCINI"
    fi
    ODBCSEARCH="ODBC_SYSTEM_DSN"        # create system dsn
    export ODBCSEARCH
    $ECHO "Running odbcinst command:"
    $ECHO "$UNIXODBCPATH/bin/odbcinst -s -i -f $TEMPLATE_FILE"
    $ECHO "=========="
    $UNIXODBCPATH/bin/odbcinst -s -i -f $TEMPLATE_FILE
    $ECHO "=========="
    if [ $? -ne 0 ]; then
      $ECHO "** Failed to install unixODBC/OOB demo data source **"
    fi
  fi
fi

