#!/bin/sh
#
# $Id: install,v 1.41 1999/12/01 14:37:58 martin Exp martin $
#
# $Log: install,v $
# Revision 1.41  1999/12/01 14:37:58  martin
# Further changes to remove double slashes from output.
# Fix typo: libdodbc -> libodbc
#
# Revision 1.40  1999/11/29 12:47:40  martin
# Add check for id.
# Create the licence dir and licences file if neccessary.
#
# Revision 1.39  1999/11/25 10:00:51  martin
# Bourne shell on Solaris needs -h to test for symbolic links and there is
# 	no -e on test, so using -r.
#
# Revision 1.38  1999/11/24 10:30:32  martin
# FreeBSD uses -h in test to test for symbolic link not -L.
# Internal bug ID 617 - double /'s shown in path in warning message.
#
# Revision 1.37  1999/11/17 10:55:31  martin
# Replace zeros in the word ODBC with O's.
# Fix a few other typos.
# Fix handling of "Define new service" question which says use 'q' and tests
# 	for 'n'.
#
# Revision 1.36  1999/11/12 10:32:20  martin
# Add stuff about licensing.
#
# Revision 1.35  1999/10/27 13:23:52  martin
# Check echo interprets backslash chrs and modify by adding -e if necessary.
#
# Revision 1.34  1999/10/27 12:01:30  martin
# FIx the path added to ld.so.conf for unixODBC to add the lib dir.
# Fix the LDLIBDIRS variable to substitute : for spaces in LDPATHLIBDIRS.
# When adding paths to ld.so.conf loop through LDLIBDIRS not LDPATHLIBDIRS.
# Ensure paths added to ld.so.conf end in newline.
# Make sure notes for esoobserver startup shell script are added to script
# 	instead of output to screen.
#
# Revision 1.33  1999/10/15 10:13:30  martin
# Incorrect path added to ld.so.conf.
#
# Revision 1.32  1999/10/14 17:14:00  martin
# If the installer does not install the server the root-continue
# 	message still pops up.
#
# Revision 1.31  1999/10/14 16:51:40  martin
# Chnage note at end of inetd startup.
#
# Revision 1.30  1999/10/14 16:31:41  martin
# Look for unixodbc tar file before asking to install it.
# If the server was not installed skip inetd/services stuff.
#
# Revision 1.29  1999/10/14 14:36:14  martin
# Create /usr/local if necessary when non-default install path chosen.
# Do not ask to install server services if server not installed.
#
# Revision 1.28  1999/10/14 13:56:27  martin
# Only offer unixODBC install if the tar file exists.
#
# Revision 1.27  1999/10/14 12:40:36  martin
# Use the testlib binary in Linux to detect the libc version instead of all
# 	that manic script.
#
# Revision 1.26  1999/10/13 10:45:29  martin
# Change Press any key messages to press return key.
# Add a check for the installer being root at the start. Warn about the
# 	consequences of not being root but still offer the installer a
# 	chance to continue.
# If the installer does not choose the default install path then symbolically
# 	link /usr/local/easysoft to install_path/easysoft.
# 	Without this binaries built with runpath will not find the shared
# 	objects.
# Fix bug in setting of LDLIBDIRS.
# OOB may be installed under unixODBC on any platform.
# Add the section on untarring the separate server files.
#
# Revision 1.25  1999/10/13 09:03:49  martin
# Change the messages saying a command is missing to be a little more friendly.
# Check for pwd.
# Check for head.
# Major change due to components of OOB being split up into multiple tar files
# 	e.g. client, common, server and unixODBC.
# Use the dynamic linker version to test for libc/glibc.
# Offer chance to install unixODBC.
# Ensure LD_LIBRARY_PATH and LD_RUN_PATH are set before attempting to use
# 	unixODBC commands.
#
# Revision 1.24  1999/09/27 13:19:20  martin
# Fix small bug in handling unixODBC install question.
# Allow installer to replace existing inetd.conf and service
# 	entries instead of forcing new defn.
#
# Revision 1.23  1999/09/16 16:05:20  martin
# Remove duplicate SCO lines.
# Fix bug where LD_LIBRARY_PATH derefererenced.
#
# Revision 1.22  1999/09/14 10:55:07  martin
# Simply add another "Press any key to continue" message so split install
# 	up a bit (and let installers see the console output).
#
# Revision 1.21  1999/09/06 15:02:19  martin
# Add comments on building the unixODBC/OOB Setup library.
#
# Revision 1.20  1999/09/03 14:54:22  martin
# Some OS's find command has to have an action or they return an error.
#
# Revision 1.19  1999/09/03 14:32:36  martin
# Add comment to read the INSTALL file BEFORE installing.
#
# Revision 1.18  1999/09/03 12:21:34  martin
# Add echo messages about running ldconfig and possible warnings output.
# Add support for installing OOB under unixODBC.
#
# Revision 1.17  1999/08/04 12:22:17  martin
# Changed occurrances of can't in cat <<EOF to cannot as some shells
# 	parse the text between cat and EOF.
# Check for find command.
# Change the code at the start which outputs the commands as they are
# 	found to list them across the screen instead of down the screen
# 	to save space and although it to be seen when the terminal scrolls.
# Get the OS release and output a message.
# Add support for SCO Openserver.
# For Linux add a whole host of checks:
# 	Output warning messages for untested kernel versions.
# 	Check the OOB distribution (libc or glibc) matches the c runtime
# 		library in use.
# 	Check for the minimum kernel requirements of libc.
# 	Check we recognise the current runtime libc.
# Fix silly problem testing uid when only set in Linux-specific code.
# Changes to the creation of the shell-startup script. Generate it now
# 	instead of substitution in a template.
#
# Revision 1.16  1999/07/26 15:15:50  martin
# Add "Press any key to continue" messages to allow the screens to be
# 	read one page at a time.
# Change the initial text to explain root requirements better.
# More logical installation order for the OOB client. If not root tell the
# 	installer that entries in ld.so.conf can not be made and should be
# 	done manually. If root, then add the entries BEFORE moving on to
# 	ask whether the service and inetd entries should be added.
# When adding the service and inetd entries add explanation of what they
# 	are for.
# Add messages showing whether the Client and Server were installed OK.
#
# Revision 1.15  1999/07/13 14:24:11  martin
# Fix internal bug ID 373, wrong path added to server/server cd command.
# Fix bug locating server name that already exists - used single quotes
#      around sname preventing env expansion.
#
# Revision 1.14  1999/06/24 08:28:24  martin
# Add rcs history.
#
#
#
# Clear the screen
#
if [ -x /usr/bin/clear ]; then
  /usr/bin/clear
fi
#
# Output starting screen
#
cat <<EOF
Installation procedure for the Easysoft ODBC-ODBC Bridge 2000

(c) Easysoft Ltd 1999

Please read the file INSTALL before attempting to install.
EOF
#
# Which echo?
#
if [ -x /bin/echo ];then
  ECHO="/bin/echo"
else
  if [ -x /usr/bin/echo ];then
    ECHO="/usr/bin/echo"
  else
    cat <<EOF
Cannot find the echo command
EOF
  exit 1
  fi
fi
#
# echo interprets backslash chrs by default? POSIX says no.
#
test=`$ECHO "fred\c"`
if [ "$test" = "fred\c" ];then
  ECHO="$ECHO -e "
  test=`$ECHO "fred\c"`
  if [ "$test" != "fred" ];then
    cat <<EOF
echo command ($ECHO) does not interpret backslash characters.
You may see backslash c's when asked questions during this install.
You can safely ignore this except in Linux where you may need to
edit /etc/ld.so.conf to remove extra backslash n's from the lines added
by this script.
EOF
  fi
fi
#
# License
#
cat <<EOF
                             IMPORTANT NOTICE

This installation may be used to install the Easysoft ODBC-ODBC Bridge 2000.

The installation of this SOFTWARE and its associated LICENSE is subject to
                   the Easysoft End-User License Agreement.
Your use of the SOFTWARE and LICENSE shows your acceptance of the terms
                              and conditions.

Before proceeding you should read the Client-License.txt if you are only
installing the Client and the Server-Client-License.txt if you are
installing the Server.

EOF
answer=hoho
while [ "$answer" != "q" -a "$answer" != "no" -a "$answer" != "yes" ]
do
  $ECHO "Do you accept the license? (q=quit to read license, yes, no): \c"
  read answer
  if [ "$answer" = "q" ];then
    $ECHO "Installation terminated at user request"
    exit 99
  elif [ "$answer" = "yes" ];then
    $ECHO "License accepted by user"
  elif [ "$answer" = "no" ];then
    $ECHO "License not accepted by user - installation terminated"
    exit 99
  else
   $ECHO "You must answer \"yes\", \"no\" or (q)uit"
  fi
done


#
# Check we have the tools we need.
# ================================
#
$ECHO "Checking you have the basic tools for the install..."
$ECHO
if ( pwd 1>/dev/null 2>/dev/null )
then
  $ECHO "pwd \c"
else
  $ECHO "Cannot install without \"pwd\""
fi
if ( echo "test" | grep test 1>/dev/null 2>/dev/null )
then
  $ECHO "grep \c"
else
  $ECHO "Cannot install without \"grep\""
  exit 1
fi
if ( $ECHO "test" | awk '{print $1}' 1>/dev/null 2>/dev/null )
then
  $ECHO "awk \c"
else
  $ECHO "Cannot install without \"awk\""
  exit 1
fi
if ( $ECHO "test" | cut -b1-2 1>/dev/null 2>/dev/null )
then
  $ECHO "cut \c"
else
  $ECHO "Cannot install without \"cut\""
  exit 1
fi
if ( ps 1>/dev/null 2>/dev/null )
then
  $ECHO "ps \c"
  PSOK="y"
else
  $ECHO "Can't find a working \"ps\" command"
  $ECHO "Installation will be unable to restart inetd for you"
  PSOK="n"
fi
if ( $ECHO "test" | sed 's/test/fred/g' 1>/dev/null 2>/dev/null )
then
  $ECHO "sed \c"
else
  $ECHO "Cannot install without \"sed\""
  exit 1
fi
if ( $ECHO "test" | cat 1>/dev/null 2>/dev/null )
then
  $ECHO "cat \c"
else
  $ECHO "Cannot install without \"cat\""
  exit 1
fi
if ( $ECHO "test" | wc -w 1>/dev/null 2>/dev/null )
then
  $ECHO "wc \c"
else
  $ECHO "Cannot install without \"wc\""
  exit 1
fi
if ( $ECHO "test" | head -1 1>/dev/null 2>/dev/null )
then
  $ECHO "head \c"
else
  $ECHO "Cannot install without \"head\""
  exit 1
fi
if ( uname -s 1>/dev/null 2>/dev/null )
then
  $ECHO "uname \c"
else
  $ECHO "Cannot install without \"uname\""
  exit 1
fi
if ( $ECHO "TeSt" | tr '[A-Z]' '[a-z]' 1>/dev/null 2>/dev/null )
then
  $ECHO "tr \c"
else
  $ECHO "Cannot install without \"tr\""
  exit 1
fi
if ( find . -print 1>/dev/null 2>/dev/null )
then
  $ECHO "find \c"
else
  $ECHO "Cannot install without \"find\""
  exit 1
fi
if ( id 1>/dev/null 2>/dev/null )
then
  $ECHO "id \c"
else
  $ECHO "Cannot install without \"id\""
  exit 1
fi
$ECHO
$ECHO
#
# Set up vars used throughout.
# ============================
#
DEFAULT_PATH="/usr/local"    # default base install path
INST_SUBDIR="easysoft"       # dir under above OOB installed in
SUPPORT_EMAIL="support@easysoft.com"
INSTALLED_UNIXODBC=""        # installed unixODBC DM?
INSTALLED_CLIENT=""          # installed OOB Client?
INSTALLED_SERVER=""          # installed OOB Server?
INSTALLED_COMMON=""          # installed common OOB components?
INSTALLED_LDSO_LINKS=""      # Updated ld.so (linux only)

#
# See what platform we are on
# ===========================
#
PLATFORM=`uname -s`
platform=`uname -s | tr '[A-Z]' '[a-z]'`
osrelease=`uname -r`
osversions=`uname -v`
echo "Installation for $platform chosen"
#
# Platform specific differences
# =============================
#
# PIDARG is column is ps output where process ID shown
# PSARGS are the arguments to ps to generate process output that can be
# grepped for inetd's PID
#
PIDARG="1"
TESTLINK="-L"
TESTEXISTS="-e"
if [ "$platform" = "aix" ];then
  PSARGS="ax"
fi
if [ "$platform" = "hp-ux" ];then
  PSARGS="-ef"
  PIDARG="2"
fi
if [ "$platform" = "linux" ];then
  PSARGS="ax"
fi
if [ "$platform" = "sunos" ];then
  PSARGS="-ef"
  PIDARG="2"
  TESTEXISTS="-r"
  TESTLINK="-h"
fi
if [ "$platform" = "sco_sv" ];then
  PSARGS="-ef"
  PIDARG="2"
fi
if [ "$platform" = "freebsd" ];then
  PSARGS="ax"
  TESTLINK="-h"
fi
#
# Platform specific checks
# ========================
#
# Linux - see what the highest version of libc installed is.
#
if [ "$platform" = "linux" ];then
  $ECHO "Performing $platform-specific tests"
  $ECHO "Operating System Version: $osrelease"
  osmaj=`$ECHO "$osrelease" | cut -d. -f1`
  osmin=`$ECHO "$osrelease" | cut -d. -f2`
  #
  #  Output warning for development kernels.
  #
  if [ "$osmaj" = "2" -a "$osmin" = "3" ];then
    cat <<EOF
****
WARNING:
You appear to be running a development kernel. Easysoft have not verified the
ODBC-ODBC Bridge works with development kernels although it is not believed
there is anything in the ODBC-ODBC Bridge which would prevent it working
with current development kernels.
****
EOF
  fi
  #
  #  Output incompatible for 1.n kernels.
  #
  if [ "$osmaj" = "1" ];then
    cat <<EOF
****
WARNING:
You appear to be running a 1.n series Linux kernel. Easysoft have not verified
the ODBC-ODBC Bridge works with these kernels and it is thought that they
probably will not. Please contact us at $SUPPORT_EMAIL if you
find otherwise.
****
EOF
  fi
  #
  #  Try and run our testlib program which is linked with the same version
  #  of libc5/glibc as the rest of the distribution. If it doesn't run the
  #  user is probably trying to install the wrong version of OOB on this
  #  machine.
  #
  if [ -x ./testlib ]; then
    $ECHO "Testing this machine for libc5/glibc compatability."
    response=`./testlib`
    if [ \( $? -ne 0 \) -o \( "$response" != "hello" \) ];then
      cat <<EOF
The test program (./testlib) failed to run correctly. The likely causes of this
are:

[1] You are installing the wrong version of the ODBC-ODBC Bridge for this
    machine. e.g. installing a glibc version on a machine without glibc.
    The preceding output is likely to mention undefined symbols such as
    "__register_frame_info" in this case.

    If this is the case then you should download the other version of OOB
    and install that.

[2] You are installing the ODBC-ODBC Bridge on a machine with an incompatible
    version of glibc. e.g. installing the glibc version on a machine which
    has a version of glibc before 2.0.7.

    If this is the case then you will need to upgrade glibc to at least 2.0.7.

The installation is being aborted due to incompatible C runtime library.
EOF
      exit 99
    else
      $ECHO "Passed C runtime library check"
    fi
  else
    $ECHO "Runtime library check skipped due to missing distribution "
    $ECHO "testlib program."
  fi
fi
#
# Check the package
# =================
#
# Check the tar file(s) exist (there may be more than one)
#  e.g. for multiple sub-packages
# Check the md5sum/sum is correct.
# 
$ECHO "Press the return key to continue"
read press
$ECHO
$ECHO "Checking your package is OK..."
TARPATTERN="*.tar"
TARFILE_LIST=`$ECHO $TARPATTERN`
exist=`$ECHO $TARFILE_LIST`
#
# If the shell did not expand *.tar then there can't be any matching
# tar files in this dir.
#
if [ "$exist" = "$TARPATTERN" ]; then
  $ECHO "Cannot find any tar file packages matching \"$TARPATTERN\" in"
  $ECHO "the current directory. Are you sure you are running the install"
  $ECHO "script in the same directory the first tar file was unpacked to?"
  exit 1
fi
#
# Find out how many tar files we have and check each one.
#
TARFILES=`$ECHO $TARFILE_LIST | wc -w | awk '{print $1}'`
$ECHO "Found $TARFILES package(s)"
for TARFILE in $TARFILE_LIST
do
  $ECHO "Checking $TARFILE"
  if [ ! -r $TARFILE ];then
    $ECHO "Cannot find readable $TARFILE file"
    exit 1
  fi
  #
  # Try checking the tar file with md5sum.
  #
  checked=""
  if [ -r $TARFILE.md5sum ];then
    if ( $ECHO "test" | md5sum 2> /dev/null 1> /dev/null )
    then
      check=`md5sum $TARFILE | awk '{print $1}'`
      check2=`cat $TARFILE.md5sum`
      if [ "$check" = "$check2" ];then
  	checked="ok"
      else
  	checked="fail"
      fi
    else
      "Can't find md5sum on this machine"
    fi
  else
    $ECHO "$TARFILE.md5sum not found - ignoring."
  fi
  #
  # Try checking the tar file with sum.
  #
  if [ "$checked" = "" ];then
    if  [ -r $TARFILE.sum ];then
      if ( $ECHO "test" | sum 2> /dev/null 1> /dev/null )
      then
  	check=`sum $TARFILE | awk '{print $1}'`
  	check2=`cat $TARFILE.sum`
  	if [ "$check" = "$check2" ];then
  	  checked="ok"
  	else
  	  checked="fail"
  	fi
      else
  	$ECHO "Can't find sum on this machine"
      fi
    else
      $ECHO "$TARFILE.sum not found - ignoring,"
    fi
  fi
done
#
# Tar files not checksummed
# =========================
#
# If we failed to check a tar file then give the installer a chance to
# pull out of the installation.
#
if [ "$checked" = "" ];then
  $ECHO "Tarred packages not checked - can't find md5sum or sum."
  $ECHO "This package comes with a md5 message digest and a checksum."
  $ECHO "The installation was unable to check the package due to either"
  $ECHO "[1] failed to locate md5sum or sum."
  $ECHO "[2] failed to locate the md5 or checksum package files."
  $ECHO "It is not absolutely necessary to check your package in this way"
  $ECHO "and it is usually safe to continue."
  $ECHO
  $ECHO "Do you want to continue? (y/n) [n]: \c"
  answer="haha"
  while [ "$answer" != "" -a "$answer" != "n" -a "$answer" != "y" ]
  do
    read answer
    if [ "$answer" = "" ];then
      answer="n"
    fi
    if [ "$answer" != "y" ];then
      $ECHO "Installation terminated"
      exit 99
    fi
  done
fi
$ECHO "Package check - OK"
$ECHO
$ECHO "Press the return key to continue"
read press
#
# Check for root
# ==============
#
$ECHO
$ECHO "This installation requires the installer to be the root user."
uid=`id | awk '{print $1}' | awk -F= '{print $2}' | awk -F\( '{print $1}'`
if [ "$uid" != "0" ];then
  cat <<EOF
and you do not appear to be root. You can still install the OOB client
but the installation will be unable to update the dynamic linker search
paths or install OOB under the unixODBC Driver Manager (the latter is
not necessary). You may complete these processes manually by consulting
the INSTALL file in the current directory.

You will not be able to install the OOB Server as a non-root user.

EOF
  $ECHO "Do you want to continue with the installation? (y/n) [n]: \c"
  answer="hoho"
  while [ "$answer" != "" -a "$answer" != "n" -a "$answer" != "y" ]
  do
    read answer
    if [ "$answer" = "" ];then
      answer="n"
    fi
  done
  if [ "$answer" = "n" ];then
    $ECHO "Installation aborted at user request"
    exit 99
  fi
fi
#
#  Install Path
#  ============
#
cat <<EOF

You need to enter a location for the Easysoft ODBC-ODBC Bridge. You
should specify an existing directory under which directories called
"$INST_SUBDIR/oob", "$INST_SUBDIR/lib" and possibly
"$INST_SUBDIR/unixODBC" will be created. You need
write permission to the specified directory. If the last directory in the
path you specify does not exist the installation will ask whether
you would like it to be created.

Just hit <enter> for the defaults.
EOF

#
# Request and create installation path
# ====================================
#
path=""
while [ "$path" = "" ]
do
  $ECHO "Enter a base install directory (q=quit) [ $DEFAULT_PATH ]: \c"
  read path
  if [ "$path" = "q" ];then
    $ECHO "Installation aborted at user request"
    exit 99
  fi
  if [ "$path" = "" ];then
    path="$DEFAULT_PATH"
  fi
  if [ ! -d "$path" ]; then
    $ECHO "$path does not exist"
    $ECHO "Do you want to attempt to create it (y/n) [y]: \c"
    answer="hoho"
    while [ "$answer" != "" -a "$answer" != "n" -a "$answer" != "y" ]
    do
      read answer
      if [ "$answer" = "" ];then
        answer="y"
      fi
      if [ "$answer" = "y" ];then
        mkdir $path
	if [ $? -ne 0 ]
	then
          $ECHO "Failed to create $path"
	  path=""
	fi
      else
        path=""
      fi
    done
  elif [ ! -w "$path" ]; then
    $ECHO "$path is not writeable"
    path=""
  fi
done
$ECHO
#
# We now make the <install_path>/$INST_SUBDIR tree.
# INST_SUBDIR may have more than one dir in it.
#
$ECHO "Making install dir \c"
echo "$path/$INST_SUBDIR" | sed 's,//,/,g'
flds=`echo "$INST_SUBDIR" | sed 's,/, ,g'`
base=`echo "$INST_SUBDIR" | awk -F/ '{print $NF}'`
stem=""
for dir in $flds
do
  xpath=`echo $path/$stem/$dir | sed 's,//,/,g'`
  if [ -d "$path/$stem/$dir" ];then
    $ECHO "$xpath already exists."
    $ECHO
    if [ "$dir" = "$base" ];then
      $ECHO "If you are reinstalling then it is wise to delete"
      $ECHO "$path/easysoft/oob first"
      $ECHO "(as this will tell you if files are in use)."
      $ECHO
      $ECHO "If you are upgrading from a previous beta then Easysoft strongly"
      $ECHO "recommend you remove the previous beta first."
      $ECHO "However, you may continue if you wish."
      $ECHO ""
      $ECHO "Continue with install? (y/n) [n]: \c"
      answer="haha"
      while [ "$answer" != "" -a "$answer" != "n" -a "$answer" != "y" ]
      do
        read answer
        if [ "$answer" = "" ];then
          answer="n"
        fi
        if [ "$answer" != "y" ];then
          $ECHO "Installation terminated"
          exit 99
        fi
      done
    fi
  else
    if ( mkdir $xpath )
    then
      $ECHO "$xpath created"
    else
      $ECHO "Failed to create $xpath"
      exit 1
    fi
  fi
  stem="$stem/$dir"
done
#
#  If not installed in the default place provide a symbolic link from the
#  default to the actual or shared object runpaths won't work.
#
if [ "$path" != "$DEFAULT_PATH" ];then
cat <<EOF

You have not chosen the default path for installing the Easysoft ODBC-ODBC
Bridge. For the built-in shared object run paths to work the installation
needs to link $DEFAULT_PATH/$INST_SUBDIR to $path/$INST_SUBDIR.

EOF
  answer="hoho"
  if [ "$TESTEXISTS" "$DEFAULT_PATH/$INST_SUBDIR" ];then
    $ECHO "but $DEFAULT_PATH/$INST_SUBDIR already exists"
    if [ "$TESTLINK" "$DEFAULT_PATH/$INST_SUBDIR" ];then
      $ECHO "and is a symbolic link."
      $ECHO "Do you want to replace the symbolic link"
      $ECHO "$DEFAULT_PATH/$INST_SUBDIR ? (y/n) [y]: \c"
      while [ "$answer" != "" -a "$answer" != "n" -a "$answer" != "y" ]
      do
  	read answer
  	if [ "$answer" = "" ];then
  	  answer="y"
  	fi
      done
    else
      $ECHO "and is not a symbolic link."
      $ECHO "Please see the INSTALL file for information on how this may be corrected"
    fi
  else
    if [ ! "$TESTEXISTS" /usr/local ]; then
      if ( mkdir /usr/local )
      then
        $ECHO "/usr/local created"
      else
        $ECHO "Failed to create /usr/local"
      fi
    fi
    answer="y"
  fi
  if [ "$answer" = "y" ];then
    ln -sf $path/$INST_SUBDIR $DEFAULT_PATH/$INST_SUBDIR
    if [ $? -ne 0 ]; then
      $ECHO "**WARNING** Failed to create symbolic link"
      $ECHO "See INSTALL file for information on how this may be corrected"
    fi
  fi
fi
#
# Install unixODBC DM
# ===================
#
# Give the installer a chance to install unixODBC
#
$ECHO "Press the return key to continue"
read press
upat="unixodbc*.tar"
ue=`$ECHO $upat`
if [ "$ue" != "$upat" ];then
  cat <<EOF

The Easysoft ODBC-ODBC Bridge comes with the Driver Manager from the unixODBC
project (see http://www.unixodbc.org for details). You do not need to install
the unixODBC Driver Manager but you should consider doing so if:

[1] You intend using multiple ODBC drivers under the OOB Server.
[2] You already have other ODBC Drivers installed on this machine.

You can download the entire source distribution of unixODBC from
ftp://ftp.easysoft.com/pub/beta/unixODBC and install it independently if
you would prefer. If you already have unixODBC installed then skip this
section.

EOF
  answer="hoho"
  $ECHO "Install unixODBC Driver Manager? (y/n) [n]: \c"
  while [ "$answer" != "" -a "$answer" != "n" -a "$answer" != "y" ]
  do
    read answer
    if [ "$answer" = "" ];then
      answer="n"
    fi
    if [ "$answer" = "y" ];then
      cwd=`pwd`
      $ECHO "Unpacking unixODBC Driver Manager"
      cd $path/$INST_SUBDIR
      $ECHO
      $ECHO "Press the return key to continue"
      read press
      tar -xvf $cwd/unixodbc*.tar
      INSTALLED_UNIXODBC="y"
      cd "$cwd"
    fi
  done
fi
#
# Install OOB Common
# ==================
#
# Unpack the common tar file
#
cwd=`pwd`
cd $path/$INST_SUBDIR
$ECHO
$ECHO "Unpacking Common parts of the ODBC ODBC Bridge distribution"
$ECHO "Press the return key to continue"
read press
tar -xvf $cwd/all*.tar
INSTALLED_COMMON="y"
cd "$cwd"
#
# Install the OOB Client
# ======================
#
cat <<EOF

You can now install the Client side of the ODBC-ODBC Bridge. You need to
install the Client if you want to access remote ODBC Drivers from applications
running on this machine e.g. A Perl DBI script on this machine accessing
MS SQLServer on a remote NT machine.

EOF
answer="hoho"
$ECHO "Install ODBC-ODBC Bridge Client (y/n) [y]: \c"
while [ "$answer" != "" -a "$answer" != "y" -a "$answer" != "n" ]
do
  read answer
  if [ "$answer" = "" ];then
    answer="y"
  fi
  if [ "$answer" = "y" ];then
    #
    # Unpack the Client tar file
    #
    cwd=`pwd`
    cd $path/$INST_SUBDIR
    $ECHO
    $ECHO "Unpacking Client parts of the ODBC-ODBC Bridge distribution"
    $ECHO "Press the return key to continue"
    read press
    tar -xvf $cwd/client*.tar
    INSTALLED_CLIENT="y"
    $ECHO
    cd $cwd
  fi
done
#
$ECHO "Press the return key to continue"
read press
#
# Set LD_LIBRARY_PATH and LD_RUN_PATH so we can pick up unixODBC libs.
#
LDPATHLIBDIRS="$path/$INST_SUBDIR/lib"
if [ "$INSTALLED_CLIENT" != "" ];then
  LDPATHLIBDIRS="$LDPATHLIBDIRS:$path/$INST_SUBDIR/oob/client"
fi
if [ "$INSTALLED_UNIXODBC" != "" ];then
  LDPATHLIBDIRS="$LDPATHLIBDIRS:$path/$INST_SUBDIR/unixODBC/lib"
fi
LD_LIBRARY_PATH="$LD_LIBRARY_PATH:$LDPATHLIBDIRS"
export LD_LIBRARY_PATH
LD_RUN_PATH="$LD_RUN_PATH:$LDPATHLIBDIRS"
export LD_RUN_PATH
LDLIBDIRS=`echo $LDPATHLIBDIRS | sed 's/:/ /g'`
#
# Install ld.so paths
# ===================
#
# To finish off the Client/unixODBC/Common install for Linux, we need to add
# entries to the /etc/ld.so.conf file for the dynamic linker. We need to be
# root for that. In all cases we at least need to install the oob/lib dir.
#
if [ "$platform" = "linux" ];then
  $ECHO "Attempting to update the dynamic linker with OOB shared object paths."
  $ECHO
  uid=`id | awk '{print $1}' | awk -F= '{print $2}' | awk -F\( '{print $1}'`
  if [ "$uid" != "0" ];then
    cat <<EOF
You do not appear to be root and so the OOB client install is incomplete.
You may need to add

$LDLIBDIRS

to the dynamic linker configuration file and run ldconfig to pick up
the ODBC-ODBC Bridge shared objects.
EOF

  else				# Linux and root
    #
    # Attempt to add an entry to ld.so.conf for the client shared object
    # and the lib directory containing the product-shared shared objects.
    # This makes building Apache and PHP a little easier.
    #
    if [ ! -w /etc/ld.so.conf ];then
      $ECHO "Cannot find/write your dynamic linker configuration file"
      $ECHO "/etc/ld.so.conf. You must inform the dynamic linker that"
      $ECHO "LDLIBDIRS"
      $ECHO "contain shared objects manually."
    else
      runld="n"
      for ldpath in $LDLIBDIRS
      do
        if ( grep "$ldpath" /etc/ld.so.conf 1>/dev/null )
        then
          $ECHO "$ldpath already exists in /etc/ld.so.conf"
        else
          $ECHO "Adding \"$ldpath\" to /etc/ld.so.conf."
          runld="y"
          $ECHO "$ldpath\n" >> /etc/ld.so.conf
        fi
      done
      if [ "$runld" = "y" ];then
        if [ ! -x /sbin/ldconfig ];then
          $ECHO "Failed to find ldconfig"
          $ECHO "You must inform the dynamic linker of the change to ld.so.conf manually"
        else
          $ECHO "Running ldconfig"
          $ECHO "Any \"can\'t open\" warnings are unlikely to be a result of this installation"
          /sbin/ldconfig
          INSTALLED_LDSO_LINKS="y"
        fi
      fi
    fi
  fi
else
 cat <<EOF
You may need to add

$LDLIBDIRS

paths to your LD_RUN_PATH or LD_LIBRARY_PATH
(depending on your operating system) environment variable and export it
before you can use the OOB client shared objects.
EOF
fi
$ECHO
$ECHO "** The OOB Client has been installed successfully **"
$ECHO
$ECHO "Press the return key to continue"
read press

#
# Install OOB Client under unixODBC DM
# ====================================
#
cat <<EOF

You can install OOB for the unixODBC (http://www.unixodbc.org) driver manager
now if you would like. You do not have to do this as OOB works fine without
a driver manager. However, if you have already installed unixODBC then
you probably want to do this.

You will probably have to be the root user to do this.

If you choose this option you should look at the unixODBC subdirectory
of the OOB (e.g. /usr/local/easysoft/oob/unixODBC) as the setup library
required by unixODBC to create OOB DSNs may have to be built for your
platform.

EOF
answer="hoho"
while [ "$answer" != "" -a "$answer" != "y" -a "$answer" != "n" ]
do
  $ECHO "Install OOB into unixODBC (y/n) [n]: \c"
  read answer
  if [ "$answer" = "" ];then
    answer="n"
  fi
done
if [ "$answer" = "y" ];then
  #
  #  try and locate odbcinst command
  #
  found="1"
  if [ "$INSTALLED_UNIXODBC" != "" ];then
    unixodbcpath="$path/$INST_SUBDIR/unixODBC"
  else
    unixodbcpath="/usr/local"
  fi
  if [ ! -x "$unixodbcpath/bin/odbcinst" ];then
    $ECHO "The odbcinst command to install drivers is not in the default"
    $ECHO "location of $unixodbcpath/bin/odbcinst"
    $ECHO "Where did you install unixODBC (i.e. what did you set --prefix to)"
    found="0"
    while [ "$found" = "0" ]
    do
      $ECHO "unixODBC install path? (q to quit OOB for unixODBC install): \c"
      answer=""
      while [ "$answer" = "" -o "$answer" != "q" ]
      do
        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
	      unixodbcpath="$answer"
          found="1"
        fi
      done
    done
  fi
  if [ "$found" = "1" ];then
    > unixODBC.template
    $ECHO "[OOB]" >> unixODBC.template
    $ECHO "Description = Easysoft ODBC-ODBC Bridge" >> unixODBC.template
    oobso=`find $path/$INST_SUBDIR/oob/client -name "libesoobclient.*" -print | head -1`
    $ECHO "Driver = $oobso" >> unixODBC.template
    oobsetupso=`find $path/$INST_SUBDIR/oob/client -name "libesoobsetup.*" -print`
    $ECHO "Setup = $oobsetupso" >> unixODBC.template
    #
    # Check to see if OOB is already registered
    #
    $ECHO
    $ECHO "Drivers already installed for unixODBC are:"
    unixodbcdrvs=`$unixodbcpath/bin/odbcinst -d -q`
    $unixodbcpath/bin/odbcinst -d -q
    $ECHO
    $ECHO "xxx $unixodbcdrvs" | grep OOB 1>/dev/null 2>/dev/null
    if [ $? -eq 0 ];then
      cat <<EOF
*****
WARNING:
It appears the OOB driver is already registered with unixODBC. If you are
reinstalling the OOB to a different install path you may have to hand
edit your odbcinst.ini file to correct it as the following odbcinst
commands to install the OOB driver will simply increase the usage count
and not affect the paths.
*****
EOF
    fi
    $ECHO "Running odbcinst command:"
    $ECHO "$unixodbcpath/bin/odbcinst -i -d -f unixODBC.template"
    $ECHO "=========="
    $unixodbcpath/bin/odbcinst -i -d -f unixODBC.template
    $ECHO "=========="
    if [ $? -ne 0 ]; then
      $ECHO "** Failed to install OOB for unixODBC **"
    fi
  fi # end of if odbcinst found
fi
#
# Install the OOB Server (files)
#
cat <<EOF

You can now install the Server side of the ODBC-ODBC Bridge. You need to
install the Server if you want to access ODBC drivers on this machine
from other remote machines (where you install the OOB Client). The
OOB Server is linked against the libodbc shared object so you must either:

[1] Install the unixODBC Driver Manager (which provides libodbc) and then
    tell the driver manager about your ODBC Drivers OR
[2] Ensure that the ODBC driver you want to use is available as libodbc.so
    (amend extension appropriate for platform). You can do this by simply
    renaming your ODBC driver.

EOF
answer="hoho"
$ECHO "Install ODBC-ODBC Bridge Server (y/n) [y]: \c"
while [ "$answer" != "" -a "$answer" != "y" -a "$answer" != "n" ]
do
  read answer
  if [ "$answer" = "" ];then
    answer="y"
  fi
  if [ "$answer" = "y" ];then
    #
    # Unpack the Server tar file
    #
    cwd=`pwd`
    cd $path/$INST_SUBDIR
    $ECHO
    $ECHO "Unpacking Server parts of the ODBC-ODBC Bridge distribution"
    $ECHO "Press the return key to continue"
    read press
    tar -xvf $cwd/server*.tar
    INSTALLED_SERVER="y"
    $ECHO
    cd $cwd
  fi
done
#
$ECHO "Press the return key to continue"
read press
#
# Check for an existing licence dir and file. If one does not exist make
# the licence dir and copy a blank licence file in to it.
#
cwd=`pwd`
cd $path/easysoft
if [ ! "$TESTEXISTS" licence ]; then
  if ( mkdir licence )
  then
      $ECHO "$path/easysoft/licence directory created"
  else
      $ECHO "Failed to create $path/easysoft/licence"
  fi
  if [ ! "$TESTEXISTS" licence/licences ]; then
    if ( cp oob/server/install/licences licence )
    then
      $ECHO "Licence template created"
    else
      $ECHO "Failed to copy licence template"
    fi
  fi
fi
cd $cwd
#
# root-specific install
# =====================
#
if [ "$INSTALLED_SERVER" != "y" ];then
  $ECHO "Installation completed successfully"
  exit 0
fi
uid=`id | awk '{print $1}' | awk -F= '{print $2}' | awk -F\( '{print $1}'`
$ECHO 
$ECHO "You must be root to continue on with this script and complete the OOB"
$ECHO "Server installation which adds entries to your inetd and service files."
if [ "$uid" != "0" ];then
  cat <<EOF
...and you are not root

Press the return key and the rest of this installation will be aborted.
Then see the installation documentation for how root can complete
the OOB server installation.
EOF
  read line
  exit 2
fi
$ECHO
$ECHO "Do you wish to install the services and inetd entries? (y/n) [y]: \c"
read answer
if [ "$answer" = "" ]; then
  answer="y"
fi
if [ "$answer" != "y" ]; then
  $ECHO "Exiting - installation complete."
  exit 0
fi
#
# Check we can locate the service file and the inetd.conf file.
#
cd $path/easysoft
SERVICE_FILE="/etc/services"
INETD_CONF="/etc/inetd.conf"
#
#  Check the services file already exists.
#
$ECHO "Checking for your services file"
while [ ! -w "$SERVICE_FILE" ]
do
  $ECHO "Failed to find writeable $SERVICE_FILE file"
  SERVICE_FILE=""
  while [ "$SERVICE_FILE" = "" ]
  do
    $ECHO "Enter the path to your services file (leave empty to abort) : \c"
    read SERVICE_FILE
    if [ "$SERVICE_FILE" = "" ];then
      $ECHO "Installation aborted"
      exit 99
    fi
  done
done
#
#  Check the inetd configuration file already exists.
#
$ECHO "Checking for your inetd configuration file"
while [ ! -w "$INETD_CONF" ]
do
  $ECHO "Failed to find writeable $INETD_CONF file"
  INETD_CONF=""
  while [ "$INETD_CONF" = "" ]
  do
    $ECHO "Enter the path to your inetd configuration file (leave empty to abort) : \c"
    read INETD_CONF
    if [ "$INETD_CONF" = "" ];then
      $ECHO "Installation aborted"
      exit 99
    fi
  done
done
$ECHO "Using services file $SERVICE_FILE"
$ECHO "Using inet configuration file $INETD_CONF"
#
# Install services entry
# ======================
#
#
# Get the default port and service names from the configuration file.
#
cd oob
port=`cat server/install/service.conf | awk '{print $2}' | awk -F/ '{print $1}'`
sname=`cat server/install/service.conf | awk '{print $1}'`
cat <<EOF

$ECHO Attempting to install the service and inetd entries

The service name provides a friendly mapping between textual names for
internet services, and their underlying assigned port numbers and protocol
types. inetd will listen on behalf of a service and run it when a client
connects to the specified port. The defaults for the OOB Server are:

Service Name: $sname
Port Number: $port

EOF

#
# Check that this service does not already exist.
#
$ECHO "Checking service name \"$sname\" and port \"$port\" are not already in use"
while [ `awk '{print $1}' < $SERVICE_FILE | grep "^$sname$"` ]
do
  $ECHO "Service name \"$sname\" already exists in the $SERVICE_FILE."
  $ECHO
  $ECHO "The conflicting service entry is:"
  grep "^$sname[ \009].*$" $SERVICE_FILE
  $ECHO
  $ECHO "You can choose to replace this entry or"
  $ECHO "specify a new unique service name".
  answer=""
  while [ \( "$answer" != "r" \) -a \( "$answer" != "d" \) -a \( "$answer" != "q" \) ]
  do
    $ECHO "Replace (r), Define new service name (d), (q=abort): \c"
    read answer
  done
  if [ "$answer" = "q" ];then
    $ECHO "Installation aborted"
    exit 99
  elif [ "$answer" = "d" ];then
    $ECHO "service name: \c"
    read sname
  elif [ "$answer" = "r" ];then
    cp $SERVICE_FILE $SERVICE_FILE.pre_OOB
    grep -v "^$sname[ 	].*$" $SERVICE_FILE.pre_OOB > $SERVICE_FILE
    cp $INETD_CONF $INETD_CONF.pre_OOB
    grep -v "^$sname[ 	].*$" $INETD_CONF.pre_OOB > $INETD_CONF
  fi
done
$ECHO "Cannot find service name $sname in $SERVICE_FILE - OK"
#
# Check the port is not already in use
#
defport="$port"
spareport=""
while [ "$spareport" = "" ]
do
  if (grep $port $SERVICE_FILE 1>/dev/null)
  then
    $ECHO "Attempting to use $port for the Easysoft ODBC-ODBC Bridge Server."
    $ECHO "This appears to be in use. Please choose another port."
    $ECHO "Port: \c"
    while [ "$newport" = "" ]
    do
      read newport
      $ECHO $newport | grep -E '^[0-9]+$' 1>/dev/null
      if [ $? = 1 ]; then
        $ECHO "$newport invalid - please specify a port number"
        newport=""
      fi
    done
    port="$newport"
  else
    $ECHO "Cannot find $port in $SERVICE_FILE - OK"
  fi
  spareport="$port"  
done
$ECHO "Installing a $SERVICE_FILE entry for the Easysoft OOB Server"
$ECHO "as service name $sname on port $spareport"
sed -e "s/$defport/$spareport/g" -e "s/esoobserver/$sname/g" < server/install/service.conf > server/install/service.install
cat server/install/service.install >> $SERVICE_FILE
#
# Install inetd configuration file entry
# ======================================
#
if [ ! -x /bin/sh ];then
  $ECHO "/bin/sh does not seem to exist"
  $ECHO "Please enter the full path to a shell that may be used"
  shellpath=""
  while [ "$shellpath" = "" ]
  do
    $ECHO "full shell path: \c"
    read shell
    if [ ! "$TESTEXISTS" "$shell" ]; then
      $ECHO "$shell does not exist"
      shell=""
    elif [ ! -x "$shell" ]; then
      $ECHO "$shell is not executable"
      shell=""
    fi
    shellpath="$shell"
  done
else
  shellpath="/bin/sh"
fi
sed "s|SHELL|$shellpath|g" < server/install/inetd.conf > server/install/inetd.install1
sed "s|STARTUP|$path/easysoft/oob/server/server|g" < server/install/inetd.install1 > server/install/inetd.install2
sed "s/esoobserver/$sname/g" < server/install/inetd.install2 > server/install/inetd.install 
cat server/install/inetd.install >> $INETD_CONF
#
#  Tell inetd to re-read conf files
#
if [ "$PSOK" = "y" ]; then
  $ECHO "Informing inetd of the configuration file change"
  if [ "$PIDARG" = "2" ];then
  pid=`ps $PSARGS | grep inetd | grep -v grep | awk '{print $2}'`
  else
  pid=`ps $PSARGS | grep inetd | grep -v grep | awk '{print $1}'`
  fi
  if [ "$pid" != "" ];then
    kill -HUP $pid
  fi
fi
#
# Create the shell startup script
#
sout="server/install/shell_startup.conf"
> $sout
echo "#!$shellpath" >> $sout
#
# wanted to put the following in cat >> file << EOF but some vendors Bourne
# shells interpret the comment characters.
#
echo "#" >> $sout
echo "# This is the OOB Server startup script run by inetd." >> $sout
echo "#" >> $sout
echo "# You only need to set up the OOB Server on this machine if you want" >> $sout
echo "# to gain access to ODBC databases on this machine through the OOB" >> $sout
echo "# client on a remote machine." >> $sout
echo "#" >> $sout
echo "# The OOB Server is dynamically linked with libodbc.<shared_extension>" >> $sout
echo "# You need to rename your ODBC driver or driver manager to" >> $sout
echo "# libodbc.<shared_extension> and change the path in the next line" >> $sout
echo "# to point at the directory where your ODBC driver or driver manager" >> $sout
echo "# is located." >> $sout
echo "# <shared_extension> is .so, .a or .sl depending on your platform." >> $sout
echo "#" >> $sout
echo "LD_LIBRARY_PATH=\$LD_LIBRARY_PATH:$path/easysoft/path_to_odbc_driver_shared_object" >> $sout
echo "export LD_LIBRARY_PATH" >> $sout
echo "cd $path/easysoft/oob/server" >> $sout
echo "./esoobserver inetd" >> $sout
cp server/install/shell_startup.conf server/server
#
cd $cwd
$ECHO
$ECHO "** The OOB Server has been installed successfully **"
