#!/bin/bash
#
# 1. setup /etc/dataone/index
# 2. Install d1_index_task_generator, d1_index_task_processor and 
#    d1_index_build_tool jars
# 3. Setup postgres d1-index-queue database
#
LONG_DATE=$(date +%Y%m%d%H%M%S)
VAR_DATAONE=/var/dataone
TOMCAT_USER=tomcat

chmod +x /etc/init.d/d1-index-task-generator
chmod +x /etc/init.d/d1-index-task-processor

if [ ! -d "/etc/dataone/index" ]; then
  mkdir /etc/dataone/index
  FRESH_INSTALL="true"
fi

if [ ! -d "/etc/dataone/index/solr" ]; then
  mkdir  /etc/dataone/index/solr
fi

#provide context properties files for postgres, log4j, solr location
cp /usr/share/dataone-cn-index/debian/d1client.properties /etc/dataone/index/d1client.properties
cp /usr/share/dataone-cn-index/debian/log4j-generator.properties /etc/dataone/index/log4j-generator.properties
cp /usr/share/dataone-cn-index/debian/log4j-processor.properties /etc/dataone/index/log4j-processor.properties
cp /usr/share/dataone-cn-index/debian/jdbc.properties /etc/dataone/index/jdbc.properties
cp /usr/share/dataone-cn-index/debian/solr.properties /etc/dataone/index/solr.properties
cp /usr/share/dataone-cn-index/debian/cn-index-processor.properties /etc/dataone/index/cn-index-processor.properties
cp /usr/share/dataone-cn-index/debian/cn-index-generator.properties /etc/dataone/index/cn-index-generator.properties
cp -rf /usr/share/dataone-cn-index/debian/index-generation-context /etc/dataone/index/
cp -rf /usr/share/dataone-cn-index/debian/schema-org-contexts /etc/dataone/index/


# source the debconf library
if [ -e "/usr/share/debconf/confmodule" ]; then
    . /usr/share/debconf/confmodule
else
    echo "debconf must be installed. Exiting."
    exit 1
fi

db_get dataone-cn-os-core/cn.hostname
HOSTNAME=$RET
if [ "$HOSTNAME" != "" ]
then
  sed -i.bak  's/SERVER_NAME/'${HOSTNAME}'/' /etc/dataone/index/d1client.properties
else
  echo "HOSTNAME can not be set in /etc/dataone/d1client.properties"
fi


chown -R ${TOMCAT_USER}:${TOMCAT_USER} /etc/dataone/index

cp /usr/share/dataone-cn-index/d1_index_task_generator_daemon.jar /usr/share/java
cp /usr/share/dataone-cn-index/d1_index_task_processor_daemon.jar /usr/share/java
cp /usr/share/dataone-cn-index/d1_index_build_tool.jar /usr/share/java

chown -R ${TOMCAT_USER}:${TOMCAT_USER} /usr/share/java/d1_index_task_generator_daemon.jar
chown -R ${TOMCAT_USER}:${TOMCAT_USER} /usr/share/java/d1_index_task_processor_daemon.jar
chown -R ${TOMCAT_USER}:${TOMCAT_USER} /usr/share/java/d1_index_build_tool.jar

if [ ! -d "/var/log/dataone/index" ]; then
  mkdir  /var/log/dataone/index
fi
chown -R ${TOMCAT_USER}:${TOMCAT_USER} /var/log/dataone/index

#### create a triple store directory for the index processor ###
if [ ! -d ${VAR_DATAONE} ]; then
    mkdir ${VAR_DATAONE}
fi 

if [ ! -d ${VAR_DATAONE}/index ]; then
  mkdir ${VAR_DATAONE}/index
fi 

if [ ! -d ${VAR_DATAONE}/index/tdb ]; then
  mkdir ${VAR_DATAONE}/index/tdb
fi
chown -R ${TOMCAT_USER}:${TOMCAT_USER} ${VAR_DATAONE}/index


###############################################################################
# Configure Postgres
###############################################################################
PG=postgresql
PG_USER=postgres
if [ -e "/etc/postgresql/14/main" ]; then
    PG_CONF=/etc/postgresql/14/main
else
    PG_CONF=/etc/postgresql/10/main
fi

INDEX_DB=d1-index-queue
INDEX_DB_USER=indexer
INDEX_DB_PASS=indexer

## modify pg_hba.conf
PG_HBA_IS_MODIFIED=`grep "${INDEX_DB} ${INDEX_DB_USER}" ${PG_CONF}/pg_hba.conf`
if [ "${PG_HBA_IS_MODIFIED}" == "" ]
then
  echo "backing up ${PG_CONF}/pg_hba.conf to ${PG_CONF}/pg_hba.conf.bak.index"
  cp ${PG_CONF}/pg_hba.conf ${PG_CONF}/pg_hba.conf.bak.index
  chown ${PG_USER} ${PG_CONF}/pg_hba.conf
  chgrp ${PG_USER} ${PG_CONF}/pg_hba.conf

  echo "appending 'host ${INDEX_DB} ${INDEX_DB_USER} 127.0.0.1 255.255.255.255 password' to ${PG_CONF}/pg_hba.conf"
  echo "host ${INDEX_DB} ${INDEX_DB_USER} 127.0.0.1 255.255.255.255 password" >> ${PG_CONF}/pg_hba.conf
fi

## create index task schema and user
echo "Creating index task database schema"
if su ${PG_USER} -c "psql -lqt | cut -d \| -f 1 | grep -qw $INDEX_DB" ; then
  echo "DB $INDEX_DB exists and we will not create it"
else
  echo "DB $INDEX_DB does not exist and we will create it"
  if ! (su ${PG_USER} -c "createdb ${INDEX_DB}"); then
      echo "Unable to createdb ${INDEX_DB}"
      exit 1
  fi
fi
#su ${PG_USER} -c "createdb ${INDEX_DB}"

echo "Creating index task db user - indexer" 
if su - $PG_USER -c "psql -t -c '\du'| cut -d \| -f 1 | grep -qw $INDEX_DB_USER" ; then
  echo "User $INDEX_DB_USER exists and we will not create it"
else
  echo "User $INDEX_DB_USER does not exist and we will create it"
  if ! (su ${PG_USER} -c "psql -c \"CREATE USER ${INDEX_DB_USER} WITH PASSWORD '${INDEX_DB_PASS}'\""); then
      echo "Unable to CREATE USER ${INDEX_DB_USER}"
      exit 1
  fi
fi
#su ${PG_USER} -c "psql -c \"CREATE USER ${INDEX_DB_USER} WITH PASSWORD '${INDEX_DB_PASS}'\""


## Restart the postgres db
echo "Restarting postgres database"
/etc/init.d/${PG} restart
######################################################


######### Finished! inform user how to start daemons
echo ""
echo "CN Search Index has finished installing. "
echo "To build or re-build Solr Index with /usr/share/dataone-cn-index/d1_index_build_tool.jar " 
echo "You can do this using /usr/share/dataone-cn-index/scripts/index-tool.sh for example."
echo ""
echo "While index task generateor/processor daemons are NOT running! You can use those commands to start them." 
echo "Start Index Task Generator with /etc/init.d/d1-index-task-generator start"
echo "Start Index Task Processor with /etc/init.d/d1-index-task-processor start"

## Update DateONE Version Info Doc
java -jar /usr/share/dataone-cn-version-tool/dataone-cn-version-tool.jar -F/usr/share/dataone-cn-version-tool/version-tool.properties -html > /var/www/cn-version.html
 
db_stop 
 
exit 0