#!/bin/sh
# DocumentId:	$Id: update-twinspot-users-shell 1017 2001-07-29 12:52:06Z olalu $
# Author:	$Author: olalu $
# Date:		$Date: 2001-07-29 14:52:06 +0200 (sön, 29 jul 2001) $
# Summary:
#	Updates the shell accounts.

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

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;")

# Users that should have shell access.
FUSERS=$(find $USERSRC -maxdepth 3 -path "$USERSRC/*/*" -type f  -name "shell" | sed -e "s|/shell||;" | sed -e "s|.*/||;" | sort -u)

for U in $EUSERS ; do
    # Modify the user information for all users that should exist.
    . $USERSC
    if [ -f $USERSRC/$U ] ; then
	. $USERSRC/$U
    fi
    if [ ! -z "$CRYPT" ] ; then
	if echo "$FUSERS" | grep "^$U\$" > /dev/null 2>&1 ; then
	    # User should have shell access.
	    if ! grep "^$U:$CRYPT:" $SHDF > /dev/null 2>&1 ; then
		echo "Updating password for user $U."
		usermod -p "$CRYPT" $U
	    fi
	else
	    # User should not have shell access.
	    if ! grep "^$U:X:" $SHDF > /dev/null 2>&1 ; then
		echo "Updating password for user $U (no shell access)."
		usermod -p "X" $U
	    fi
	fi
    else
	# User should not have shell access.
	if ! grep "^$U:x:" $SHDF > /dev/null 2>&1 ; then
	    echo "Updating password for user $U (no password found)."
	    usermod -p "x" $U
	fi
    fi
    if [ ! -z "$SHELL" ] ; then
	if ! grep ":$SHELL\$" $PWDF > /dev/null 2>&1 ; then
	    usermod -s $SHELL $U
	fi
    fi
done
