#! /bin/sh
#----------------------------------------------------------------------------
# change-group - change group of a user
#
# Copyright (c) 2001-2003 Frank Meyer <frank@eisfair.org>
#
# Creation:	04.11.2001  fm
# Last Update:  20.07.2003  fm
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#----------------------------------------------------------------------------

clrhome
colecho "Change group of user" gn
echo
echo

echo -e "User: \c"
read user

IFS=':'

if [ "$user" != "" ]
then
    echo
    line=`grep "^$user:" /etc/passwd`

    if [ "$line" != "" ]
    then    
	set -- $line
	gid=$4
	homedir=$6
	echo "Changing main group of user $user..."
	echo
	echo -e "Old group: \c"

	while read line
	do
	    set -- $line
	    group="$1"
	    g="$3"

	    if [ $g = $gid ]
	    then
		echo $group
		break
	    fi
	done </etc/group

	echo
	echo -e "New group: \c"
	read new_group

	found_group=false

	if [ "$new_group" != "" ]
	then
	    while read line
	    do
		set -- $line
		g="$1"
		gid="$3"

		if [ $g = $new_group ]
		then
		    found_group=true
		    new_gid=$gid
		    break
		fi
	    done </etc/group

	    if [ $found_group = true ]
	    then
		while read line
		do
		    set -- $line
		    u="$1"
		    pass="$2"
		    uid="$3"
		    g="$4"
		    name="$5"
		    home="$6"
		    shell="$7"

		    if [ $u = $user ]
		    then
			echo "$u:$pass:$uid:$new_gid:$name:$home:$shell"
		    else
			echo "$u:$pass:$uid:$g:$name:$home:$shell"
		    fi
		done </etc/passwd >/tmp/passwd-$$

		cp /tmp/passwd-$$ /etc/passwd	# keep inodes and permissions
		rm -f /tmp/passwd-$$
		chgrp -R $new_group $homedir
	    else
		echo
		colecho "Group $group does not exist!" br x br
	    fi
	else
	    echo
	    colecho "Command aborted" br x br
	fi
    else
	echo
	colecho "User $user does not exist!" br x br
    fi
else
    echo
    colecho "Command aborted" br x br
fi
echo
/var/install/bin/anykey