#!/bin/ksh
########################################################
#    file                 : DAViewer
#    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  ignore missing libXevie on solaris
#  marian.popeanga   18-feb-2011  cd in lib folder, so that CrashReport can start
#  marian.popeanga   11-feb-2011  add xhost +local:root to allow CrashReporter to start
#  marian.popeanga   17-jun-2010  fix checking missing libs on solaris
#  marian.popeanga   22-mar-2010  complete model path if empty
#  marian.popeanga   26-oct-2006  Start logging history
#
#

SETUP_FILE="$HOME/.DataArchitect/setup.cfg"
version="4.3"
usage()
{
     echo "DAViewer "$version" usage: "
     echo ""
     echo "DAViewer [debug] [model] "
     echo "    where:"
     echo "   -debug - debug flag in order to get the stack trace"
     echo "   -model - model file you want to load"

     exit 1
}

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
}

if [ $# -gt 2 ]; then
     usage
fi

debug="N"
case "$1" in
'debug')
    debug="Y"
    echo "Debugging ... "
    parameter=$2
  ;;
*)
    parameter=$1
esac

if [ ! -z $parameter ]; then
   modelFolder=`dirname $parameter`
   if [ $modelFolder == "." ]; then
        parameter=`pwd`/$parameter
   fi
fi

if [ ! -f $SETUP_FILE ] ; then
     echo "You need to run the setup before using the product."
     exit 1
fi

driver=`cat $SETUP_FILE|grep driver|awk -F":" '{print $2}'`
config=`cat $SETUP_FILE|grep config|awk -F":" '{print $2}'`

if [ $driver == "unixODBC" ] ; then
    if [ $config == "unicode" ] ; then
         FILE="./unixodbc_u/DAViewer"
         LIBS="unixodbc_u"
    else
         FILE="./unixodbc_a/DAViewer"
         LIBS="unixodbc_a"
    fi
else
    if [ $config == "unicode" ] ; then
         FILE="./iodbc_u/DAViewer"
         LIBS="iodbc_u"
    else
         FILE="./iodbc_a/DAViewer"
         LIBS="iodbc_a"
    fi
fi

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 "DAViewer cannot start without thekompany-support package !"
         echo "Please install: "
         echo "       thekompany-support 1.6 !"
         echo ""
         echo "You can ask for support at www.thekompany.com"
         echo "or on DataArchitect mail list da@thekompany.com"
         exit 1
    fi

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

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

    export SHLIB_PATH=${LIB}:${LIBS}:$SHLIB_PATH
    xhost +local:root
  ;;		
*) # 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 "DAViewer "$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
fi

check_libs "odbc"
odbc=$?
if [ $odbc -gt 0 ] ; then
     echo "unixODBC libraries are not installed/configured on this system !"
     echo "See www.unixodbc.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

if [ ${debug} = "Y" ]; then
     gdb $FILE  <<CMD
     run $parameter
     bt
     quit
CMD
else
     $FILE $parameter
fi

