#!/bin/sh
#
# $Id: is_perl.sh 52 2005-05-20 15:37:52Z martin $
#
# ./is_perl [installed | check] {unixODBC_installed unixODBC_path}
#
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
# command line
if [ $# -lt 1 ];then
  $ECHO "Invalid command line"
  exit 1
fi
if [ \( "$1" != "installed" \) -a \( "$1" != "check" \) ]; then
  $ECHO "Invalid command line"
  exit 1
fi
if [ "$1" = "check" ];then
  UNIXODBCINSTALLED=$2
  UNIXODBCWHERE=$3	# if empty - not found
  DOPERLCHECKONLY="n"
else
  DOPERLCHECKONLY="y"
fi

#
# Perl
#
perl -e '' 2>/dev/null
result=$?
if [ $result -ne 0 ];then
  cat <<EOF

Cannot find Perl on this machine.

EOF
  exit 1
elif [ "$DOPERLCHECKONLY" = "y" ];then
    exit 0
fi
$ECHO "You appear to have Perl installed"
#
# Perl version
#
perlversionstr=`perl -e 'if (defined($^V)) {printf "%vd",$^V}else{print $];};'`
$ECHO "\tand it is version $perlversionstr"
#
# Is Perl threaded?
#
perl -MConfig -e 'if ($Config{useithreads}) {exit 1} else {exit 0}'
result=$?
if [ $result -eq 1 ];then
  threadedperl="y"
else
  threadedperl="n"
fi
#
# is this redhat/Mandrake?
#
debversion=""
rhversion=""
mdversion=""
if [ "$TESTEXISTS" /etc/redhat-release ];then
  if [ "$TESTEXISTS" /etc/mandrake-release ];then
    mdversion=`cat /etc/mandrake-release`
    $ECHO "This appears to be $mdversion"
  else
    rhversion=`cat /etc/redhat-release`
    $ECHO "This appears to be $rhversion"
  fi
elif [ "$TESTEXISTS" /etc/debian_version ];then
  debversion=`cat /etc/debian_version`
  $ECHO "This appears to be Debian $debversion"
fi
#
# RPM?
#
rpmversion=`rpm --version 2>/dev/null`
result=$?
if [ $result -ne 0 ];then
  rpmversion=""
else
  $ECHO "You appear to have $rpmversion"
fi
#
# If RPM look for unixODBC
#
if [ "$rpmversion" = "" ];then
  rpmunixodbc=""
  rpmunixodbcdev=""
else
  rpmunixodbc=`rpm --query unixODBC 2>/dev/null`
  result=$?
  if [ $result -ne 0 ];then
    rpmunixodbc=""
  fi
  rpmunixodbcdev=`rpm --query unixODBC-devel 2>/dev/null`
  result=$?
  if [ $result -ne 0 ];then
    rpmunixodbcdev=""
  fi
fi
#
# Look for DBI
#
perl -e 'use DBI;' 2>/dev/null
result=$?
if [ $result -ne 0 ];then
  $ECHO "You do not seem to have the Perl DBI modules installed"
  dbiversion=""
  dbidrivers=""
else
  dbiversion=`perl -MDBI -e 'print $DBI::VERSION,"\n"'`
  $ECHO "You appear to have DBI $dbiversion installed"
  dbidrivers=`perl -MDBI -e 'print join(", ", DBI->available_drivers),"\n";'`
  $ECHO "\tand these DBI drivers ($dbidrivers)"
fi
#
# Look for DBD::ODBC
#
perl -e 'use DBD::ODBC;' 2>/dev/null
result=$?
if [ $result -ne 0 ];then
  $ECHO "You do not seem to have the DBD::ODBC module installed."
  dbdodbcversion=""
else
  dbdodbcversion=`perl -MDBD::ODBC -e 'print $DBD::ODBC::VERSION,"\n"'`
  $ECHO "You appear to have DBD::ODBC $dbdodbcversion installed"
  odbcdsns=`perl -MDBI -e 'print join(", ", DBI->data_sources('ODBC'));'`
  $ECHO "\tand these data sources\n\t$odbcdsns"
fi
#
cat <<EOF

Results/Recommendations
=======================

1. Perl
EOF
if [ "$threadedperl" = "y" ];then
  cat <<EOF
  
WARNING. You are using a Perl configured with threading enabled. Please
see the warnings in DBI when it is installed about using multiple threads
not being recommended for production environments. Also consult the OOB
FAQ about thread-enabled Perl and ODBC drivers.
EOF
else
  $ECHO "   OK"
fi
$ECHO
$ECHO "Press return for more"
read something
# DBI
$ECHO "2. Perl DBI Module"
if [ "$dbiversion" = "" ];then
  cat <<EOF
   DBI not installed - WARN
   You can obtain Perl DBI from CPAN (http://www.cpan.org) and build
   it from the source. This is straight forward so long as you have
   a C compiler.

EOF
  if [ \( "$rpmversion" != "" \) -o \( "$mdversion" != "" \) ];then
    cat <<EOF
   You can install the perl-DBI RPM. You should find this on your
   operating system distribution CDs or you should be able to obtain it from
   the FTP site for your operating system distribution. Alternatively,
   search for it on a service like http://www.rpmfind.org.

EOF
  elif [ "$debversion" != "" ];then
    cat <<EOF
   You might be able to find a debian package you can install with apt-get.

EOF
  fi
else
  $ECHO "   You have DBI $dbiversion - OK"
fi
#
$ECHO
$ECHO "Press return for more"
read something
$ECHO "3. Perl DBD::ODBC Module"
if [ "$dbdodbcversion" != "" ];then
  cat <<EOF
   You have DBD::ODBC $dbdodbcversion - OK
EOF
else 				# DBD::ODBC not installed
  cat <<EOF
   DBD::ODBC - Not Installed

   You need to install the DBD::ODBC module as this is the DBD driver that
   sits under DBI and talks to the ODBC driver manager (and ODBC driver).

EOF
  if [ "$LANG" != "" ];then
    $ECHO $LANG | grep 'UTF-8' 1>/dev/null 2>/dev/null
    if [ $? -eq 0 ];then
      cat <<EOF
   LANG=$LANG WARN
   You may find if you attempt to build DBD::ODBC yourself that you will get
   a "Makefile:90: *** missing separator.  Stop." error when trying to
   make DBD::ODBC. To avoid this you need to remove ".UTF-8" from you LANG,
   re-export LANG and rerun "perl Makefile.PL".

EOF
    fi
  fi
  cat <<EOF
   You can find DBD::ODBC on CPAN (http://search.cpan.org). When you
   build it you need to run:

EOF
  if [ "$UNIXODBCWHERE" = "" ];then
    cat <<EOF
   perl Makefile.PL -o path_to_unixODBC

EOF
  else
    cat <<EOF
   perl Makefile.PL -o $UNIXODBCWHERE

EOF
  fi
fi
$ECHO
$ECHO "Press return for more"
read something
if [ "$dbdodbcversion" = "" ];then
  $ECHO "4. unixODBC"
  if [ "$UNIXODBCINSTALLED" = "y" ];then
    cat <<EOF
   unixODBC was installed as part of this installation - OK.

   You can find it in $UNIXODBCWHERE.

EOF
  else
  #
  # we did not install unixODBC
  #
    if [ "$UNIXODBCWHERE" = "" ];then
    #
    # unixODBC was not found
    #
      cat <<EOF
   unixODBC was not found on this machine and not installed by this
   installation.

   You need to install unixODBC to use the Perl DBD::ODBC module.

EOF
      if [ "$rhversion" != "" ];then
      #
      # RedHat
      #
        alternative="y"
        cat <<EOF
   On RedHat you will need to install two RPMs, unixODBC and unixODBC-devel.
   You can find these on your distribution CDs or the RedHat FTP site.

EOF
      elif [ "$mdversion" != "" ];then
	alternative="y"
	cat <<EOF
   On Mandrake you will need to install two RPMs, unixODBC and unixODBC-devel.
   Alternatively, you can rerun this installation and install the included
   unixODBC.

EOF
      else
      #
      # Not RedHat/Mandrake
      #
	alternative="y"
	if [ "$rpmversion" != "" ];then
        #
        # Not RedHat but has rpm
        #
	  cat <<EOF
   As you appear to have rpm ($rpmversion) you might like to look for
   unixODBC rpms which you can find at http://www.unixodbc.org and
   http://sourceforge.net/projects/unixodbc.

EOF
	else
        #
	# Not RedHat, no rpm.
	#
	  cat <<EOF
   All Easysoft distributions come with the unixODBC driver manager. You
   should either rerun this install and select to install the driver
   manager or build unixODBC yourself from source which you can find
   at ftp://ftp.easysoft.com/pub/unixODBC.

EOF
	fi
      fi
      if [ "$alternative" = "y" ];then
	cat <<EOF
   Alternatively, you can rerun this installation and install the included
   unixODBC.

EOF
      fi
    else
    #
    # found unixODBC but not installed by us.
    #
      cat <<EOF
   unixODBC was found on this machine. It is located in
   $UNIXODBCWHERE

EOF
      #
      # If RedHat check for unixODBC-devel.
      #
      if [ \( "$rhversion" != "" \) -o \( "$mdversion" != "" \) ];then
	cat <<EOF
   For RedHat machines unixODBC is split into two packages, unixODBC and
   unixODBC-devel. You appear to have:

   unixODBC - $rpmunixodbc - OK

EOF
	if [ "$rpmunixodbcdev" = "" ];then
	  cat <<EOF
   unixODBC-devel - Not Installed

   You need to install the unixODBC-devel package for the sql*.h files.
   You should find this on your distribution CDs or on your distributions
   FTP site.

EOF
	else
	  cat <<EOF
   unixODBC-devel - $rpmunixodbcdev
EOF
	fi
      elif [ "$rpmversion" != 0 ];then
	cat <<EOF

   Usually unixODBC is split into two RPMs (unixODBC and unixODBC-devel).
   You appear to have rpm ($rpmversion). rpm --query reports:

   unixODBC - $rpmunixodbc
   unixODBC-devel - $rpmunixodbcdev
EOF
	if [ "$rpmunixodbc" = "" ];then
	  cat <<EOF
   As rpm does not report a unixODBC package but you have unixODBC it
   would suggest you are not an rpm based system but have rpm, you have
   deserted rpm or you installed unixODBC yourself. If you installed
   unixODBC from source then you will have the required sql*.h files
   distributed in the unixODBC-devel RPM.

EOF
        fi
      fi
    fi
  fi
  $ECHO
  $ECHO "Press return for more"
  read something
fi
#
if [ \( "$dbdodbcversion" != "" \) -a \( "$dbiversion" != "" \) -a \( "$threadedperl" != "y" \) ];then
  cat <<EOF 
As you seem to have DBI and DBD::ODBC installed we can check whether it
is working by obtaining a list of tables from one of the data sources.

EOF
  dsns=`perl -MDBI -e 'print join(" ", DBI->data_sources('ODBC'));'`
  if [ "$dsns" = "" ];then
    cat <<EOF
but you do not appear to have any DSNs recognised by DBD::ODBC.

EOF
  else
    if [ "$TESTEXISTS" /usr/local/easysoft/etc/es.ld.so.conf ];then
      ldpaths=`cat  /usr/local/easysoft/etc/es.ld.so.conf `
      ldpaths=`echo  $ldpaths | sed 's/ /:/g'`
      LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$ldpaths
      export LD_LIBRARY_PATH
      DYLD_LIBRARY_PATH=$DYLD_LIBRARY_PATH:$ldpaths
      export DYLD_LIBRARY_PATH
      SHLIB_PATH=$SHLIB_PATH:$ldpaths
      export SHLIB_PATH
      LIBPATH=$LIBPATH:$ldpaths
      export LIBPATH
    fi
    trya="y"
    while [ "$trya" = "y" ]
    do
      count=0
      for i in $dsns
      do
        count=`expr $count + 1`
        $ECHO $count - $i
      done
      answer="hoho"
      while [ "$answer" != "" ]
      do
        $ECHO "Choose a DSN (enter number) (q=abort Perl tests) : \c"
        read answer
        if [ "$answer" = "q" ];then
          $ECHO "DBD::ODBC test aborted at user request"
          exit 0
        fi
        if [ "$answer" != "" ];then
          pick=""
          count=0
          for i in $dsns
          do
            count=`expr $count + 1`
            if [ "$count" = "$answer" ];then
              pick="$i"
              answer=""
              break
            fi
          done
        else
          answer="hoho"
        fi
      done
      $ECHO "You picked $pick"
      DBI_DSN="$pick"
      export DBI_DSN
      $ECHO "Database username? : \c"
      read dbuser
      $ECHO "Database password? : \c"
      $ECHOOFF
      read dbpass
      $ECHOON
      $ECHO
      DBI_USER="$dbuser"
      export DBI_USER
      DBI_PASS="$dbpass"
      export DBI_PASS
      perl -MDBI -e 'my $d=DBI->connect() || exit 1;my $s=$d->table_info(undef,undef,undef,"TABLE") || exit 2;while (my @row = $s->fetchrow_array) {print join(",", @row), "\n";}'
      result=$?
      if [ $result -eq 0 ];then
        $ECHO "Perl Tests OK"
	trya="n"
      else
	$ECHO
        if [ $result -eq 1 ];then
	  $ECHO "It looks as though the connect failed. See error above."
        elif [ $result -eq 2 ];then
	  $ECHO "It looks as though the connect was fine but we failed to get tables"
        fi
	answer="hoho"
	while [ \( "$answer" != "y" \) -a \( "$answer" != "n" \) ]
        do
	  $ECHO "Try again with different DSN/Username/Password? (y/n) [n] : \c"
          read answer
          if [ "$answer" = "" ];then
	    answer="n"
          fi
          if [ "$answer" != "y" ];then
	    trya="n"
          fi
        done
      fi
    done
  fi
fi
