#!/bin/bash

###############################################################################################################
##
##  Install script for client 
##
##  ./install.sh     (Install into $HOME direcotyr. You should execute this script on each host in your cluster)
##  ./install.sh     -d <INSTALL_DIR>     (You should execute this script on each host in your cluster)
##
###############################################################################################################

### users can change this to a different directory
export  JAGUAR_HOME=$HOME/jaguar

if [[ "x$1" = "x-d" ]]; then
    JAGUAR_HOME=$2
    if [[ "x$JAGUAR_HOME" = "x" ]]; then
        echo "Usage:  ./install.sh -d <JAGUAR_HOME>"
        echo "Usage:  ./install.sh"
        exit 1
    fi

    if [[ -e "$JAGUAR_HOME" ]]; then
        echo "OK, install jaguar files to $JAGUAR_HOME ..."
    else
        echo "$JAGUAR_HOME does not exist, exit"
        exit 1
    fi
fi

echo "Installing jaguar client in $JAGUAR_HOME/ ..."
pd=`pwd -P`

/bin/mkdir -p $JAGUAR_HOME/bin
/bin/mkdir -p $JAGUAR_HOME/conf
/bin/mkdir -p $JAGUAR_HOME/data
/bin/mkdir -p $JAGUAR_HOME/pdata
/bin/mkdir -p $JAGUAR_HOME/ndata
/bin/mkdir -p $JAGUAR_HOME/log
/bin/mkdir -p $JAGUAR_HOME/doc
/bin/mkdir -p $JAGUAR_HOME/include
/bin/mkdir -p $JAGUAR_HOME/lib
/bin/mkdir -p $JAGUAR_HOME/tmp
/bin/mkdir -p $JAGUAR_HOME/backup

/bin/cp -f  jag rlwrap jagimport* jagexport* $JAGUAR_HOME/bin
/bin/cp -f include/*.h $JAGUAR_HOME/include
/bin/cp -f lib/*.so* lib/*.jar lib/jaguarnode.node $JAGUAR_HOME/lib
/bin/cp -f *.ini *.conf $JAGUAR_HOME/conf
/bin/cp -f  JaguarJDBCTest.java example.* README.* $JAGUAR_HOME/doc
echo "Uncompress golang.tar.gz to  $JAGUAR_HOME/doc ..."
/bin/ls -l golang.tar.gz
tar -C $JAGUAR_HOME/doc  --warning=no-timestamp -zxf golang.tar.gz

##### make symlinks of shared library
cd $JAGUAR_HOME/lib
[[ -L libssl.so ]] && unlink libssl.so
[[ -f libssl.so ]] && unlink libssl.so
ln -s libssl.so.3 libssl.so

[[ -L libcrypto.so ]] && unlink libcrypto.so
[[ -f libcrypto.so ]] && unlink libcrypto.so
ln -s libcrypto.so.3 libcrypto.so
cd $pd

uo=`uname -o`
if [[ "x$uo" = "xGNU/Linux" ]]; then
    glibcver=`./glibcversion`
    if [[ "x$glibcver" = "x2.12" ]]; then
	    /bin/cp -f lib/2.12/glibc2.12-libJaguarClient.so $JAGUAR_HOME/lib/libJaguarClient.so
	    #/bin/cp -f lib/2.12/jaguarphp.so $JAGUAR_HOME/lib/
    fi
fi

### python so lib
if type python >/dev/null 2>&1
then
	pv=`python --version 2>&1|awk '{print $2}'|cut -d. -f1`
	if (( pv==2 )); then
		echo "Python2 is not supported. Python3 is supported."
		#if [[ -f "lib/jaguarpy2.so" ]]; then
		#	/bin/cp -f lib/jaguarpy2.so $JAGUAR_HOME/lib/jaguarpy.so
		#fi
	fi

    cd $JAGUAR_HOME/lib

	pv2=`python --version 2>&1|awk '{print $2}'|cut -d. -f2`
    python_ver="${pv}.${pv2}"

    [[ -L jaguarpy.so ]] && unlink jaguarpy.so
    [[ -f jaguarpy.so ]] && unlink jaguarpy.so

    if [[ $python_ver = "3.8" ]]; then
        ln -s jaguarpy-python3.8.18.so jaguarpy.so
    elif [[ $python_ver = "3.9" ]]; then
        ln -s jaguarpy-python3.9.23.so jaguarpy.so
    elif [[ $python_ver = "3.10" ]]; then
        ln -s jaguarpy-python3.10.12.so jaguarpy.so
    elif [[ $python_ver = "3.11" ]]; then
        ln -s jaguarpy-python3.11.13.so jaguarpy.so
    elif [[ $python_ver = "3.12" ]]; then
        ln -s jaguarpy-python3.12.3.so jaguarpy.so
    elif [[ $python_ver = "3.13" ]]; then
        ln -s jaguarpy-python3.13.7.so jaguarpy.so
    elif [[ $python_ver = "3.14" ]]; then
        ln -s jaguarpy-python3.14.5.so jaguarpy.so
    else
        echo "Your Python version $python_ver is not supported in JaguarDB client library"
    fi

    cd $pd

fi

echo "Successfully installed jaguar client in $JAGUAR_HOME"

