
#!/bin/sh
#
#Sample script for AlterOS to use in postinstall tasks for domain join
#

while getopts "d:n:p:" option; do
   case $option in
        d) domain=$OPTARG;;
        n) username=$OPTARG;;
        p) password=$OPTARG;;
    \?) # Invalid option
         echo "Error: Invalid option"
         exit 1;;
   esac
done

compname=$(hostname | cut -d. -f1)
workgroup=$( cut -d. -f1 <<< "$domain" )

#echo $domain $compname $username $password >>/opt/vars.txt

echo "start install packages required to join"
dnf install sssd realmd oddjob oddjob-mkhomedir adcli samba-common samba-common-tools krb5-workstation -y

echo "start rename"
hostnamectl set-hostname $compname.$domain
systemctl restart systemd-hostnamed.service
sh -c "echo '127.0.0.1  `hostname -f` `hostname -s`' >> /etc/hosts"
systemctl restart NetworkManager

echo "Настройка multiuser входа в /etc/lightdm/lightdm.conf"
if grep -q "greeter-show-manual-login=true" /etc/lightdm/lightdm.conf; then
    sed -i "/greeter-show-manual-login=true/d" /etc/lightdm/lightdm.conf
fi

if grep -q "allow-user-switching=true" /etc/lightdm/lightdm.conf; then
    sed -i "/allow-user-switching=true/d" /etc/lightdm/lightdm.conf
fi

sed -i 's!^\[Seat\:\*\]!\[Seat\:\*\]\ngreeter-show-manual-login=true!' /etc/lightdm/lightdm.conf
sed -i 's!^\[Seat\:\*\]!\[Seat\:\*\]\nallow-user-switching=true!' /etc/lightdm/lightdm.conf

echo "start to join"
texterror=$(yes $password | realm join -U $username $domain 2>&1)
realmjoin=$?
echo "check status"
echo $realmjoin
if [ $realmjoin -eq 0 ]; then
    echo "Присоединение к домену выполнено успешно."
    net time set -S $domain
    echo "Изменение конфигурации sssd"
    sed -i 's!use_fully_qualified_names = True!use_fully_qualified_names = False!' /etc/sssd/sssd.conf
    sed -i -e '$aad_gpo_access_control = permissive' /etc/sssd/sssd.conf    
    echo "Включаем создание домашних директорий для пользователей домена"
    authselect select sssd with-mkhomedir > /dev/null
    echo "Запускаем и включаем службу sssd"
    systemctl enable sssd.service
    systemctl restart sssd
else    
    echo "Ошибка: $texterror"    
fi
