#!/bin/bash

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

echo "Running the prerm file in the dataone-cn-index package"
## Handle the index build tool
rm -f /usr/share/java/d1_index_build_tool.jar

## handle index task generator
/etc/init.d/d1-index-task-generator stop
update-rc.d -f d1-index-task-generator remove
rm -f /etc/init.d/d1-index-task-generator

## handle index task processor
/etc/init.d/d1-index-task-processor stop
update-rc.d -f d1-index-task-processor remove
rm -f /etc/init.d/d1-index-task-processor

## clean up log and config directory
rm -rf /etc/dataone/index/index-generation-context
rm -f /etc/dataone/index/solr/d1search.xsl
rm -f /etc/dataone/index/jdbc.properties
rm -f /etc/dataone/index/log4j-generator.properties
rm -f /etc/dataone/index/log4j-processor.properties
rm -f /etc/dataone/index/solr.properties
rm -f /etc/dataone/index/messaging.properties

#rm -rf /var/log/dataone/index

rm -f /var/run/d1-index-task-generator.pid
rm -f /var/run/d1-index-task-processor.pid

rm -f /usr/share/java/d1_index_task_generator_daemon.jar
rm -f /usr/share/java/d1_index_task_processor_daemon.jar

### Handle postgres cleanup
PG=postgresql
PG_USER=postgres

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

## drop index task schema and user
#echo "Dropping index task database"
#su ${PG_USER} -c "dropdb ${INDEX_DB}"
#echo "Droping index task database user indexer"
#su ${PG_USER} -c "psql --command \"DROP USER ${INDEX_DB_USER}\""

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

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

echo "Finish the running of the prerm file of the dataone-cn-index package"
exit 0