#!/bin/bash

##########################################################################################
##
##  Startup script for jaguar server
##
##  ./jaguarstart
##
##   Below also starts fwww http gateway server
##  ./jaguarstart http
##
##########################################################################################

### users can change this to a different directory 
. `dirname $0`/jaguarenv

http=$1  ### "http" to bring up fwww servers together

hname=`uname -n`
hip=`hostname -I|cut -d' ' -f1`
hostinfo="$hname $hip"

wishopenfile=1000000
filehardlimit=`ulimit -Hn`
if ((filehardlimit < wishopenfile )); then
    ulimit -n $filehardlimit
    if [[ ! -n "$DOCKER_CONTAINER" ]]; then
        echo "The current open file hard limit on this host ($hostinfo) is set to $filehardlimit, which is lower than the recommended value of $wishopenfile."
        echo "JaguarDB will temporarily use $filehardlimit as the open file limit. It is recommended that you update /etc/security/limits.conf " 
        echo "with \"* soft nofile $wishopenfile\" and \"* hard nofile $wishopenfile\", then log out, log back in, and restart the JaguarDB process."
    fi
else
    ulimit -n $wishopenfile
fi

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

if [[ "x$pid" != "x" ]]; then
	echo "jaguardb server is already RUNNING on $hostinfo"
	exit 1
fi


if [[ ! -f "$JAGUAR_HOME/conf/cluster.conf" ]]; then
    echo "127.0.0.1" > "$JAGUAR_HOME/conf/cluster.conf"
fi

cd $JAGUAR_HOME/log
logfile=jaguar.log

if [[ -f $logfile ]]; then
    tm=`date +'%Y_%m_%d_%H_%M'`
    /bin/mv -f $logfile  ${logfile}.${tm}
    gzip -f ${logfile}.${tm} 
    /bin/mv -f ${logfile}.${tm}.gz  $JAGUAR_HOME/log/history/
fi


un=`uname -o`
export FROM_SHELL=yes

## if start fwww/http server
((start_http=0))
if [[ "x$http" = "xhttp" ]]; then
    if [[ -f "$HOME/fwww/bin_dir/start_all_servers.sh" ]]; then
        ((start_http=1))
    fi
fi

if [[ -n "$DOCKER_CONTAINER" ]]; then
    echo "Starting jaguardb in docker container"

    if ((start_http==1)); then
        chown -R nobody.nogroup $JAGUAR_HOME
	    exec $JAGUAR_HOME/bin/jaguar.bin > $logfile 2>&1 &
        sleep 8
        chown -R nobody.nogroup $JAGUAR_HOME/conf

        cd $HOME/fwww/bin_dir
        chown -R nobody.nogroup $HOME
        echo "Starting fwww/http server in docker container"
        su nobody -p -g nogroup -s /bin/bash -c "$HOME/fwww/bin_dir/start_all_servers.sh"
        tail -f /dev/null
    else
	    exec $JAGUAR_HOME/bin/jaguar.bin > $logfile 2>&1
    fi
    
else
    ### start fwww/http server if requied
    dt=`date`
    echo "Starting jaguardb server on $hostinfo $dt ..."
    if ((start_http==1)); then
        
	    $JAGUAR_HOME/bin/jaguar.bin > $logfile 2>&1 &
        sleep 60 

        echo "Starting http fwww server on $hostinfo ..."
        cd $HOME/fwww/bin_dir
        source  $HOME/fwww/venv_fwww/bin/activate
        ./start_all_servers.sh
    else
	    exec $JAGUAR_HOME/bin/jaguar.bin > $logfile 2>&1 &
    fi

    echo

fi


