#!/bin/bash -x

# 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-audit-index.debian.log
if [ ! -e ${D1_LOG_DIR} ]; then
    mkdir -p ${D1_LOG_DIR}
    chown tomcat7:tomcat7 ${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} postint $@" >> ${D1_LOG_DIR}/${D1_LOG_FILE}
}

SOLR_USER=tomcat7

APACHE_CONF_HOME=/etc/apache2/conf-available
JETTY_HOME=/var/lib/jetty

SOLR_ROOT=/var/solr
SOLR_HOME=${SOLR_ROOT}/home
SOLR_CORE_TEMPLATE_CONF_DIR=${SOLR_ROOT}/core-template/conf/
AUDIT_CORE_NAME=cn-audit
AUDIT_CORE_DIR=${SOLR_HOME}/${AUDIT_CORE_NAME}

# Zookeeper configuration
ZK_HOME=/var/lib/zookeeper
ZK_CONF_HOME=/var/lib/zookeeper/conf
ZK_CLIENT_PORT=9983
ZK_SERVER_PORT_1=7612
ZK_SERVER_PORT_2=7632
ZK_SERVER_ID_FILE=/var/lib/zookeeper/data/myid

# Substitution tokens used in config files like zoo.cfg, start.ini,etc
SERVER_1_TOKEN="D1_CN_IP_1"
SERVER_2_TOKEN="D1_CN_IP_2"
SERVER_3_TOKEN="D1_CN_IP_3"
ZK_CLIENT_PORT_TOKEN="D1_ZK_CLIENT_PORT"
ZK_SERVER_PORT_TOKEN_1="D1_ZK_SERVER_PORT_1"
ZK_SERVER_PORT_TOKEN_2="D1_ZK_SERVER_PORT_2"

log "start"

if ! (/etc/init.d/jetty stop >> ${D1_LOG_DIR}/${D1_LOG_FILE} 2>&1); then
	log "jetty refused to stop"
fi
if ! (/etc/init.d/zookeeper stop >> ${D1_LOG_DIR}/${D1_LOG_FILE} 2>&1); then
	log "zookeeper refused to stop"
fi

if [ ! -d "$AUDIT_CORE_DIR" ]; then
	FRESH_INSTALL="true"
	mkdir ${AUDIT_CORE_DIR}
	cp -rf ${SOLR_CORE_TEMPLATE_CONF_DIR} ${AUDIT_CORE_DIR}/conf/
fi

cp /usr/share/dataone-cn-audit-index/debian/schema.xml ${AUDIT_CORE_DIR}/conf/schema.xml
cp /usr/share/dataone-cn-audit-index/debian/solrconfig.xml ${AUDIT_CORE_DIR}/conf/solrconfig.xml
chown -R ${SOLR_USER}:${SOLR_USER} ${AUDIT_CORE_DIR}

if [ -n "$FRESH_INSTALL" ]; then
	/etc/init.d/jetty start
	if ! (curl "http://localhost:8983/solr/admin/cores?action=CREATE&name=$AUDIT_CORE_NAME&instanceDir=$AUDIT_CORE_DIR&dataDir=data" >> ${D1_LOG_DIR}/${D1_LOG_FILE} 2>&1); then
		log "CREATE of $AUDIT_CORE_NAME failed"
	fi
	/etc/init.d/jetty stop
fi

## Deploy and configure files that need variable subsitution
##### Parse node.properties to derive CN IP list.
db_get dataone-cn-os-core/cn.iplist
IP_LIST=(${RET})

SERVER_1=${IP_LIST[0]}
SERVER_2=${IP_LIST[1]}
SERVER_3=${IP_LIST[2]}
##### Use hostname command to derive local IP, used to set zookeeper server myid file.
# using hostname like this may return the wrong interface address, if more than
# a single interface is configured and the first interface address is 
# not the correct one to use -rpw (that being said, i should just add cn.host.ip
# into the dataone-cn-os-core debian backend db)
#LOCAL_IP=$(/bin/hostname -I | cut -d' ' -f1)
MY_POSSIBLE_IPS=(`hostname --all-ip-addresses`)
for i in ${MY_POSSIBLE_IPS[@]}
do
	if [[ "${IP_LIST[@]}" =~ "${i}" ]]; then
		LOCAL_IP=${i}
	fi
done

### Substitute server IP and PORT numbers in config files at token locations
##### Configure apache mod proxy for to allow solr communication between CN nodes
cp /usr/share/dataone-cn-audit-index/debian/modproxy_cnaudit.conf ${APACHE_CONF_HOME}/modproxy_cnaudit.conf
if ! (sed -i "s/$SERVER_1_TOKEN/$SERVER_1/g" ${APACHE_CONF_HOME}/modproxy_cnaudit.conf >> ${D1_LOG_DIR}/${D1_LOG_FILE}  2>&1); then
	log "unable to set $SERVER_1 in modproxy_cnaudit.conf"
fi
if ! (sed -i "s/$SERVER_2_TOKEN/$SERVER_2/g" ${APACHE_CONF_HOME}/modproxy_cnaudit.conf >> ${D1_LOG_DIR}/${D1_LOG_FILE}  2>&1); then
	log "unable to set $SERVER_2 in modproxy_cnaudit.conf"
fi
if ! (sed -i "s/$SERVER_3_TOKEN/$SERVER_3/g" ${APACHE_CONF_HOME}/modproxy_cnaudit.conf >> ${D1_LOG_DIR}/${D1_LOG_FILE}  2>&1); then
	log "unable to set $SERVER_3 in modproxy_cnaudit.conf"
fi

a2enconf modproxy_cnaudit

##### Configure jetty's start.ini with zookeeper server configuration
##### and bootstrap example for cn-audit index core
mv ${JETTY_HOME}/start.ini ${JETTY_HOME}/start.ini.bak
cp /usr/share/dataone-cn-audit-index/debian/jetty.start.ini ${JETTY_HOME}/start.ini
chown -R ${SOLR_USER}:${SOLR_USER} ${JETTY_HOME}
if ! (sed -i "s/$SERVER_1_TOKEN/$SERVER_1/g" ${JETTY_HOME}/start.ini >> ${D1_LOG_DIR}/${D1_LOG_FILE} 2>&1); then
	log "Unable to modify ${JETTY_HOME}/start.ini for $SERVER_1_TOKEN $SERVER_1"
fi
if ! (sed -i "s/$SERVER_2_TOKEN/$SERVER_2/g" ${JETTY_HOME}/start.ini >> ${D1_LOG_DIR}/${D1_LOG_FILE} 2>&1); then
	log "Unable to modify ${JETTY_HOME}/start.ini for $SERVER_2_TOKEN $SERVER_2"
fi
if ! (sed -i "s/$SERVER_3_TOKEN/$SERVER_3/g" ${JETTY_HOME}/start.ini >> ${D1_LOG_DIR}/${D1_LOG_FILE} 2>&1); then
	log "Unable to modify ${JETTY_HOME}/start.ini for server $SERVER_3_TOKEN $SERVER_3"
fi
if ! (sed -i "s/$ZK_CLIENT_PORT_TOKEN/$ZK_CLIENT_PORT/g" ${JETTY_HOME}/start.ini >> ${D1_LOG_DIR}/${D1_LOG_FILE} 2>&1); then
	log "Unable to modify ${JETTY_HOME}/start.ini for $ZK_CLIENT_PORT_TOKEN $ZK_CLIENT_PORT"
fi

##### Configure zookeeper server configuration
mv ${ZK_CONF_HOME}/zoo.cfg ${ZK_CONF_HOME}/zoo.cfg.bak
cp /usr/share/dataone-cn-audit-index/debian/zoo.cfg ${ZK_CONF_HOME}/zoo.cfg
if ! (sed -i "s/$SERVER_1_TOKEN/$SERVER_1/g" ${ZK_CONF_HOME}/zoo.cfg >> ${D1_LOG_DIR}/${D1_LOG_FILE} 2>&1); then
	log "Unable to modify ${ZK_CONF_HOME}/zoo.cfg for $SERVER_1_TOKEN $SERVER_1"
fi
if ! (sed -i "s/$SERVER_2_TOKEN/$SERVER_2/g" ${ZK_CONF_HOME}/zoo.cfg >> ${D1_LOG_DIR}/${D1_LOG_FILE} 2>&1); then
	log "Unable to modify ${ZK_CONF_HOME}/zoo.cfg for $SERVER_2_TOKEN $SERVER_2"
fi
if ! (sed -i "s/$SERVER_3_TOKEN/$SERVER_3/g" ${ZK_CONF_HOME}/zoo.cfg >> ${D1_LOG_DIR}/${D1_LOG_FILE} 2>&1); then
	log "Unable to modify ${ZK_CONF_HOME}/zoo.cfg for $SERVER_3_TOKEN $SERVER_3"
fi
if ! (sed -i "s/$ZK_CLIENT_PORT_TOKEN/$ZK_CLIENT_PORT/g" ${ZK_CONF_HOME}/zoo.cfg >> ${D1_LOG_DIR}/${D1_LOG_FILE} 2>&1); then
	log "Unable to modify ${ZK_CONF_HOME}/zoo.cfg for $ZK_CLIENT_PORT_TOKEN $ZK_CLIENT_PORT"
fi
if ! (sed -i "s/$ZK_SERVER_PORT_TOKEN_1/$ZK_SERVER_PORT_1/g" ${ZK_CONF_HOME}/zoo.cfg >> ${D1_LOG_DIR}/${D1_LOG_FILE} 2>&1); then
	log "Unable to modify ${ZK_CONF_HOME}/zoo.cfg for $ZK_CLIENT_PORT_TOKEN_1 $ZK_CLIENT_PORT_1"
fi
if ! (sed -i "s/$ZK_SERVER_PORT_TOKEN_2/$ZK_SERVER_PORT_2/g" ${ZK_CONF_HOME}/zoo.cfg ${ZK_CONF_HOME}/zoo.cfg >> ${D1_LOG_DIR}/${D1_LOG_FILE} 2>&1); then
	log "Unable to modify ${ZK_CONF_HOME}/zoo.cfg for $ZK_CLIENT_PORT_TOKEN_2 $ZK_CLIENT_PORT_2"
fi

##### Create zookeeper myid file for server 1, etc
if [[ $LOCAL_IP == $SERVER_1 ]]; then
  echo "1" > $ZK_SERVER_ID_FILE
elif [[ $LOCAL_IP == $SERVER_2 ]]; then
  echo "2" > $ZK_SERVER_ID_FILE
elif [[ $LOCAL_IP == $SERVER_3 ]]; then
  echo "3" > $ZK_SERVER_ID_FILE
else
  log "Unknown server IP, does not match IP form node.properties.  No zookeeper server myid file set."
fi
chown -R ${SOLR_USER}:${SOLR_USER} ${ZK_HOME}

### Firewall configuration to allow zookeeper communication
if ! (ufw allow from $SERVER_1 to any port $ZK_CLIENT_PORT >> ${D1_LOG_DIR}/${D1_LOG_FILE} 2>&1); then
	log "Unable to open ufw port $ZK_CLIENT_PORT for $SERVER_1 "
fi
if ! (ufw allow from $SERVER_2 to any port $ZK_CLIENT_PORT >> ${D1_LOG_DIR}/${D1_LOG_FILE} 2>&1); then
	log "Unable to open ufw port $ZK_CLIENT_PORT for $SERVER_2 "
fi
if ! (ufw allow from $SERVER_3 to any port $ZK_CLIENT_PORT >> ${D1_LOG_DIR}/${D1_LOG_FILE} 2>&1); then
	log "Unable to open ufw port $ZK_CLIENT_PORT for $SERVER_3 "
fi
if ! (ufw allow from $SERVER_1 to any port $ZK_SERVER_PORT_1 >> ${D1_LOG_DIR}/${D1_LOG_FILE} 2>&1); then
	log "Unable to open ufw port $ZK_SERVER_PORT_1 for $SERVER_1 "
fi
if ! (ufw allow from $SERVER_2 to any port $ZK_SERVER_PORT_1 >> ${D1_LOG_DIR}/${D1_LOG_FILE} 2>&1); then
	log "Unable to open ufw port $ZK_SERVER_PORT_1 for $SERVER_2 "
fi
if ! (ufw allow from $SERVER_3 to any port $ZK_SERVER_PORT_1 >> ${D1_LOG_DIR}/${D1_LOG_FILE} 2>&1); then
	log "Unable to open ufw port $ZK_SERVER_PORT_1 for $SERVER_3 "
fi

if ! (ufw allow from $SERVER_1 to any port $ZK_SERVER_PORT_2 >> ${D1_LOG_DIR}/${D1_LOG_FILE} 2>&1); then
	log "Unable to open ufw port $ZK_SERVER_PORT_2 for $SERVER_1 "
fi
if ! (ufw allow from $SERVER_2 to any port $ZK_SERVER_PORT_2 >> ${D1_LOG_DIR}/${D1_LOG_FILE} 2>&1); then
	log "Unable to open ufw port $ZK_SERVER_PORT_2 for $SERVER_2 "
fi
if ! (ufw allow from $SERVER_3 to any port $ZK_SERVER_PORT_2 >> ${D1_LOG_DIR}/${D1_LOG_FILE} 2>&1); then
	log "Unable to open ufw port $ZK_SERVER_PORT_2 for $SERVER_3 "
fi

/etc/init.d/zookeeper start
/etc/init.d/apache2 restart

if [ -z "$FRESH_INSTALL" ]; then
	/etc/init.d/jetty start
	if ! (curl "http://localhost:8983/solr/admin/cores?action=RELOAD&core=$AUDIT_CORE_NAME" >> ${D1_LOG_DIR}/${D1_LOG_FILE} 2>&1); then
		log "RELOAD of $AUDIT_CORE_NAME failed"
	fi
fi
log "complete"
db_stop
exit 0