#!/bin/sh

NDB_HOME=
export NDB_CONNECTSTRING
if [ -z "$MYSQLCLUSTER_TOP" ]; then
  echo "MYSQLCLUSTER_TOP not set"
  exit 1
fi
if [ -d "$MYSQLCLUSTER_TOP" ]; then :; else
  echo "$MYSQLCLUSTER_TOP directory does not exist"
  exit 1
fi
if [ -d "$MYSQLCLUSTER_TOP/ndb" ]; then :; else
  echo "$MYSQLCLUSTER_TOP/ndb directory does not exist"
  exit 1
fi

start_default_ndbcluster() {

# configurable parameters, make sure to change in mysqlcluterd as well
MYSQLCLUSTER_FILESYSTEM=$MYSQLCLUSTER_TOP/data/mysqlclusterfs
MYSQLCLUSTER_PORT_BASE="22"  # using ports MYSQLCLUSTER_PORT_BASE{"00","01", etc}
# end configurable parameters

# do some checks

NDB_CONNECTSTRING=

[ -d "$MYSQLCLUSTER_FILESYSTEM" ] || mkdir "$MYSQLCLUSTER_FILESYSTEM"
if [ -d "$MYSQLCLUSTER_FILESYSTEM" ]; then :; else
  echo "$MYSQLCLUSTER_FILESYSTEM filesystem directory does not exist"
  exit 1
fi


# set som help variables

NDB_HOST="localhost"
NDB_PORT=$MYSQLCLUSTER_PORT_BASE"00"
NDB_CONNECTSTRING_BASE="host=$NDB_HOST:$NDB_PORT;nodeid="


# Edit file system path and ports in config file

cd $MYSQLCLUSTER_FILESYSTEM
sed \
    -e s,"WRITE_PATH_TO_FILESYSTEM_2_HERE",$MYSQLCLUSTER_FILESYSTEM,g \
    -e s,"CHOOSE_PORT_BASE",$MYSQLCLUSTER_PORT_BASE,g \
    < $MYSQLCLUSTER_TOP/ndb/demos/config-templates/config_template-install.ini \
    > config.ini


# Start management server as deamon

NDB_ID="1"
NDB_CONNECTSTRING=$NDB_CONNECTSTRING_BASE$NDB_ID
#xterm -e mgmtsrvr -c $MYSQLCLUSTER_FILESYSTEM/config.ini &
if mgmtsrvr -d -c $MYSQLCLUSTER_FILESYSTEM/config.ini ; then :; else
  echo "Unable to start mgmtsrvr"
  exit 1
fi


# Start database node 

cd $MYSQLCLUSTER_FILESYSTEM  # the output from the database node gets where it starts
NDB_ID="2"
NDB_CONNECTSTRING=$NDB_CONNECTSTRING_BASE$NDB_ID
#xterm -T "NDB Cluster DB Node" -geometry 80x10 -xrm *.hold:true -e ndb -i &
ndb -d -i &

# Start xterm for application programs

NDB_ID="3"
NDB_CONNECTSTRING=$NDB_CONNECTSTRING_BASE$NDB_ID
#xterm -T "NDB Cluster API Node" -geometry 80x10 &
echo set before running ndbApi programs > export NDB_CONNECTSTRING=$NDB_CONNECTSTRING

# Start management client

#xterm -T "NDB Management Client" -geometry 80x10 -xrm *.hold:true -e mgmtclient $NDB_HOST $NDB_PORT &
echo "NDB Management Client starts with: mgmtclient $NDB_HOST $NDB_PORT"

# test if Ndb Cluster starts properly

NDB_ID="11"
NDB_CONNECTSTRING=$NDB_CONNECTSTRING_BASE$NDB_ID
if list_tables | grep "NDBT_ProgramExit: 0 - OK"; then :; else
  echo "Ndbcluster startup failed"
  exit 1
fi
}

start_mysql_install_db() {
  # run install of regular MySQL Server

  cd $MYSQLCLUSTER_TOP
  scripts/mysql_install_db --basedir=$MYSQLCLUSTER_TOP --datadir=$MYSQLCLUSTER_TOP/data --socket=$MYSQLCLUSTER_TOP/data/mysqlcluster.sock $*
}

if test "$1" = "ndb_started"
then
  shift
  mgmt_host=$1
  shift
  mgmt_port=$1
  shift
  if [ -z "$mgmt_host" -o -z "$mgmt_port" ]; then
    echo "syntax: ndb_started hostname port"
    exit 1
  fi
  NDB_CONNECTSTRING="host=$mgmt_host:$mgmt_port;nodeid=11"
  echo using NDB_CONNECTSTRING=$NDB_CONNECTSTRING
  start_mysql_install_db $*
else
  start_default_ndbcluster
  start_mysql_install_db
fi

