#!/usr/bin/bash
echo "Iniciando update..."
echo "Fazendo bkp do nxrelay"
cp -r /nxrelay /usr/local/nxrelay_bkp_$(date +%d%m%Y)
cd /nxrelay
echo "Baixando arquivo"
/usr/bin/wget --quiet --no-check-certificate http://git.usb.org.br/infra/scripts/raw/master/nxrelay-2.7.8-adv.zip
/usr/bin/zip -qq -d nxrelay-2.7.8-adv.zip "conf/cfg.properties" 
/usr/bin/zip -qq -d nxrelay-2.7.8-adv.zip "conf/log4j.properties" 

echo "Extraindo arquivos"
/usr/bin/systemctl stop nxrelay
/usr/bin/unzip -qq -o nxrelay-2.7.8-adv.zip
/usr/bin/systemctl start nxrelay

echo "Definindo variaveis"
PREFIX=$(ip -o -4 addr list eth0 | awk '{print $4}' | cut -d/ -f2)

sed -i '/local_dns/d' /nxrelay/conf/cfg.properties
sed -i '/local_domain/d' /nxrelay/conf/cfg.properties
sed -i '/token/a bypass_domain = adventistas.local' /nxrelay/conf/cfg.properties;
sed -i "/token/a bypass_dns = $(ip addr show eth0 | grep "inet\b" | awk '{print $2}' | sed  -n "s/1.$PREFIX/3/p"),$(ip addr show eth0 | grep "inet\b" | awk '{print $2}' | sed  -n "s/1.$PREFIX/4/p")" /nxrelay/conf/cfg.properties;
sed -i '/token/a local_domain = ' /nxrelay/conf/cfg.properties ;
sed -i '/token/a local_dns = 1.1.1.1,8.8.8.8' /nxrelay/conf/cfg.properties;
sed -i -e 's/'\"\$(printf '\015')\"'//g' /nxrelay/conf/cfg.properties;


/usr/bin/systemctl restart nxrelay

sed -i '/distroverpkg/a  exclude=nxrelay*' /etc/yum.conf;

echo "Tudo OK!"
