#!/bin/sh
[ -f /etc/samba/smb.conf ] || grep -q "server role = standalone server" /etc/samba/smb.conf && rm /etc/samba/smb.conf

if [ -f /etc/samba/smb.conf ]; then
  net ads testjoin -k >/dev/null 2>&1 && exit
fi
systemctl stop smbd nmbd winbind
systemctl mask nmbd
ln -sf /etc/omix/files/conf/smb.conf /etc/samba/smb.conf
ln -sf /etc/omix/files/conf/krb5.conf /etc/krb5.conf
rm -f /var/run/samba/*.[t,l]db \
    /var/lib/samba/*.[t,l]db \
    /var/lib/samba/private/*.[t,l]db \
    /var/cache/samba/*.[t,l]db  \
    /var/cache/samba/printing/*.[t,l]db
[ -f /etc/security/limits.d/samba.conf ] || cat >/etc/security/limits.d/samba.conf <<EOF
* - nofile 16384
root - nofile 16384
EOF
sed -i -E -e '/winbind/b' -e '/^passwd:|group:/s/^(.*)$/\1 winbind/' /etc/nsswitch.conf
/usr/share/omix/mkconf

net ads join -U Administrator%{{ admin_password }}
systemctl start smbd winbind
