#!/bin/bash

##############################################################################################################
##  Copyright JaguarDB
##
##  Server Installation Script 
##
##  ./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 localIPs()
{
    g_ips=`ip a|grep inet|grep brd|awk '{print $2}'|cut -d '/' -f 1`
}

function usage
{
    echo "./install.sh (Install to $HOME directory)"
    echo "./install.sh -d <INSTALL_DIR>"
}

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


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


## Traverse all options given for this shell script
randomstring=""
jaguarhome=""
i=0
for arg in "$@"
do
    i=`expr $i + 1`
    case $arg in
        "-d")
            i_next=`expr $i + 1`
            if [ $i_next -le $# ]; then
                export JAGUAR_HOME=${!i_next}
            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 "Installing jaguar server in $JAGUAR_HOME/jaguar ..."
echo $JAGUAR_HOME > $HOME/.jaguarhome

/bin/mkdir -p $JAGUAR_HOME/bin
/bin/mkdir -p $JAGUAR_HOME/conf/data
/bin/mkdir -p $JAGUAR_HOME/conf/backup
/bin/mkdir -p $JAGUAR_HOME/data
/bin/mkdir -p $JAGUAR_HOME/pdata
/bin/mkdir -p $JAGUAR_HOME/ndata
/bin/mkdir -p $JAGUAR_HOME/user
/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/backup


######## copy files  ##############
files=`/bin/ls jaguar* jql.bin *.sh |uniq`
/bin/cp -f $files $JAGUAR_HOME/bin
/bin/mkdir -p $JAGUAR_HOME/bin/tools
/bin/cp -f tools/* $JAGUAR_HOME/bin/tools/
/bin/cp -f lib/* $JAGUAR_HOME/lib/
/bin/rm -f $JAGUAR_HOME/bin/install.sh

if [[ ! -f "$JAGUAR_HOME/conf/server.conf" ]]; then
    echo " $JAGUAR_HOME/conf/server.conf not found, use a supplied copy of server.conf"
    /bin/cp -f server.conf $JAGUAR_HOME/conf/
else
    /bin/cp -f server.conf $JAGUAR_HOME/conf/server.conf.new
    echo "Your exisiting configuration file $JAGUAR_HOME/conf/server.conf is kept the same."
    echo "The new configuration file server.conf is saved as $JAGUAR_HOME/conf/server.conf.new"
fi

/bin/cp -f ../version.txt $JAGUAR_HOME/conf
if [[ -f "../serialnumber.txt" ]]; then
    /bin/cp -f ../serialnumber.txt $JAGUAR_HOME/conf
fi
/bin/cp -f syncpass.txt $JAGUAR_HOME/conf
/bin/cp -f README.* $JAGUAR_HOME/doc

touch $JAGUAR_HOME/conf/license.txt
chmod 700 $JAGUAR_HOME
chmod 700 $JAGUAR_HOME/bin/*
chmod 700 $JAGUAR_HOME/bin/tools/*
chmod 700 $JAGUAR_HOME/data
chmod 700 $JAGUAR_HOME/conf
chmod 700 $JAGUAR_HOME/log
chmod 700 $JAGUAR_HOME/user

### public and private keys
if [[ ! -f "$JAGUAR_HOME/conf/private_openssl.pem" ]]; then
    $JAGUAR_HOME/bin/tools/createKeyPair.bin -publickey $JAGUAR_HOME/conf/public_openssl.pem -privatekey $JAGUAR_HOME/conf/private_openssl.pem
    chmod 600 $JAGUAR_HOME/conf/private_openssl.pem
    /bin/ls -l $JAGUAR_HOME/conf/p*.pem
fi

echo "Successfully installed jaguar server in $JAGUAR_HOME/ "
echo "Please install the same package on all the servers in your cluster."
echo "Also, conf/server.conf and conf/cluster.conf must be the same on all servers."
echo "File $JAGUAR_HOME/conf/cluster.conf should have the valid IP address of the host."

