#!/bin/bash

#IP=`curl -s ifcfg.me`

IP=$(curl -s https://yandex.ru/internet/api/v0/ip | tr -d '"')
FULL=$(host $IP | sed 's/.* //' | sed 's/\.$//')
SHORT=`echo $FULL|sed 's/\..*//'`

echo "*************************************************************************"
echo " IP: "$IP
echo " Hostname: "$FULL
echo "*************************************************************************"
echo

if [ "`cat /etc/hosts|grep $FULL`" == "" ];then
        echo -e $IP'\t'$FULL'\t'$SHORT > /etc/hosts.t
        cat /etc/hosts >> /etc/hosts.t
        mv /etc/hosts.t /etc/hosts
fi

echo $FULL > /etc/hostname
hostname $FULL

cp /etc/network/interfaces /etc/network/interfaces-$(date +%s)

# Для тунельного балансера:
netconfig -B  | grep -qE "tunl0|tnl0" && 
ln -svf /usr/lib/yamail-network-scripts/99-tunnelRS.conf /etc/sysctl.d/ &&
sysctl -p /etc/sysctl.d/99-tunnelRS.conf

netconfig > /etc/network/interfaces

chattr -i /etc/resolv.conf
echo "
# THIS FILE HAS READONLY ATTRIBUTE
# THIS FILE CREATED BY yamail-network-scripts:

nameserver ::1
nameserver 127.0.0.1
nameserver 2a02:6b8::1:1
nameserver 77.88.8.8
search yandex.net yandex.ru mail.yandex.net
" > /etc/resolv.conf
chattr +i /etc/resolv.conf

/etc/init.d/bind9 restart

echo && echo
echo "######################################################"
echo "                       ALERT!"
echo "/etc/resolv.conf was updated and set to readonly mode!"
echo "######################################################"
echo && echo


exit 0;

