#!/bin/ksh
########################################################
#    file                 : setup
#    copyright            : (C) 2012 by ByteAnywhere
#    author               : Marian Popeanga
#    email                : marian@byteanywhere.com
# ########################################################

# ########################################################
#
#     This program is licensed under the terms contained in the file
#     LICENSE which is contained with the source code distribution.
#
# #########################################################
#
#  DESCRIPTION
#
#
#  Author            Date         Note
#
#  marian.popeanga   14-jul-2012  Creation
#
#


version="4.3"

check_libs()
{
     if [ -z $1 ]; then
         lib_to_check=" "  # check all missing libs
     else
         lib_to_check=$1   # check only the matching lib
     fi

     case "$platform" in
     "Linux")   # Running on linux platform
         libs=`ldd ${FILE} |grep "not found"|grep "$lib_to_check"|wc -l`
         ;;
     "Solaris") # Running on Solaris platform
         # Don't count libXevie, since this lib is not important.
         libs=`ldd ${FILE} |grep "file not found"|grep "$lib_to_check"|grep -v libXevie|wc -l`
         ;;
     "Hpux")    # Runnung on HP-UX platform
         libs=`ldd ${FILE}  |grep "find path for shared library"|grep "$lib_to_check"|wc -l`
         ;;
     *) # Unsupported platform
         echo "Unknown platform"
         exit 1
     esac

     return $libs
}

FILE="./setup"
DIR=`dirname $0`
LIB="."
platform="Unknown"
cd "$DIR/lib"

case `uname` in
'Linux') # Running on linux platform 
    platform="Linux"

    if [ ! -x /usr/local/thekompany ] ; then
         echo "setup "$version" cannot start without thekompany-support package !"
         echo "Please install: "
         echo "       thekompany-support 1.6 !"
         echo ""
         echo "You can ask for support at:"
         echo "    www.thekompany.com, www.byteanywhere.com"
         echo "or on DataArchitect mail list da@thekompany.com"
         exit 1
    fi

    export LD_LIBRARY_PATH=${LIB}:${LIBS}:$LD_LIBRARY_PATH
  ;;
'SunOS') # Running on Solaris platform
    platform="Solaris"

    export LD_LIBRARY_PATH=${LIB}:${LIBS}:$LD_LIBRARY_PATH
  ;;
'HP-UX') # Runnung on HP-UX platform
    platform="Hpux"

    export SHLIB_PATH=${LIB}:${LIBS}:$SHLIB_PATH
  ;;
*) # Unsupported platform
    echo "Unknown platform"
    exit 1
esac

# Check if we can query application libraries
# If there is an error, application won't start
# On linux this means /usr/local/thekompany is empty 
# on other platforms there is a critical OS error.
libraries=`ldd ${FILE} |wc -l`
if [ $libraries -eq 0 ] ; then
     echo "setup "$version" cannot start"
     if [ $platform = "Linux" ] ; then
          echo "       without thekompany-support package !"
          echo "Please install: "
          echo "       thekompany-support 1.6 !"
     else
          echo "       Critical OS error while executing OS ldd command"  
     fi
     echo ""
     echo "You can ask for support at:"
     echo "    www.thekompany.com, www.byteanywhere.com"
     echo "or on DataArchitect mail list da@thekompany.com"
     exit 1
fi

check_libs
libraries=$?
if [ $libraries -gt 0 ] ; then
     echo "Some libraries are missing from DataArchitect install directory !"
     echo "Reinstall the product. "
     exit 1
fi
      
$FILE "$@"


