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

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

D1_LOG_DIR=/var/log/dataone
D1_LOG_FILE=dataone-cn-solr.remove.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 tomcat8:tomcat8 ${D1_LOG_DIR}
    fi
    now=$(date "+%Y-%m-%d %H:%M:%S %Z: ")
    echo -e "${now} postinst $@" >> ${D1_LOG_DIR}/${D1_LOG_FILE}
}

if (rm /etc/tomcat8/Catalina/localhost/d1-cn-index.xml >> ${D1_LOG_DIR}/${D1_LOG_FILE} 2>&1 ); then
    log "rm /etc/tomcat8/Catalina/localhost/d1-cn-index.xml succeeded"
fi

if (rm /etc/tomcat8/Catalina/localhost/d1-cn-log.xml >> ${D1_LOG_DIR}/${D1_LOG_FILE} 2>&1 ); then
    log "rm /etc/tomcat8/Catalina/localhost/d1-cn-log.xml succeeded"
fi

if (rm -f /etc/tomcat8/policy.d/53solr.policy >> ${D1_LOG_DIR}/${D1_LOG_FILE} 2>&1 ); then
    log "rm -f /etc/tomcat8/policy.d/53solr.policy succeeded"
fi

if (rm -f /etc/apache2/jk_mount/solr_jk.* >> ${D1_LOG_DIR}/${D1_LOG_FILE} 2>&1 ); then
    log "rm -f /etc/apache2/jk_mount/solr_jk.* succeeded"
fi

if (/etc/init.d/apache2 start >> ${D1_LOG_DIR}/${D1_LOG_FILE} 2>&1 ); then
    log "/etc/init.d/apache2 restart succeeded"
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 succeeded"
fi

db_stop
exit 0