#!/bin/bash -x

set -e

#####
##### Read the commandline options
##### Only 'configure' is handled with processing, other actions
##### are ignored for now
#####
ACTION=${1}
ARG_VERSION=${2}

#####
##### 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

SOLR_USER=tomcat
SOLR_ROOT=/var/solr
SOLR_HOME=${SOLR_ROOT}/server/solr
SEARCH_CONFIGS_DIR=${SOLR_HOME}/configsets/dataone_search_configs/
EVENT_LOG_CONFIGS_DIR=${SOLR_HOME}/configsets/dataone_event_log_configs/

SEARCH_CORE_NAME=search_core
EVENT_LOG_CORE_NAME=event_core


LONG_DATE=`date +%Y%m%d%H%M%S`

D1_LOG_DIR=/var/log/dataone
D1_LOG_FILE=dataone-cn-solr.install.log

#####
##### log()
##### append stdout to a logfile
#####
function log() 
{
	#
	# Set Up logging
	# Reminder: don't echo to stdout, it messes up debconf
	#
    if [ ! -e ${D1_LOG_DIR} ]; then
        mkdir -p ${D1_LOG_DIR}
        chown ${SOLR_USER}:${SOLR_USER} ${D1_LOG_DIR}
    fi
    now=$(date "+%Y-%m-%d %H:%M:%S %Z: ")
    echo -e "${now} postinst $@" >> ${D1_LOG_DIR}/${D1_LOG_FILE}
}


log "start"

# Copy search core configuration
if [ ! -d "$SEARCH_CONFIGS_DIR" ]; then
	FRESH_SEARCH_CORE_INSTALL="true"
	mkdir ${SEARCH_CONFIGS_DIR}
fi

cp -rf /usr/share/dataone-cn-solr/debian/dataone_search_configs/* ${SEARCH_CONFIGS_DIR}
chown -R ${SOLR_USER}:${SOLR_USER} ${SEARCH_CONFIGS_DIR}

if [ -d "/etc/dataone/index" ]; then
    log "/etc/dataone/index exists and don't need to create it." 
else
    log "Directory /etc/dataone/index does not exists and create it."
    mkdir /etc/dataone/index
    chown ${SOLR_USER}:${SOLR_USER} /etc/dataone/index
fi

cp /usr/share/dataone-cn-solr/debian/dataone_search_configs/conf/schema.xml /etc/dataone/index/schema.xml
chown ${SOLR_USER}:${SOLR_USER} /etc/dataone/index/schema.xml

# Copy event log core configuration
if [ ! -d "$EVENT_LOG_CONFIGS_DIR" ]; then
	FRESH_EVENT_LOG_CORE_INSTALL="true"
	mkdir ${EVENT_LOG_CONFIGS_DIR}
fi

cp -rf /usr/share/dataone-cn-solr/debian/dataone_event_log_configs/* ${EVENT_LOG_CONFIGS_DIR}
chown -R ${SOLR_USER}:${SOLR_USER} ${EVENT_LOG_CONFIGS_DIR}


if [ ! -d /etc/dataone/event-index ]
then
	if (mkdir /etc/dataone/event-index >> ${D1_LOG_DIR}/${D1_LOG_FILE} 2>&1 ); then
        log "mkdir /etc/dataone/event-index succeeded"
    fi
fi

if (cp /usr/share/dataone-cn-solr/debian/eventIndexQueryFieldDescriptions.properties /etc/dataone/event-index/eventIndexQueryFieldDescriptions.properties >> ${D1_LOG_DIR}/${D1_LOG_FILE} 2>&1 ); then
    log "cp /usr/share/dataone-cn-solr/debian/eventIndexQueryFieldDescriptions.properties succeeded"
fi

if (cp /usr/share/dataone-cn-solr/debian/queryFieldDescriptions.properties /etc/dataone/index/solr/queryFieldDescriptions.properties >> ${D1_LOG_DIR}/${D1_LOG_FILE} 2>&1 ); then
    log "cp /usr/share/dataone-cn-solr/debian/queryFieldDescriptions.properties succeeded"
fi

##### Configure log output directory if not there
if [ ! -d /var/log/dataone/cn ]
then
	if (mkdir /var/log/dataone/cn >> ${D1_LOG_DIR}/${D1_LOG_FILE} 2>&1 ); then
    log "mkdir /var/log/dataone/cn succeeded"
    fi
fi

if (chown -R ${SOLR_USER}:${SOLR_USER} /var/log/dataone/cn >> ${D1_LOG_DIR}/${D1_LOG_FILE} 2>&1 ); then
    log "chown -R ${SOLR_USER}:${SOLR_USER} /var/log/dataone/cn succeeded"
fi
######


if ( cp /usr/share/dataone-cn-solr/debian/server_configs/event_log_proxy.conf /etc/apache2/conf-available >> ${D1_LOG_DIR}/${D1_LOG_FILE} 2>&1 ); then
    a2enconf event_log_proxy
    log "cp /usr/share/dataone-cn-solr/debian/server_configs/event_log_proxy.conf succeeded"
fi

if ( cp /usr/share/dataone-cn-solr/debian/server_configs/search_proxy.conf /etc/apache2/conf-available >> ${D1_LOG_DIR}/${D1_LOG_FILE} 2>&1 ); then
    a2enconf search_proxy
    log "cp /usr/share/dataone-cn-solr/debian/server_configs/search_proxy.conf succeeded"
fi

if ( cp /usr/share/dataone-cn-solr/debian/proxy.conf /etc/apache2/conf-available/ >> ${D1_LOG_DIR}/${D1_LOG_FILE} 2>&1 ); then
    a2enconf proxy
    log "p /usr/share/dataone-cn-solr/debian/proxy.conf succeeded"
fi

if [ -e /etc/apache2/conf-enabled/rewrite_cnlog_localhost.conf ]; then
  a2disconf rewrite_cnlog_localhost
  if ( rm /etc/apache2/conf-available/rewrite_cnlog_localhost.conf >> ${D1_LOG_DIR}/${D1_LOG_FILE} 2>&1 ); then
    log "remove /usr/share/dataone-cn-solr/debian/rewrite_cnlog_localhost.conf succeeded"
  fi
fi

if [ -e /etc/apache2/conf-enabled/rewrite_cnlog.conf ]; then
	a2disconf rewrite_cnlog
	rm /etc/apache2/conf-available/rewrite_cnlog.conf
fi

if [ -e /etc/apache2/conf-enabled/rewrite_cnquery.conf ]; then
	a2disconf rewrite_cnquery
	rm /etc/apache2/conf-available/rewrite_cnquery.conf
fi

if [ -e /etc/apache2/conf-enabled/rewrite_cnsearch.conf ]; then
  a2disconf rewrite_cnsearch
	rm /etc/apache2/conf-available/rewrite_cnsearch.conf
fi

/etc/init.d/apache2 restart

##### Copy the jar file into the correct lib directory for D1 Authorization
if (cp /usr/share/dataone-cn-solr/d1_solr_extensions.jar ${SOLR_ROOT}/server/lib/ >> ${D1_LOG_DIR}/${D1_LOG_FILE} 2>&1 ); then
    log "cp ${SOLR_ROOT}/server/lib/d1_solr_extensions.jar succeeded"
fi

if (cp /usr/share/dataone-cn-solr/d1_solr_extensions.jar ${SOLR_ROOT}/server/solr-webapp/webapp/WEB-INF/lib/ >> ${D1_LOG_DIR}/${D1_LOG_FILE} 2>&1 ); then
    log "cp ${SOLR_ROOT}/server/solr-webapp/webapp/WEB-INF/lib/d1_solr_extensions.jar succeeded"
fi

#### COPY A web.xml that configures authentication filters
#### MAKE THIS WORK WITH JETTY
if (mv /var/solr/server/solr-webapp/webapp/WEB-INF/web.xml /var/solr/server/solr-webapp/webapp/WEB-INF/web.xml.bak >> ${D1_LOG_DIR}/${D1_LOG_FILE} 2>&1 ); then
    log "mv /var/solr/server/solr-webapp/webapp/WEB-INF/web.xml succeeded"
fi

if (cp /usr/share/dataone-cn-solr/debian/server_configs/web.xml /var/solr/server/solr-webapp/webapp/WEB-INF >> ${D1_LOG_DIR}/${D1_LOG_FILE} 2>&1 ); then
    log "cp /usr/share/dataone-cn-solr/debian/server_configs/web.xml succeeded"
fi
##############

/etc/init.d/solr restart

###### Create cores if fresh install - otherwise update and reload cores.
if [ -z "$FRESH_SEARCH_CORE_INSTALL" ]; then
	if ! (${SOLR_ROOT}/server/scripts/cloud-scripts/zkcli.sh -cmd upconfig -z localhost:2181 -confname ${SEARCH_CORE_NAME} -confdir ${SEARCH_CONFIGS_DIR}conf/ >> ${D1_LOG_DIR}/${D1_LOG_FILE} 2>&1); then
		log "UPDATE of $SEARCH_CORE_NAME config failed"
	fi
	if ! (curl "http://localhost:8983/solr/admin/collections?action=RELOAD&name=$SEARCH_CORE_NAME" >> ${D1_LOG_DIR}/${D1_LOG_FILE} 2>&1); then
		log "RELOAD of $SEARCH_CORE_NAME failed"
	fi	
else
	if ! (${SOLR_ROOT}/bin/solr create -c ${SEARCH_CORE_NAME} -d ${SEARCH_CONFIGS_DIR} -rf 3 >> ${D1_LOG_DIR}/${D1_LOG_FILE} 2>&1); then
			log "ERROR creating solr core $SEARCH_CORE_NAME failed"
	fi
fi

if [ -z "$FRESH_EVENT_LOG_CORE_INSTALL" ]; then
    if ! (${SOLR_ROOT}/server/scripts/cloud-scripts/zkcli.sh -cmd upconfig -z localhost:2181 -confname ${EVENT_LOG_CORE_NAME} -confdir ${EVENT_LOG_CONFIGS_DIR}conf/ >> ${D1_LOG_DIR}/${D1_LOG_FILE} 2>&1); then
		log "UPDATE of $EVENT_LOG_CORE_NAME config failed"
	fi
	if ! (curl "http://localhost:8983/solr/admin/collections?action=RELOAD&name=$EVENT_LOG_CORE_NAME" >> ${D1_LOG_DIR}/${D1_LOG_FILE} 2>&1); then
		log "RELOAD of $EVENT_LOG_CORE_NAME failed"
	fi
else
	if ! (${SOLR_ROOT}/bin/solr create -c ${EVENT_LOG_CORE_NAME} -d ${EVENT_LOG_CONFIGS_DIR} -rf 3 >> ${D1_LOG_DIR}/${D1_LOG_FILE} 2>&1); then
		log "ERROR creating solr core $EVENT_LOG_CORE_NAME failed"
	fi
fi


## Update DateONE Version Info Doc
if ( 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  2>/dev/null ); then
    log "dataone-cn-version-tool.jar succeeded"
fi
db_stop
exit 0