#!/bin/sh
# DocumentId:	$Id: update-dpsyco-users-shell,v 1.11 2001/12/07 15:17:13 ola Exp $
# Author:	$Author: ola $
# Date:		$Date: 2001/12/07 15:17:13 $
# Summary:
#	Updates the shell accounts.

# Read the default dpsyco config.
. /etc/dpsyco/defaults.conf

TESTNFS="/ $DHOME /etc"
. /usr/share/dpsyco/checknfs.test

# Users that exist in password file.
EUSERS=$(grep "^[^:]*:[^:]*:$UID_MATCH:" $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
		if [ "$SUDO_USER" == "$U" -o "$USER" == "$U" ] ; then
		    echo "Can not remove the password for the user you are logged in as."
		    echo "So you have to do that manually by typing:"
		    echo "	usermod -p X $U"
		else
		    echo "Updating password for user $U (no shell access)."
		    usermod -p X $U
		fi
	    fi
	fi
    else
	# User should not have shell access.
	if ! grep "^$U:x:" $SHDF > /dev/null 2>&1 ; then
	    if [ "$SUDO_USER" == "$U" -o "$USER" == "$U" ] ; then
		echo "Can not remove the password for the user you are logged in as."
		echo "So you have to do that manually by typing:"
		echo "	usermod -p x $U"
	    else
		echo "Updating password for user $U (no password found)."
		usermod -p x $U
	    fi
	fi
    fi
    if [ ! -z "$SHELL" ] ; then
	if ! grep ":$SHELL\$" $PWDF > /dev/null 2>&1 ; then
	    usermod -s $SHELL $U
	fi
    fi
done
