#!/bin/sh
#  Licensed Materials - Property of IBM
#
#  5724-A60
#  5765-F36
#  5765-F32
#  (C) Copyright IBM Corporation 2001, 2002, 2003, 2004
#
#  ALL rights reserved.
#  US Government Users Restricted Rights -
#  Use, duplication or disclosure restricted by GSA ADP
#  Schedule Contract with IBM Corporation.
#


# path to DB2 IM Scoring
IMINERSCPATH=/opt/IMinerX/V8.2

# are you the instance owner?
if [ "$DB2INSTANCE" = "" ] || [ ! -d $HOME/sqllib/bin ]
then
        echo "ERROR: Your are either not an instance owner or have not executed";
        echo "\$HOME/sqllib/db2profile. Make sure that you are the instance owner";
  echo "and the DB2 environment is initialized.";
        exit 1;
fi

# find out if instance is 32 or 64 bits
line=`ls -l $HOME/sqllib/bin`;
for word in $line
do
  db2path=$word/..;
done

line=`$db2path/instance/db2ilist -a | /bin/grep $DB2INSTANCE`;
for word in $line
do
  bits=$somepath;
  somepath=$word;
done

# what architecture are we running?
arch=`uname -m`

# set path according to architecture and width of database instance
# ppc64 and x86_64 are pure 64-bit and do not have ./lib64 directory
if [ "$arch" = "ppc64" ] || [ "$arch" = "x86_64" ]
then
  if [ "$bits" = "64" ]
  then
    IMINERLIBPATH=$IMINERSCPATH/lib;
  else
    echo "ERROR: Instance $DB2INSTANCE is a 32 bit instance, but there is a"
    echo "64 bit version of the product installed! Use a 64 bit instance"
    echo "and try again."
    exit 3;
  fi
# other 64-bit versions have ./lib64 directory
else
  if [ "$bits" = "64" ]
  then
    IMINERLIBPATH=$IMINERSCPATH/lib64;
    if [ ! -d $IMINERSCPATH/lib64 ]
    then
      echo "ERROR: Instance $DB2INSTANCE is a 64 bit instance, but there is no"
      echo "64 bit version of the product installed! Install the 64 bit components"
      echo "and try again."
      exit 3;
    fi
  else
    IMINERLIBPATH=$IMINERSCPATH/lib;
  fi
fi

# clean up leftovers from previous installations
if [ $HOME/sqllib/function/idmrec ]
then
  rm -f $HOME/sqllib/function/idmrec;
fi
if [ $HOME/sqllib/function/idmdall ]
then
  rm -f $HOME/sqllib/function/idmdall;
fi
if [ $HOME/sqllib/function/idmreg ]
then
  rm -f $HOME/sqllib/function/idmreg;
fi
if [ $HOME/sqllib/function/idmclu ]
then
  rm -f $HOME/sqllib/function/idmclu;
fi
if [ $HOME/sqllib/function/idmclf ]
then
  rm -f $HOME/sqllib/function/idmclf;
fi
if [ $HOME/sqllib/function/idmrul ]
then
  rm -f $HOME/sqllib/function/idmrul;
fi
if [ $HOME/sqllib/function/idmx ]
then
  rm -f $HOME/sqllib/function/idmx;
fi
if [ $HOME/sqllib/function/idmxrun ]
then
  rm -f $HOME/sqllib/function/idmxrun;
fi
if [ $HOME/sqllib/function/IDMEasyMiningProcedures.jar ]
then
  rm -f $HOME/sqllib/function/IDMEasyMiningProcedures.jar;
fi


# install functions
linkset="0";
if [ $IMINERLIBPATH/idmrec ]
then
  ln -s $IMINERLIBPATH/idmrec $HOME/sqllib/function;
  linkset="1";
fi
if [ $IMINERLIBPATH/idmdall ]
then
  ln -s $IMINERLIBPATH/idmdall $HOME/sqllib/function;
  linkset="1";
fi
if [ $IMINERLIBPATH/idmx ]
then
  ln -s $IMINERLIBPATH/idmx $HOME/sqllib/function;
  linkset="1";
fi
if [ $IMINERLIBPATH/idmxrun ]
then
  ln -s $IMINERLIBPATH/idmxrun $HOME/sqllib/function;
  linkset="1";
fi
if [ $IMINERSCPATH/java/IDMEasyMiningProcedures.jar ]
then
  ln -s $IMINERSCPATH/java/IDMEasyMiningProcedures.jar $HOME/sqllib/function;
  linkset="1";
fi


# check for errors
if [ "$linkset" = "0" ]
then
  echo "ERROR: Could not create any links. Please check your installation.";
  exit 2;
fi


# add executables to the PATH
if [ -e $HOME/sqllib/userprofile ]
then
   /bin/cat $HOME/sqllib/userprofile | /bin/grep -v IMinerX > $HOME/sqllib/userprofile
fi
echo "# automatically appended by IMinerX" >> $HOME/sqllib/userprofile
echo "export PATH=\$PATH:$IMINERSCPATH/bin" >> $HOME/sqllib/userprofile


# verify system configuration for easy mining procedures
if [ "$bits" = "64" ]
then
  BIN_DIR="/lib64";
else
  BIN_DIR="/usr/lib";
fi

if [ $IMINERSCPATH/java/IDMEasyMiningProcedures.jar ]
then
   JDKPATH=`$HOME/sqllib/bin/db2jdkp`
   # x86_64 uses j9vm
   if [ "$arch" = "x86_64" ]
   then
     LINK1=`/bin/ls -l $BIN_DIR/libj9dyn22.so 2>/dev/null | /bin/sed 's/[^/]*\//\//' | /bin/sed 's/[^>]* -> //'`
     LINK2=`/bin/ls -l $BIN_DIR/libj9gc22.so 2>/dev/null | /bin/sed 's/[^/]*\//\//' | /bin/sed 's/[^>]* -> //'`
     LINK3=`/bin/ls -l $BIN_DIR/libj9hook22.so 2>/dev/null | /bin/sed 's/[^/]*\//\//' | /bin/sed 's/[^>]* -> //'`
     LINK4=`/bin/ls -l $BIN_DIR/libj9thr22.so 2>/dev/null | /bin/sed 's/[^/]*\//\//' | /bin/sed 's/[^>]* -> //'`
     LINK5=`/bin/ls -l $BIN_DIR/libj9zlib22.so 2>/dev/null | /bin/sed 's/[^/]*\//\//' | /bin/sed 's/[^>]* -> //'`
     LINK6=`/bin/ls -l $BIN_DIR/libjava.so 2>/dev/null | /bin/sed 's/[^/]*\//\//' | /bin/sed 's/[^>]* -> //'`
     LINK7=`/bin/ls -l $BIN_DIR/libjclscar_22.so 2>/dev/null | /bin/sed 's/[^/]*\//\//' | /bin/sed 's/[^>]* -> //'`
     LINK8=`/bin/ls -l $BIN_DIR/libjsig.so 2>/dev/null | /bin/sed 's/[^/]*\//\//' | /bin/sed 's/[^>]* -> //'`
   else
     LINK1=`/bin/ls -l $BIN_DIR/libjava.so 2>/dev/null | /bin/sed 's/[^/]*\//\//' | /bin/sed 's/[^>]* -> //'`
     LINK2=`/bin/ls -l $BIN_DIR/libjvm.so 2>/dev/null | /bin/sed 's/[^/]*\//\//' | /bin/sed 's/[^>]* -> //'`
     LINK3=`/bin/ls -l $BIN_DIR/libhpi.so 2>/dev/null | /bin/sed 's/[^/]*\//\//' | /bin/sed 's/[^>]* -> //'`
     LINK4=`/bin/ls -l $BIN_DIR/libjsig.so 2>/dev/null | /bin/sed 's/[^/]*\//\//' | /bin/sed 's/[^>]* -> //'`
     LINK5=`/bin/ls -l $BIN_DIR/libdbgmalloc.so 2>/dev/null | /bin/sed 's/[^/]*\//\//' | /bin/sed 's/[^>]* -> //'`
   fi
   JDK1=`echo $LINK1 | /bin/sed 's/\/jre\/.*//'`
   JDK2=`echo $LINK2 | /bin/sed 's/\/jre\/.*//'`
   JDK3=`echo $LINK3 | /bin/sed 's/\/jre\/.*//'`
   JDK4=`echo $LINK4 | /bin/sed 's/\/jre\/.*//'`
   JDK5=`echo $LINK5 | /bin/sed 's/\/jre\/.*//'`
   JDK6=`echo $LINK6 | /bin/sed 's/\/jre\/.*//'`
   JDK7=`echo $LINK7 | /bin/sed 's/\/jre\/.*//'`
   JDK8=`echo $LINK8 | /bin/sed 's/\/jre\/.*//'`
   if [ "$JDKPATH" = "" ]
   then
      echo "WARNING: The configuration parameter JDK_PATH is not set in the database"
      echo "manager. It has to be set to have the easy mining procedures working."
      if [ "$JDK1" != "" ] && [ $JDK1 = $JDK2 ] && [ $JDK2 = $JDK3 ] &&
         [ "$JDK1" != "$BIN_DIR" ]
      then
         echo "Run the following command line: "
         echo "     db2 update dbm cfg using JDK_PATH $JDK1"
      else
         echo "Run the following command line: "
         echo "     db2 update dbm cfg using JDK_PATH <jdk_path>"
         echo "Where <jdk_path> is the path to a valid Java Kit".
         echo
         echo "Do not forget to add also the corresponding java links in $BIN_DIR"
         echo "by running the following commands as root:"

         case "$arch" in
         x86_64)
           echo "ln -s <jdk_path>/jre/bin/libj9dyn22.so $BIN_DIR/libj9dyn22.so"
           echo "ln -s <jdk_path>/jre/bin/libj9gc22.so $BIN_DIR/libj9gc22.so"
           echo "ln -s <jdk_path>/jre/bin/libj9hook22.so $BIN_DIR/libj9hook22.so"
           echo "ln -s <jdk_path>/jre/bin/libj9thr22.so $BIN_DIR/libj9thr22.so"
           echo "ln -s <jdk_path>/jre/bin/libj9zlib22.so $BIN_DIR/libj9zlib22.so"
           echo "ln -s <jdk_path>/jre/bin/libjava.so $BIN_DIR/libjava.so"
           echo "ln -s <jdk_path>/jre/bin/libjclscar_22.so $BIN_DIR/libjclscar_22.so"
           echo "ln -s <jdk_path>/jre/bin/libjsig.so $BIN_DIR/libjsig.so"
           ;;
         ppc64)
           echo "ln -s <jdk_path>/jre/bin/libjava.so $BIN_DIR/libjava.so"
           echo "ln -s <jdk_path>/jre/bin/classic/libjvm.so $BIN_DIR/libjvm.so"
           echo "ln -s <jdk_path>/jre/bin/libhpi.so $BIN_DIR/libhpi.so"
           echo "ln -s <jdk_path>/jre/bin/libjsig.so $BIN_DIR/libjsig.so"
           echo "ln -s <jdk_path>/jre/bin/libdbgmalloc.so $BIN_DIR/libdbgmalloc.so"
           ;;
         *)
           echo "ln -s <jdk_path>/jre/bin/libjava.so $BIN_DIR/libjava.so"
           echo "ln -s <jdk_path>/jre/bin/classic/libjvm.so $BIN_DIR/libjvm.so"
           echo "ln -s <jdk_path>/jre/bin/libhpi.so $BIN_DIR/libhpi.so"
           ;;
         esac
      fi
   else
      if [ "$JDK1" = "" ] || [ "$JDK2" = "" ] || [ "$JDK3" = "" ] ||
         [ $JDK1 != $JDK2 ] || [ $JDK2 != $JDK3 ]
      then
         echo "WARNING; one of the following files is missing or is a link that does"
         echo "not correspond to the configuration parameter JDK_PATH, actually set"
         echo "to $JDKPATH:"

         case "$arch" in
         x86_64)
           echo "$BIN_DIR/libj9dyn22.so -> $LINK1"
           echo "$BIN_DIR/libj9gc22.so -> $LINK2"
           echo "$BIN_DIR/libj9hook22.so -> $LINK3"
           echo "$BIN_DIR/libj9thr22.so -> $LINK4"
           echo "$BIN_DIR/libj9zlib22.so -> $LINK5"
           echo "$BIN_DIR/libjava.so -> $LINK6"
           echo "$BIN_DIR/libjclscar_22.so -> $LINK7"
           echo "$BIN_DIR/libjsig.so -> $LINK8"
           echo
           echo "You can solve this problem by running the following commands as root:"
           echo "ln -s $JDKPATH/jre/bin/libj9dyn22.so $BIN_DIR/libj9dyn22.so"
           echo "ln -s $JDKPATH/jre/bin/libj9gc22.so $BIN_DIR/libj9gc22.so"
           echo "ln -s $JDKPATH/jre/bin/libj9hook22.so $BIN_DIR/libj9hook22.so"
           echo "ln -s $JDKPATH/jre/bin/libj9thr22.so $BIN_DIR/libj9thr22.so"
           echo "ln -s $JDKPATH/jre/bin/libj9zlib22.so $BIN_DIR/libj9zlib22.so"
           echo "ln -s $JDKPATH/jre/bin/libjava.so $BIN_DIR/libjava.so"
           echo "ln -s $JDKPATH/jre/bin/libjclscar_22.so $BIN_DIR/libjclscar_22.so"
           echo "ln -s $JDKPATH/jre/bin/libjsig.so $BIN_DIR/libjsig.so"
           ;;
         ppc64)
           echo "$BIN_DIR/libjava.so -> $LINK1"
           echo "$BIN_DIR/libjvm.so -> $LINK2"
           echo "$BIN_DIR/libhpi.so -> $LINK3"
           echo "$BIN_DIR/libjsig.so -> $LINK4"
           echo "$BIN_DIR/libdbgmalloc.so -> $LINK5"
           echo
           echo "You can solve this problem by running the following commands as root:"
           echo "ln -s $JDKPATH/jre/bin/libjava.so $BIN_DIR/libjava.so"
           echo "ln -s $JDKPATH/jre/bin/classic/libjvm.so $BIN_DIR/libjvm.so"
           echo "ln -s $JDKPATH/jre/bin/libhpi.so $BIN_DIR/libhpi.so"
           echo "ln -s $JDKPATH/jre/bin/libjsig.so $BIN_DIR/libjsig.so"
           echo "ln -s $JDKPATH/jre/bin/libdbgmalloc.so $BIN_DIR/libdbgmalloc.so"
           ;;
         *)
           echo "$BIN_DIR/libjava.so -> $LINK1"
           echo "$BIN_DIR/libjvm.so -> $LINK2"
           echo "$BIN_DIR/libhpi.so -> $LINK3"
           echo
           echo "You can solve this problem by running the following commands as root:"
           echo "ln -s $JDKPATH/jre/bin/libjava.so $BIN_DIR/libjava.so"
           echo "ln -s $JDKPATH/jre/bin/classic/libjvm.so $BIN_DIR/libjvm.so"
           echo "ln -s $JDKPATH/jre/bin/libhpi.so $BIN_DIR/libhpi.so"
           ;;
         esac
      fi
   fi
fi

exit 0;
