#!/bin/bash
print_yellow () {
    echo -e "\03s3[93m"$1"\033[0m"
}
print_red () {
    echo -e "\033[91m"$1"\033[0m"
}
wb_restart () {
    for srv in $(systemctl list-units --type=service | grep winbind | grep loaded | awk '{print $1}')
        do
            systemctl restart $srv
        done
}
if [ "$EUID" -ne 0 ]
  then print_red "Please run as root or with sudo"
  exit
fi
offline=false
ping packages.wnam.ru -c1 > /dev/null 2>&1
if [ $? -ne 0 ]; then
    offline=true
fi
dir=$(pwd)
if [ -n "$1" ]; then
    up_file=$1
elif [ -f adctool.tgz ]; then
    up_file="$dir/adctool.tgz"
else
    if $offline ; then
        print_red "Файл обновлений не найден. Загрузка из сети недоступна."
        exit
    else
        print_yellow "Файл обновлений не найден. Обновление будет загружено."
        wget -O adctool.tgz https://packages.wnam.ru/wnam/1e98eccc-3ab1-4373-95e7-5a2d73a4cab0/adctool.tgz
        up_file="$dir/adctool.tgz"
    fi
fi
cd /home/wnam
systemctl stop adctool
if [ -d /home/wnam/adctool ] && [ -d /home/wnam/adctool.prev ]; then
    rm -rf /home/wnam/adctool.prev
fi
mv /home/wnam/adctool /home/wnam/adctool.prev
tar zxvf "$up_file" -C /home/wnam
cp /home/wnam/adctool.prev/keys/* /home/wnam/adctool/keys/
if [ -d /home/wnam/adctool.prev/samba/ ]; then
    cp -r /home/wnam/adctool.prev/samba/* /home/wnam/adctool/samba/
fi
cp /home/wnam/adctool.prev/.flaskenv /home/wnam/adctool/
cp /home/wnam/adctool.prev/config.json /home/wnam/adctool/

cd /home/wnam/adctool
if [ -f /etc/os-release ]; then
    . /etc/os-release
    OS=$NAME
    VER=$VERSION_ID
    if [ "$OS" == "Ubuntu" ] && [[ $VER == "20.04" || $VER == "22.04" ]]; then
        if [ ! -f /usr/lib/x86_64-linux-gnu/samba/libwinbind-client.so.0.bak ]; then
            mv /usr/lib/x86_64-linux-gnu/samba/libwinbind-client.so.0 /usr/lib/x86_64-linux-gnu/samba/libwinbind-client.so.0.bak
        fi
        if [ "$(md5sum /home/wnam/adctool/libwinbind-client.so-ubuntu)" != "$(md5sum /usr/lib/x86_64-linux-gnu/samba/libwinbind-client.so.0)" ]; then
            cp /home/wnam/adctool/libwinbind-client.so-ubuntu /usr/lib/x86_64-linux-gnu/samba/libwinbind-client.so.0
            wb_restart
        fi
    elif [[ "$OS" == *"Debian"* ]] && [ $VER == "11" ]; then
        if [ ! -f /usr/lib/x86_64-linux-gnu/samba/libwinbind-client.so.0.bak ]; then
            mv /usr/lib/x86_64-linux-gnu/samba/libwinbind-client.so.0 /usr/lib/x86_64-linux-gnu/samba/libwinbind-client.so.0.bak
        fi
        if [ "$(md5sum /home/wnam/adctool/libwinbind-client.so)" != "$(md5sum /usr/lib/x86_64-linux-gnu/samba/libwinbind-client.so.0)" ]; then
            cp /home/wnam/adctool/libwinbind-client.so /usr/lib/x86_64-linux-gnu/samba/libwinbind-client.so.0
            wb_restart
        fi
    elif [[ "$OS" == *"Debian"* ]] && [ $VER == "12" ]; then
        if [ ! -f /usr/lib/x86_64-linux-gnu/libwbclient.so.0.15.bak ]; then
            mv /usr/lib/x86_64-linux-gnu/libwbclient.so.0.15 /usr/lib/x86_64-linux-gnu/libwbclient.so.0.15.bak
        fi
        if [ "$(md5sum /home/wnam/adctool/libwbclient.so-deb12)" != "$(md5sum /usr/lib/x86_64-linux-gnu/libwbclient.so.0.15)" ]; then
            cp /home/wnam/adctool/libwbclient.so-deb12 /usr/lib/x86_64-linux-gnu/libwbclient.so.0.15
            wb_restart
        fi
    elif [ "$OS" == "Astra Linux" ]; then
        if [ ! -f /usr/lib/x86_64-linux-gnu/samba/libwinbind-client.so.0.bak ]; then
            mv /usr/lib/x86_64-linux-gnu/samba/libwinbind-client.so.0 /usr/lib/x86_64-linux-gnu/samba/libwinbind-client.so.0.bak
        fi
        if [ "$(md5sum /home/wnam/adctool/libwinbind-client.so)" != "$(md5sum /usr/lib/x86_64-linux-gnu/samba/libwinbind-client.so.0)" ]; then
            cp /home/wnam/adctool/libwinbind-client.so /usr/lib/x86_64-linux-gnu/samba/libwinbind-client.so.0
            wb_restart
        fi
    else
        print_red "Operation system $OS $VER is not supported"
    fi
fi
if [ -f wnam-sudoers-template ]; then
	cp wnam-sudoers-template /etc/sudoers.d/wnam
fi
chown -R wnam:wnam /home/wnam/adctool
chown -R root:root /home/wnam/adctool/samba/*
chown wnam:wnam /home/wnam/adctool/samba/*
chmod a+s /usr/bin/wbinfo 
chmod a+s /usr/bin/ntlm_auth
if ! grep -q "rdns=false" /etc/krb5.conf
then
    sed -i '/\[libdefaults\]/a \ \ \ \ \ \ \ \ rdns=false' /etc/krb5.conf
fi
systemctl daemon-reload
systemctl start adctool
