#!/bin/bash

echo "dataone-cn-metacat.prerm called with action: $1, version: $2"

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

/etc/init.d/tomcat8 stop
/etc/init.d/apache2 stop

TOMCAT_USER=tomcat8
TOMCAT_HOME=/var/lib/tomcat8
APACHE_CONF=/etc/apache2
METACAT_VAR=/var/metacat
SOURCE_DIR=/usr/share/metacat
PG=postgresql

## drop metacat schema and user
#echo "Dropping metacat database"
#su postgres -c "dropdb metacat"
#echo "Dropping metacat database use metacat"
#su postgres -c "psql --command \"DROP USER metacat\""

## Do NOT revert postgres configuration since other modules may still have customizations

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

#rm -rf ${METACAT_VAR}
rm -rf ${SOURCE_DIR}

# old artifacts (previous releases of metacat)
rm -rf ${TOMCAT_HOME}/webapps/knb
rm -f ${TOMCAT_HOME}/webapps/knb*
rm -f ${TOMCAT_HOME}/conf/Catalina/localhost/knb.xml
rm -f ${APACHE_CONF}/jk_mount/knb_jk.*

#current naming
rm -rf ${TOMCAT_HOME}/webapps/metacat
rm -f ${TOMCAT_HOME}/webapps/metacat*
rm -f ${TOMCAT_HOME}/conf/Catalina/localhost/metacat.xml
rm -f ${APACHE_CONF}/jk_mount/metacat_jk.*

rm -f ${TOMCAT_HOME}/conf/policy.d/51metacat.policy

rm -rf /etc/dataone/storage

/etc/init.d/tomcat8 start
/etc/init.d/apache2 start

# disable the metacat_proxy configurations
a2disconf metacat_proxy


## Update DateONE Version Info Doc
if [ -e /usr/share/dataone-cn-version-tool/dataone-cn-version-tool.jar ] ; then
   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
fi

# source the debconf library
#if [ -e "/usr/share/debconf/confmodule" ]; then
#    . /usr/share/debconf/confmodule
#    # purge unneeded selections for metacat on removal
#    db_purge
#fi

exit 0