#!/usr/bin/sh
# It receives polydir path as $1, the instance path as $2,
# a flag whether the instance dir was newly created (0 - no, 1 - yes) in $3,
# and user name in $4.
#
# The following section will copy the contents of /etc/skel if this is a
# newly created home directory.
if [ "$3" = 1 ]; then
        # This line will fix the labeling on all newly created directories
        [ -x /sbin/restorecon ] && /sbin/restorecon "$1"
        user="$4"
        passwd=$(getent passwd "$user")
        homedir=$(echo "$passwd" | cut -f6 -d":")
        if [ "$1" = "$homedir" ]; then
                gid=$(echo "$passwd" | cut -f4 -d":")
                cp -rT /etc/skel "$homedir"
                chown -R "$user":"$gid" "$homedir"
                mask=$(awk '/^UMASK/{gsub("#.*$", "", $2); print $2; exit}' /etc/login.defs)
                mode=$(printf "%o" $((0777 & ~mask)))
                chmod ${mode:-700} "$homedir"
                [ -x /sbin/restorecon ] && /sbin/restorecon -R "$homedir"
        fi
fi

exit 0
