#!/bin/bash
# Let's add the first local ip to the /tmp/etc/issue and external ip to ews.ip file
# If the external IP cannot be detected, the internal IP will be inherited.
myUUID=$(cat /data/uuid)
myLOCALIP=$(ip address show | awk '/inet .*brd/{split($2,a,"/"); print a[1]; exit}')
myEXTIP=$(/opt/tpot/bin/myip.sh)
if [ "$myEXTIP" = "" ];
  then
    myEXTIP=$myLOCALIP
fi

myBLACKHOLE_STATUS=$(ip r | grep "blackhole" -c)
if [ "$myBLACKHOLE_STATUS" -gt "500" ];
  then
    myBLACKHOLE_STATUS="| BLACKHOLE: [ ENABLED ]"
  else
    myBLACKHOLE_STATUS="| BLACKHOLE: [ DISABLED ]"
fi

# Build issue
echo "" > /tmp/etc/issue
echo "T-Pot 23.12" >> /tmp/etc/issue
echo >> /tmp/etc/issue
echo ",---- [ \n ] [ \d ] [ \t ]" >> /tmp/etc/issue
echo "|" >> /tmp/etc/issue
echo "| IP: $myLOCALIP ($myEXTIP)" >> /tmp/etc/issue
echo "| SSH: ssh -l tsec -p 64295 $myLOCALIP" >> /tmp/etc/issue
#if [ "$myCHECKIFSENSOR" == "0" ];
#  then
echo "| WEB: https://$myLOCALIP:64297" >> /tmp/etc/issue
#fi
echo "| ADMIN: https://$myLOCALIP:64294" >> /tmp/etc/issue
echo "$myBLACKHOLE_STATUS" >> /tmp/etc/issue
echo "|" >> /tmp/etc/issue
echo "\`----" >> /tmp/etc/issue
echo >> /tmp/etc/issue
tee /data/ews/conf/ews.ip << EOF
[MAIN]
ip = $myEXTIP
EOF
tee /data/tpot/etc/compose/elk_environment << EOF
HONEY_UUID=$myUUID
MY_EXTIP=$myEXTIP
MY_INTIP=$myLOCALIP
MY_HOSTNAME=$HOSTNAME
EOF

chown tpot:tpot /data/ews/conf/ews.ip
chmod 770 /data/ews/conf/ews.ip