#!/bin/sh
# vim:set sw=4 ts=4:
#
# $Id: make_groups,v 1.17 2005/04/11 14:14:39 fabian Exp $
#
#############################################################################
#
# ALICE
# Automatic Linux Installation and Configuration Environment
#
# Copyright (c) 2000-2002 SuSE Linux Solutions AG, Eschborn, Germany
#               2002-2004 SuSE Linux AG, Eschborn, Germany
#               2005      SUSE GmbH, Nuernberg, Germany
#
# 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.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
#############################################################################
#
# Author: Fabian Herschel
#
############################################################################
#
# make_groups
# configures the user groups
#
# Sections: groups (move to accounts?)
# Tags:     GROUPLIST, DEF_GRP_*
#
#
usage()
{
   echo "$_self -h | --help | -?"
   echo "$_self -fqhn"
}

# General Alice environment stuff
test -n "$alice_dir" || alice_dir="/usr/lib/alice2/"
export alice_dir


while [ $# -gt 0 ]
do
   case $1 in
       -fqhn ) export fqhn=$2; shift
            ;;
       -h | --help | -? ) usage
                          exit 1
                          ;;
       -gc ) groups_created="yes" # internal use only
             ;;
   esac
   shift
done

if test -f ${alice_dir}/lib/alicerc ; then . ${alice_dir}/lib/alicerc ; fi
#
# Welcome to the show
#
WELCOME

#
# Fetch defaults and list of groups to add
#
GET_CONFIGURATION groups $CFG_DEBUG
if [ -z "$GROUPLIST" ]
then
   GOOD_BYE NOCFG groups
fi
#
# group_file=$(get_config_file groups.conf) || GOOD_BYE NOCFG groups.conf
# . $group_file
#
BACKUP /etc/group

#
# Make sure we run as "root", otherwise we cant add accounts
#
if [ "$(whoami)" != "root" ] 
then 
   FATAL ROOTONLY
fi

#
# The function that does the dirty work... add a group
#
function add_a_group()
{
	#
	# add_a_group groupid groupname
        # 	
	typeset mygid mygroupname command
	mygid=$1
	mygroupname=$2

	command="groupadd -g \"$mygid\" $mygroupname"

 	eval ${command}
}

#
# On our second run we dont create new groups, but add users
#
function add_users_to_group()
{
	#
	# add_users_to_group groupname groupmember [groupmember ...]
	#
	typeset mygroupname mygroupmembers currentmember command
	mygroupname=$1
	mygroupmembers=$2
	for currentmember in $mygroupmembers
	do
	    othergroups="$(id -Gn $currentmember | tr " " "," ),$mygroupname"
	    usermod -G $othergroups $currentmember
	done
}

#
# The main function parses the .groups.conf file sourced above
#
if [ -z "$groups_created" ]
then
	export groups_created=yes

	typeset -i i=0
	echo "${GROUPLIST}" |
	(
		typeset -i mygid 
		while IFS=: read mygroupname mygid mygroupmembers
		do
			if test -n "$mygroupname"
			then
	                   if [ $mygid -eq 0 ] 
		           then 
				mygid=${DEF_GRP_STARTID}+$i
				i=$i+1
			   fi
			   add_a_group  "$mygid" "$mygroupname" 
			fi
		done
	)
else
	echo "${GROUPLIST}" |
	(
		while IFS=: read mygroupname mygid mygroupmembers
		do
			if test -n "$mygroupmembers" 
			then 
				add_users_to_group "$mygroupname" \
                                   "$(echo $mygroupmembers| tr ',' ' ')"
			fi			
		done
	)
fi

#
# Thank you and good Night!
#
GOOD_BYE 
