# this file is sourced from /usr/sbin/linuxmuster-pam-mount

# source config file
. /etc/linuxmuster-client/profile/profile.conf || exit 1
# source profile functions
. /var/lib/linuxmuster-client-profile/functions.inc || exit 1

# log some info
$LOGGING && msg2log pre-mount "Entering 010-profilecopy $1 $2"

$LOGGING && msg2log pre-mount "Environment settings are: USER=$USER VOLUME=$VOLUME MNPT=$MNTPT OPTIONS=$OPTIONS SERVER=$SERVER NUMUID=$NUMUID NUMPRIGID=$NUMPRIGID FULLNAME=$FULLNAME HOMEDIR=$HOMEDIR LOGINSHELL=$LOGINSHELL"

# this script should only get executed once, before the users home from the
# server gets mounted. In this case $USER and $VOLUME are the same.
# $USER and $VOLUME can also be the same, when mounting $VOLUME from a different server,
# thus return if $USER and $VOLUME are different or if the serverip is different from the serverip of the main server
requestserverip=$(gethostip $SERVER | awk '{print $2}')
lmlserverip=$(grep "^uri\ ldap://" /etc/ldap.conf | head -n 1 | sed "s@^uri\ ldap://@@;s@/.*@@")
if [ $USER != $VOLUME -o $lmlserverip != $requestserverip ]; then
   msg2log pre-mount "LML: $lmlserverip, Request: $requestserverip"
   return 0
fi

# Additional check, if the Home_auf_Server Directory is not empty and return
# If it was not empty, it would be bad to sync the profile
. /etc/linuxmuster-client/linuxmuster-client.conf || exit 1
if [ "$(ls -A $HOMEDIR/$HOMEFOLDERNAME)" ]; then
    msg2log pre-mount "$HOMEFOLDERNAME is not empty, not syncing."
    return 0
fi

# log success
$LOGGING && msg2log pre-mount "We are mounting users home, copy default profile"

rsync -r $RSYNC_OPTIONS /home/$PROFILE_USER/ $HOMEDIR/

mkdir -p $MNTPT
