#!/bin/bash

set -e

##### 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-zookeeper.install.log

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


# zk_user also defined in /etc/init.d/zookeeper and needs to match this value
ZK_USER=tomcat8
ZK_HOME=/var/lib/zookeeper
ZK_CONF_HOME=/var/lib/zookeeper/conf
ZK_CLIENT_PORT=2181
ZK_SERVER_PORT_1=2888
ZK_SERVER_PORT_2=3888
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"

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

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.
MY_POSSIBLE_IPS=(`hostname --all-ip-addresses`)
for i in ${MY_POSSIBLE_IPS[@]}
do
	if [[ "${IP_LIST[@]}" =~ "${i}" ]]; then
		LOCAL_IP=${i}
	fi
done

##### Configure zookeeper server configuration
mv ${ZK_CONF_HOME}/zoo.cfg ${ZK_CONF_HOME}/zoo.cfg.bak
cp /usr/share/dataone-zookeeper/debian/server_configs/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

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

##### Configure log output directory if not there
if [ ! -d /var/log/dataone/cn ]
then
	if (mkdir /var/log/dataone/cn >> ${D1_LOG_DIR}/${D1_LOG_FILE} 2>&1 ); then
    log "mkdir /var/log/dataone/cn succeeded"
    fi
fi

if (chown -R ${ZK_USER}:${ZK_USER} /var/log/dataone/cn >> ${D1_LOG_DIR}/${D1_LOG_FILE} 2>&1 ); then
    log "chown -R ${ZK_USER}:${ZK_USER} /var/log/dataone/cn succeeded"
fi
######

chown -R ${ZK_USER}:${ZK_USER} ${ZK_HOME}

chmod +x /etc/init.d/zookeeper
update-rc.d zookeeper defaults
update-rc.d zookeeper enable

/etc/init.d/zookeeper start

db_stop
exit 0