#!/bin/sh
# DocumentId:	$Id: update-twinspot-users-skel 1076 2001-08-20 08:45:21Z olalu $
# Author:	$Author: olalu $
# Date:		$Date: 2001-08-20 10:45:21 +0200 (mån, 20 aug 2001) $
# Summary:
#	Updates the twinspot base users skeleton, and patch if needed.

# Read the default twinspot config.
. /usr/share/twinspot/defaults.conf
. /usr/share/twinspot/change.func
. /usr/share/twinspot/owner.func

for TEST in / $DHOME /etc ; do
    if mount | grep "on[[:space:]][[:space:]]*$TEST[[:space:]].*type[[:space:]][[:space:]]*nfs[[:space:]]" > /dev/null 2>&1 ; then
	echo "Does not add or remove users when $TEST is nfsmounted."
	exit 0
    fi
done

# Users that exist in password file.
EUSERS=$(grep "^[^:]*:[^:]*:[5-9][0-9][0-9]:" $PWDF | sed -e "s|:.*||g;")

for U in $EUSERS ; do
    . $USERSC
    if [ -f $USERSRC/$U ] ; then
	. $USERSRC/$U
    fi
    if [ -d "$DHOME/$U" ] ; then
	if [ -d "$USERSKELSRC" ] ; then
	    rsync -rlptD -I "$USERSKELSRC/" /etc/skel
	    rsync -rlptD -I "$USERSKELSRC/" "$DHOME/$U"
	    export PATCHDEST="$DHOME/$U"
	    find $USERSKELSRC -path "$USERSKELSRC/*/$U/*" -type f -name "*.patch" -exec twinspot-patch {} \;
	fi
	changeowner $U $U "$DHOME/$U"
	if [ ! -z "$EMAIL" ] ; then
	    PROFILE="$DHOME/$U/.bash_profile"
	    if grep "^EMAIL=" /dev/null 2>&1 ; then
		changefile "s|\(EMAIL=.*\)|export \1|;" $PROFILE
	    fi
	    if grep "export[[:space:]][[:space:]]*EMAIL=" $PROFILE > /dev/null 2>&1 ; then
		if ! grep grep "export[[:space:]][[:space:]]*EMAIL='$EMAIL'" $PROFILE > /dev/null 2>&1 ; then
		    changefile "s|export[[:space:]][[:space:]]*EMAIL=.*|export EMAIL='$EMAIL'|;" $PROFILE
		fi
	    else
		echo "export EMAIL='$EMAIL'" >> $PROFILE
	    fi
	fi
    fi
done
