#!/bin/bash

# see : https://wiki.samba.org/index.php/Rsync_based_SysVol_replication_workaround

set -e

SAMBA4_VARS=/etc/eole/samba4-vars.conf

if [ -f "${SAMBA4_VARS}" ]
then
    . "${SAMBA4_VARS}"
else
    # Template is disabled => samba is disabled
    exit 0
fi

#rsync
# -A : --acls     preserve ACLs (implies --perms)
# -X : --xattrs   preserve extended attributes
# -a : --archive  archive mode; equals -rlptgoD (no -H,-A,-X)
# -v : --verbose  increase verbosity
# -z : --compress compress file data during the transfer
# --delete-after  receiver deletes after transfer, not during
#rsync -XAavz --delete-after --password-file=/var/lib/samba/rsyncd-sysvol.secret rsync://sysvol-replication@192.168.0.5/SysVol/ /var/lib/samba/sysvol

if [ -n "${AD_DC_SYSVOL_REF}" ]
then
    # Warning: Make sure that the destination folder is really your SysVol folder, because the command
    # will replicate to the given directory and removes everything in it that isn't also on the source!
    # You could damage your system! So check the output carefully to see if the replication is doing what
    # you expect!
    rsync --rsh='ssh' \
               --compress --verbose \
               --acls --xattrs \
               --delete-after --times --owner --group --ignore-times --links  --perms \
               --recursive --size-only --delete --force --numeric-ids \
               --stats root@${AD_DC_SYSVOL_REF}:/home/sysvol/ /home/sysvol/

    samba-tool ntacl sysvolcheck
    #samba-tool ntacl sysvolreset
fi
