#!/bin/bash

##############################################################################################################
##
##  Installs server and client package on a node
##  It will call the server/insall.sh and client/install.sh
##
##  ./install.sh                    (Install to $HOME directory. You should install it on each host in your cluster)
##  ./install.sh -d <INSTALL_DIR>   (Install to INSTALL_DIR. You should install it on each host in your cluster)
##
##############################################################################################################

function usage
{
    echo "Usage:  ./install.sh -d <JAGUAR_HOME>"
    echo "Usage:  ./install.sh"
    
}

#PS4='Line ${LINENO}: '
#set -x

if [[ ! -v JAGUAR_HOME ]]; then
    JAGUAR_HOME=$HOME/jaguar
fi

## Traverse all options given for this shell script
i=0
for arg in "$@"
do
    i=`expr $i + 1`
    case $arg in
        "-d")
            i_next=`expr $i + 1`
            if [ $i_next -le $# ]; then
                jaguarhome=${!i_next}
                JAGUAR_HOME=$jaguarhome
            else
                usage
                exit 1
            fi
            ;;
        "-help")
            usage
            ;;
        "-h")
            usage
            ;;
        *)
            i_previous=`expr $i - 1`
            if [ ${!i_previous} != "-r" -a ${!i_previous} != "-d" ]; then
                usage
                exit 1
            fi
            ;;
    esac
done

echo $JAGUAR_HOME > $HOME/.jaguarhome

dname=`dirname $0`
if [[ "x$dname" = "x." ]] ; then
    dname=`pwd -P`
fi


### copy info data
/bin/cp -f RELEASE_NOTES LICENSE readme *.txt $JAGUAR_HOME/
/bin/mkdir -p $JAGUAR_HOME/COPYING
/bin/cp COPYING/* $JAGUAR_HOME/COPYING/


############### install server
echo "cd $dname/server ..."
cd $dname/server
if [[ "x$JAGUAR_HOME" != "x$HOME" ]]; then
    if ! mkdir -p $JAGUAR_HOME; then
        hn=`hostname`
        echo "$h Unable to mkdir $JAGUAR_HOME  exit"
        exit 1
    fi
    echo "./install.sh -d $JAGUAR_HOME"
    ./install.sh -d $JAGUAR_HOME 
else
    echo "Invalid $JAGUAR_HOME to install, exit"
    exit 1
fi


############### install client
echo "cd $dname/client ... "
cd $dname/client
echo "./install.sh -d $JAGUAR_HOME"
./install.sh -d $JAGUAR_HOME

echo "JaguarDB installation complete"

