#!/bin/bash
set -e

ACTION=${1}
NEW_VERSION=${2}

TOMCAT=tomcat9
TOMCAT_USER=tomcat
DATAONE_USER=tomcat
TOMCAT_HOME=/var/lib/tomcat9

# 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

# Reminder: don't echo to stdout, it messes up debconf

D1_LOG_DIR=/var/log/dataone
D1_LOG_FILE=dataone-cn-os-core.remove.log
if [ ! -e ${D1_LOG_DIR} ]; then
    mkdir -p ${D1_LOG_DIR}
    chown ${TOMCAT_USER}:${TOMCAT_USER} ${D1_LOG_DIR}
fi

# functions to echo to STDERR or the install log instead of STDOUT
logError () {
    echo -e "$@" 1>&2
}

log () {
    now=$(date "+%Y-%m-%d %H:%M:%S %Z: ")
    echo -e "${now} postrm $@" >> ${D1_LOG_DIR}/${D1_LOG_FILE}
}

SOURCE_DIR=/usr/share/dataone-cn-os-core
SCRIPT_DIR=${SOURCE_DIR}/debian
APACHE_CONF=/etc/apache2
MOD_JK_CONF=/etc/libapache2-mod-jk
SITES="cn-ssl"

log "$ACTION $VERSION"
# source the debconf library
if [ "$ACTION" = "purge" ]; then
    log "Purging the database"
    db_purge
    
	## Stop apache

	if (pidof apache2  >> ${D1_LOG_DIR}/${D1_LOG_FILE} 2>&1); then
		log "Stopping Apache"
		if ! (/etc/init.d/apache2 stop >> ${D1_LOG_DIR}/${D1_LOG_FILE} 2>&1); then
			log "Unable to stop Apache"
		fi
	fi
	
	# Turn off the ssl module
	
	## disable  mod jk 
	log "Disabling Mod JK"
	if ! (a2dismod jk  >> ${D1_LOG_DIR}/${D1_LOG_FILE} 2>&1); then
		log "Unable to Diable Mod JK"
	fi
	
	#echo "Removing jk.conf from ${APACHE_CONF}/mods-available/"
	# maybe do a find in the subdirectory with a rm
	#rm -f ${SCRIPT_DIR}/jk.conf ${APACHE_CONF}/mods-available/jk.conf*
	
	## remove workers.properties file for mod-jk
	## this logic does not work for some reason, need to debug...
	
	#if [ -e ${MOD_JK_CONF}/workers.properties ]; then 
	  ## if there is a difference, then the diff script returns false
	#  if ! (diff ${SCRIPT_DIR}/workers.properties ${MOD_JK_CONF}/workers.properties >> ${D1_LOG_DIR}/${D1_LOG_FILE} 2>&1); then
	#    echo "Backing up ${MOD_JK_CONF}/workers.properties to ${MOD_JK_CONF}/workers.properties.${LONG_DATE}"
	#    mv ${MOD_JK_CONF}/workers.properties ${MOD_JK_CONF}/workers.properties.${LONG_DATE}
	#  fi
	# fi
	log "Removing workers.properties from ${MOD_JK_CONF}/"
	if ! (rm -f ${MOD_JK_CONF}/workers.properties*  >> ${D1_LOG_DIR}/${D1_LOG_FILE} 2>&1); then
		log "Unable to remove ${MOD_JK_CONF}/workers.properties"
	fi
	
	if ! (rm -rf ${SOURCE_DIR} ); then
		log "Unable to remove ${SOURCE_DIR}"
	fi
	
	## disable site configuration files
	for SITE in ${SITES}
	do
		
		if [ ${SITE}.conf != "" -a -e ${APACHE_CONF}/sites-enabled/${SITE}.conf ]; then
			log "Disabling and removing ${SITE}.conf"
			if ! (a2dissite ${SITE} >> ${D1_LOG_DIR}/${D1_LOG_FILE} 2>&1); then
				log "Unable to disable $site in apache2"
			fi
		
			if ! (rm -f  ${APACHE_CONF}/sites-available/${SITE}* >> ${D1_LOG_DIR}/${D1_LOG_FILE} 2>&1); then
				log "unable to remove ${APACHE_CONF}/sites-available/${SITE}.conf"
			fi
	    fi
	done
	
	## enable the default apache site
	if ! (a2ensite 000-default >> ${D1_LOG_DIR}/${D1_LOG_FILE} 2>&1); then
		log "Unable to enable default website again in apache2"
	fi
	## Start Apache
	if ! (/etc/init.d/apache2 start >> ${D1_LOG_DIR}/${D1_LOG_FILE} 2>&1); then
		log "Unable to start Apache2"
	fi


fi
db_stop
exit 0
