#!/bin/bash

if ! command -v zip >/dev/null 2>&1; then
    echo -e  "zip not installed. aborting"
	exit
fi
if ! command -v unzip >/dev/null 2>&1; then
    echo -e  "unzip not installed. aborting"
	exit
fi


file_check=`test -e /etc/flexmaster.path && echo "exist" || echo "not"`
if [ "${file_check}" == "exist" ]; then
    flexmaster_path=`cat /etc/flexmaster.path`
else
    flexmaster_path=`pwd`
fi

while read line
do
    echo -e "Staring to remove JNDI lookup from ${line} ..."
    zip -q -d $line org/apache/logging/log4j/core/lookup/JndiLookup.class >/dev/null
	echo -e "Remove log4j JNDI lookup from ${line} done ..."
done < <(find ${flexmaster_path} -type f -name "log4j-core*.jar")

tmp_dir="${flexmaster_path}/tmp_dir"

while read line
do
 mkdir -p ${tmp_dir}
 cd ${tmp_dir} && unzip ${line}  >/dev/null
 if [ -z `find ${tmp_dir} -type f -name "*log4j-core*.jar"` ]; then 
	   rm -fr ${tmp_dir}
	   continue
 fi
 echo -e  "Starting to remove log4j JNDI lookup from ${line} ..."
 cd ${tmp_dir} && zip -q -d BOOT-INF/lib/log4j-core*.jar /org/apache/logging/log4j/core/lookup/JndiLookup.class  >/dev/null
 cd ${tmp_dir} && zip -r -0 ${line} /BOOT-INF/lib/log4j-core*.jar  >/dev/null
 rm -fr ${tmp_dir}
 echo -e "Remove log4j JNDI lookup from ${line} done ..."
done < <(find ${flexmaster_path} -type f -name "*-SNAPSHOT.jar")
echo -e "Restarting UMM... \a\n"
cd ${flexmaster_path} && ./shutdown.sh
sleep 3
cd ${flexmaster_path} && ./startup.sh
echo -e "Restarting UMM...Done. \a\n"
