#!/bin/sh
#
#  Copyright (c) 1999-2005 Easysoft Ltd. All rights reserved.
#
#  $Id: uodbc 53 2005-05-20 15:50:22Z 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?
#
$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" = "install" ]; then
  if [ $# -ne 7 ];then
    cmdlinestr="Invalid number of parameters for install operation"
    cmdline_error="1"
  else
    UNIXODBC_TARFILE_STEM="$2"
    UNIXODBC_DESTINATION="$3"
    PRODUCTLONG="$4"
    PRODUCT="$5"
    NEEDDM="$6"
    DOLINK="$7"
  fi
elif [ "$OPERATION" = "driver" ];then
  if [ $# -lt 4 ];then
    cmdlinestr="Invalid number of parameters for driver operation"
    cmdline_error="1"
  else
    TEMPLATE_FILE="$2"
    PRODUCTLONG="$3"
    PRODUCT="$4"
    if [ $# -eq 5 ];then
      QUIET="$5"
    else
      QUIET="n"			# default not quiet
    fi
    UNIXODBCPATH=`cat uodbc_tmp`
    LD_LIBRARY_PATH="$LD_LIBRARY_PATH:$UNIXODBCPATH/lib"
    export LD_LIBRARY_PATH
    DYLD_LIBRARY_PATH="$DYLD_LIBRARY_PATH:$UNIXODBCPATH/lib"
    export DYLD_LIBRARY_PATH
    LIBPATH="$LIBPATH:$UNIXODBCPATH/lib"
    export LIBPATH
    SHLIB_PATH="$SHLIB_PATH:$UNIXODBCPATH/lib"
    export SHLIB_PATH
  fi
elif [ "$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 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 install tar_file_stem destination_dir product_long product needdm do_linkpath"
  $ECHO "uodbc driver template product_long product"
  $ECHO "uodbc dsn template product_long product [overwrite]"
  exit 2
fi
#
# Install unixODBC
# ================
#
# Check for template file and destination directory
#
if [ "$OPERATION" = "install" ];then
  $ECHO "Install procedure for unixODBC Driver Manager selected"
  if [ ! "$TESTEXISTS" "$UNIXODBC_DESTINATION" ];then
    $ECHO "$UNIXODBC_DESTINATION unixODBC destination directory not found"
    exit 5
  fi
  if [ ! -d "$UNIXODBC_DESTINATION" ]; then
    $ECHO "$UNIXODBC_DESTINATION is not a directory"
    exit 6
  fi
  if [ ! -w "$UNIXODBC_DESTINATION" ];then
    $ECHO "Cannot write files to $UNIXODBC_DESTINATION - permission denied"
    exit 7
  fi
  #
  # Look for the tar file and output the install text
  #
  upat="$UNIXODBC_TARFILE_STEM*.tar"
  ue=`$ECHO $upat`
  if [ "$ue" != "$upat" ];then
    cat "${PRODUCT}_uodbcinstall.txt"
    if [ -r unixODBC_version.txt ];then
      $ECHO
      $ECHO "This distribution contains unixODBC `cat unixODBC_version.txt`"
      $ECHO
    fi
    $ECHO "Press the return key to continue"
    read press
  else
    exit 0	# no unixODBC with this product - nothing to do
  fi
  #
  # Is unixODBC already installed
  #
  $ECHO "Searching for unixODBC DM..."
  UNIXODBCPATH=""
  UNIXODBCEXISTS="n"
  multdm=""
  for dir in $PATHSTOSEARCH
  do
    echo "Examining $dir"
    found1=`$ECHO $dir/bin/odbcinst*`
    if [ "$found1" = "$dir/bin/odbcinst*" ];then
      found1=""
    fi
    found2=`$ECHO $dir/lib/libodbcinst*`
    if [ "$found2" = "$dir/lib/libodbcinst*" ];then
      found2=""
    fi
    found3=`$ECHO $dir/lib/libodbc*`
    if [ "$found3" = "$dir/lib/libodbc*" ];then
      found3=""
    fi
    if [ \( "$found1" != "" \) -a \( "$found2" != "" \) -a \( "$found3" != "" \) ];then
      $ECHO "Found unixODBC under $dir"
      if [ "$UNIXODBCPATH" = "" ];then
        UNIXODBCPATH="$dir"
        UNIXODBCEXISTS="y"
        OV=`$found1 -j | grep -i unixodbc | awk '{print $2}'`
        if [ $? -eq 0 ];then
          $ECHO "   and it is version $OV"
        else
	  OV=""
        fi
      else
        multdm="$multdm $dir"
      fi
    fi
  done
  #
  #  Warn if unixODBC installed more than once already.
  #
  if [ "$multdm" != "" ];then
    $ECHO
    $ECHO "WARNING: Found unixODBC DM installed in multiple places."
    $ECHO "This installation appeared to find the unixODBC DM in:"
    $ECHO "$UNIXODBCPATH $multdm"
    cat <<EOF

Already having the unixODBC DM installed multiple times does not actually
do any harm but if applications are linked against different versions of
the unixODBC DM and they were built with different sysconfdir settings
each DM may see different data sources. Please consult the installation
guide for assistance.
EOF
  fi
  #
  # Do we install unixODBC
  #
  cat <<EOF

This installation includes a copy of the unixODBC Driver Manager which
may be installed into $UNIXODBC_DESTINATION/unixODBC.
EOF
  if [ "$UNIXODBCEXISTS" = "y" ];then
cat <<EOF
As you already have the unixODBC Driver Manager installed you do not have to 
install the included driver manager, instead you can use your installed
version.

EOF
  fi
  if [ "$UNIXODBCEXISTS" = "y" ];then
    $ECHO
    $ECHO "The unixODBC Driver Manager appears to be installed in"
    $ECHO "$UNIXODBCPATH."
  else
    cat <<EOF

The unixODBC Driver Manager could not be found on this machine.
This does not mean you have not already installed unixODBC it may just
mean this script could not find it. If you want to use an already installed
unixODBC DM you need to tell this script where it is.
EOF
    #
    # Could not find unixODBC - ask if it is installed and where
    #
    answer="hoho"
    while [ \( "$answer" != "y" \) -a \( "$answer" != "n" \) -a \( "$answer" != "i" \) -a \( "$answer" != "" \) ]
    do
      $ECHO
      $ECHO "Do you already have the unixODBC DM installed?"
      $ECHO "(y/n i=do not know) [n]) : \c"
      read answer
      if [ "$answer" = "" ];then
        answer="n"
      fi
    done
    if [ "$answer" = "y" ];then
      $ECHO "Where did you install unixODBC (i.e. what did you set --prefix to)"
      found="0"
      while [ "$found" = "0" ]
      do
  	answer=""
  	while [ \( "$answer" = "" \) -o \( "$answer" != "q" \) ]
  	do
          $ECHO
  	  $ECHO "unixODBC install path? (q=assume not installed): \c"
  	  read answer
  	  if [ "$answer" = "q" ];then
  	    found="q"
  	  elif [ ! -x "$answer/bin/odbcinst" ];then
  	    $ECHO "$answer/bin/odbcinst not found"
  	    $ECHO "Try again"
  	    answer=""
  	  else
	    UNIXODBCEXISTS="y"
	    UNIXODBCPATH="$answer"
	    answer="q"
  	    found="1"
  	  fi
  	done
      done			# loop until DM path found or aborted
    fi				# installer says unixODBC is installed
  fi				# end of unixODBC exists or not
  if [ "$UNIXODBCEXISTS" != "y" ];then
    #
    # unixODBC DM not found or specified but product needs it
    #
    if [ "$NEEDDM" = "1" ];then
      cat <<EOF

The $PRODUCTLONG requires the unixODBC Driver Manager to be installed.
You must either install the included unixODBC driver manager or tell this
script where to find your already installed version.
EOF
    else             # unixODBC DM not found or specified but not required
      cat <<EOF

This product does not require the unixODBC Driver Manager to be installed
but Easysoft strongly recommend you do as a driver manager offers a great
deal of extra flexibility when using ODBC drivers.

EOF
    fi
  fi
  #
  # If unixODBC already installed let the installer choose whether to use
  # it, install the included version or not use it at all.
  #
  INSTALLUNIXODBC=""
  if [ "$NEEDDM" = "1" ];then	        # Must have DM
    if [ "$UNIXODBCEXISTS" = "y" ]; then # ...and already installed
      answer="hoho"
      while [ \( "$answer" != "y" \) -a \( $answer != "n" \) ]
      do
        $ECHO
        $ECHO "Do you want to use your installed unixODBC DM in preference to the"
        $ECHO "version included with this distribution? (y/n [n]) : \c"
        read answer
        if [ "$answer" = "" ];then
          answer="n"
        fi
        if [ "$answer" = "y" ];then
          INSTALLUNIXODBC="n"
	else
          INSTALLUNIXODBC="y"
          UNIXODBCPATH="$UNIXODBC_DESTINATION/unixODBC"
        fi
      done
    else			# DM not already installed and must have it
      INSTALLUNIXODBC="y"	# no choice - must be installed
      UNIXODBCPATH="$UNIXODBC_DESTINATION/unixODBC"
      $ECHO
      $ECHO "Installing the unixODBC DM"
    fi
  else				# do not have to install DM
    answer="hoho"
    while [ \( "$answer" != "y" \) -a \( $answer != "n" \) ]
    do
      $ECHO "Do you want to install the unixODBC DM in this package \c"
      if [ "$UNIXODBCEXISTS" = "y" ];then
        $ECHO "(y/n [n]) : \c"
      else
        $ECHO "(y/n [y]) : \c"
      fi
      read answer
      if [ "$answer" = "" ];then
        #
        # Default to do not install our unixODBC if it already exists.
        #
        if [ "$UNIXODBCEXISTS" = "y" ];then
          answer="n"
        else
          answer="y"
        fi
      fi
      INSTALLUNIXODBC="$answer"
      if [ "$answer" = "y" ];then
        UNIXODBCPATH="$UNIXODBC_DESTINATION/unixODBC"
      fi
    done
  fi
  #
  # Make UNIXODBC install path available to caller.
  #
  if [ "$UNIXODBCPATH" != "" ]; then
    $ECHO "$UNIXODBCPATH" > uodbc_tmp
  fi
  #
  # Install unixODBC DM
  # ===================
  #
  if [ "$INSTALLUNIXODBC" = "y" ];then
    cwd=`pwd`
    $ECHO "Unpacking unixODBC Driver Manager"
    cd $UNIXODBC_DESTINATION
    $ECHO
    ####$ECHO "Press the return key to continue"
    ####read press
    if [ "x$TARARGS" != "x" ];then
	tar "$TARARGS" $cwd/unixodbc*.tar
    else
	tar -xvf $cwd/unixodbc*.tar
    fi
    INSTALLED_UNIXODBC="y"
    if [ "$TESTEXISTS" "$cwd/unixODBC_version.txt" ];then
      $ECHO "product: unixODBC" >> unixODBC_install.info
      $ECHO "version: \c" >> unixODBC_install.info
      cat "$cwd/unixODBC_version.txt" >> unixODBC_install.info
      $ECHO "date: \c" >> unixODBC_install.info
      date 2>/dev/null >> unixODBC_install.info
    fi
    cd "$cwd"
    #
    # ld.so.conf
    #
    if [ "$DOLINK" = "1" ];then
      LDLIBDIRS="$UNIXODBCPATH/lib"
      ./install_linkpaths $PRODUCT "$LDLIBDIRS"
    fi				# doing links
  fi
  if [ "$INSTALLED_UNIXODBC" = "y" ];then
    exit 1
  else
    exit 0
  fi
#
# Install ODBC driver
# ===================
#
elif [ "$OPERATION" = "driver" ];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
  #
  ODBCINST=`$UNIXODBCPATH/bin/odbcinst -j | grep DRIVERS | awk  '{print $2}'`
  if [ "$TESTEXISTS" "$ODBCINST" ]; then
    if [ -w "$ODBCINST" ];then
      INSTWRITABLE="y"
    else
      INSTWRITABLE="n"
    fi
  else
    DCHK=`dirname "$ODBCINST"`
    if [ -w "$DCHK" ];then
      INSTWRITABLE="y"
    else
      INSTWRITABLE="n"
    fi
  fi
  if [ "$INSTWRITABLE" != "y" ];then
    if [ "$QUIET" != "y" ];then
      cat <<EOF

**WARNING**
The $ODBCINST file is not writable by you.
You will need to add the driver 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
    fi
    exit 0
  fi
  #
  # NB. The template file must be < 14 chrs as there is a bug in unixODBC
  # for HP-UX that restricts the file to FILENAME_MAX which is 14.
  #
  #
  # Check to see if driver is already registered
  #
  SYSDSNFILE=`$UNIXODBCPATH/bin/odbcinst -j | grep DRIVERS | awk '{print $2}'`
  if [ "$TESTEXISTS" "$SYSDSNFILE" ]; then
    if [ "$QUIET" != "y" ];then
      $ECHO
      $ECHO "Drivers already installed for unixODBC are:"
      unixodbcdrvs=`$UNIXODBCPATH/bin/odbcinst -d -q`
      $UNIXODBCPATH/bin/odbcinst -d -q
      $ECHO
      $ECHO "xxx $unixodbcdrvs" | grep $PRODUCT 1>/dev/null 2>/dev/null
      if [ $? -eq 0 ];then
        cat <<EOF
*****
WARNING:
It appears the $PRODUCTLONG driver is already registered
with unixODBC.
If you are reinstalling the $PRODUCT to a different install path you
may have to edit your odbcinst.ini file to correct it as the
following odbcinst commands to install the driver will simply increase
the usage count and not affect the paths.
*****
EOF
        $ECHO "Press the return key to continue"
	read press
      fi
    fi
  fi
  if [ "$QUIET" != "y" ]; then
    $ECHO "Running odbcinst command:"
    $ECHO "$UNIXODBCPATH/bin/odbcinst -i -d -f $TEMPLATE_FILE"
    $ECHO "=========="
    $UNIXODBCPATH/bin/odbcinst -i -d -f $TEMPLATE_FILE
    $ECHO "=========="
    if [ $? -ne 0 ]; then
      $ECHO "** Failed to install $PRODUCTLONG for unixODBC **"
    fi
  else
    $UNIXODBCPATH/bin/odbcinst -i -d -f $TEMPLATE_FILE 1>/dev/null 2>/dev/null
    $ECHO "Driver installed"
  fi
#
# Install ODBC DSN
# ================
#
elif [ "$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

