#!/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 loaded | grep -o "winbind\S*")
        do
            systemctl restart $srv
        done
}
if [ "$EUID" -ne 0 ]
  then print_red "Please run as root or with sudo"
  exit
fi
offline=false
curl --connect-timeout 3 ya.ru > /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
rm "$up_file"
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
    if [ "$VERSION_ID" ]; then 
        rm -f /usr/lib/x86_64-linux-gnu/samba/libwinbind-client.so-adctool
        rm -f /usr/lib/x86_64-linux-gnu/libwinbind-client.so-adctool
        OS=$NAME
        VER=$VERSION_ID
        if [ "$OS" == "Ubuntu" ] && [[ $VER == "20.04" || $VER == "22.04" ]]; then
            ln -s  /home/wnam/adctool/libwinbind-client.so-ubuntu /usr/lib/x86_64-linux-gnu/samba/libwinbind-client.so-adctool

        elif [[ "$OS" == *"Debian"* ]] && [ $VER == "11" ]; then
            ln -s  /home/wnam/adctool/libwinbind-client.so /usr/lib/x86_64-linux-gnu/samba/libwinbind-client.so-adctool

        elif [[ "$OS" == *"Debian"* ]] && [ $VER == "12" ]; then
                ln -s  /home/wnam/adctool/libwbclient.so-deb12 /usr/lib/x86_64-linux-gnu/libwinbind-client.so-adctool

        elif [ "$OS" == "Astra Linux" ]; then
                samba_version=`dpkg -s libwbclient0 | grep  Version | cut -d '.' -f 2`
                if [ "$samba_version" -eq "17"  ]; then
                    ln -s  /home/wnam/adctool/libwbclient.so-astra176 /usr/lib/x86_64-linux-gnu/libwinbind-client.so-adctool
                elif [ "$samba_version" -eq "19"  ]; then
                    ln -s  /home/wnam/adctool/libwbclient.so-astra177 /usr/lib/x86_64-linux-gnu/libwinbind-client.so-adctool
                else 
                    ln -s  /home/wnam/adctool/libwinbind-client.so /usr/lib/x86_64-linux-gnu/samba/libwinbind-client.so-adctool
                fi
        else
            print_red "Operation system $OS $VER is not supported"
        fi
    else
        print_red "Operation system is not supported"
    fi
else
    print_red "Operation system is not supported"
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 /home/wnam/adctool/libw*
chmod a+s /usr/bin/wbinfo 
chmod a+s /usr/bin/ntlm_auth
chown wnam /etc/samba/smb.conf
if ! grep -q "rdns=false" /etc/krb5.conf
then
    sed -i '/\[libdefaults\]/a \ \ \ \ \ \ \ \ rdns=false' /etc/krb5.conf
fi
wb_restart
systemctl daemon-reload
systemctl start adctool
