#!/bin/bash

###################################################################
#Script Name	: install-graph-db.sh                                                                                              
#Description	: This bash script is a handler to setup https://bitbucket.org/jamkazam/social-graph-database-loader/src/master/ on localhost for development.
#Prerequisites : Python 3.8, pip, Serverless Framework, Postgresql Server and Client
###################################################################

PROGNAME=$0

usage() {
  cat << EOF >&2
Usage: $PROGNAME [-d <dir>] [-r <repo>] [-s <sshPrivateKeyFile>] 
[-p <pipExecutable>] [-H <pgHost>] [-U <pgUser>] [-P <pgPassword>] 
[-D <pgDatabase>] [-a <neo4jPassword>] [-b <serverPassword>]
[-c <serverPort>] [-h]

-d <dir>: directory to install. default: ./
-r <repo>: git url of social-graph-database-loader. default: git@bitbucket.org:jamkazam/social-graph-database-loader.git
-s <sshPrivateKeyFile>: ssh private key file to authorize git access. default: ~/.ssh/id_rsa
-p <pipExecutable>: pip executable path. default: /usr/bin/pip
-H <pgHost>: postgresql database host. default: localhost
-U <pgUser>: postgresql database user. default: postgres
-P <pgPassword>: postgresql database user password. default: postgres
-D <pgDatabase>: postgresql database name. default: jam
-a <neo4jPassword>: neo4j database password. default: password
-b <serverPassword>: serverless server password . default: password
-c <serverPort>: serverless server port . default: 3000
EOF
  exit 1
}

dir="$( cd -- "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )"
repo=git@bitbucket.org:jamkazam/social-graph-database-loader.git
sshPrivateKeyFile=./ssh/id_rsa
pipExecutable=/usr/bin/pip
pgHost=localhost
pgUser=postgres
pgPassword=postgres
pgDatabase=jam
neo4jPassword=password
serverPassword=password
serverPort=3000

while getopts d:r:s:p:H:U:P:D:a:b:c: o; do
  case $o in
    (d) dir=$OPTARG;;
    (r) repo=$OPTARG;;
    (s) sshPrivateKeyFile=$OPTARG;;
    (p) pipExecutable=$OPTARG;;
    (H) pgHost=$OPTARG;;
    (U) pgUser=$OPTARG;;
    (P) pgPassword=$OPTARG;;
    (D) pgDatabase=$OPTARG;;
    (a) neo4jPassword=$OPTARG;;
    (b) serverPassword=$OPTARG;;
    (c) serverPort=$OPTARG;;
    *) usage
  esac
done
shift "$((OPTIND - 1))"

#echo $serverPort
#exit

if [ -d "$dir" -a ! -h "$dir" ]
then
  echo "setting up social-graph-database-loader in $dir, please wait..."
  
  mkdir -p $dir
  chmod +w $dir

  cd $dir
  
  repoDir="${dir}/social-graph-database-loader"
  
  if [ ! -d $repoDir ]; then
    eval `ssh-agent -s`
    ssh-add $sshPrivateKeyFile
    git clone $repo
  fi

  cd $repoDir
  
  $pipPath install -r test_requirements.txt

  npm install

  CONTAINER_NAME="jamkazamneo4j"

  OLD="$(docker ps --all --quiet --filter=name="$CONTAINER_NAME")"
  if [ -n "$OLD" ]; then
    docker stop $OLD #&& docker rm $OLD
  fi
  echo "starting neo4j docker container... "
  docker run --rm --name $CONTAINER_NAME -p7473:7473 -p7474:7474 -p7687:7687 -d -v $dir/jamkazam/neo4j/data:/data -v $dir/jamkazam/neo4j/logs:/logs -v $dir/jamkazam/neo4j/import:/var/lib/neo4j/import -v $dir/jamkazam/neo4j/plugins:/plugins --env NEO4J_AUTH=neo4j/password --env NEO4J_dbms_connector_https_advertised__address="localhost:7473" --env NEO4J_dbms_connector_http_advertised__address="localhost:7474" --env NEO4J_dbms_connector_bolt_advertised__address="localhost:7687" neo4j:latest
  
  echo "srarting serverless offline..."
  serverless offline --neo4j_uri bolt://localhost:7687 --neo4j_password $neo4jPassword --postgres_host $pgHost --postgres_db $pgDatabase --postgres_user $pgUser --postgres_password $pgPassword --server_password=$serverPassword --httpPort $serverPort
else
   echo "Error: $dir not found or is symlink to $(readlink -f ${dir})."
fi