#!/bin/bash
# vim:set sw=4 ts=4:
# 
# $Id: make_users,v 1.28 2005/04/11 14:14:39 fabian Exp $
# vim:set sw=4
#
#############################################################################
#
# 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: Pieter Hollants
#
#############################################################################
#
# make_users
# configures the users of the system
#
# Sections:	users (change that to accounts?)
# Tags:         USERLIST, USERDELLIST, DEF_USER_*
#
usage()
{
   (
      echo "$_self -h | --help | -?"
      echo "$_self -fqhn fqhn"
   ) >&2
}

#
# 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
                          ;;
   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 users to add
#
#config_file=$(get_config_file users.conf) || GOOD_BYE
#. $config_file
#
GET_CONFIGURATION users $CFG_DEBUG
if [ -z "${USERLIST}" -a -z "${USERDELLIST}" ]
then
   GOOD_BYE
fi
BACKUP /etc/passwd /etc/shadow

#
# 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 user
#
function add_a_user()
{
    # Set default values, if need be
    [[ -z "${mypasswd}" ]] && mypasswd="${DEF_USR_PASSWD}"
    [[ -z "${mycmt}" ]]    && mycmt="${DEF_USR_COMMENT}"
    [[ -z "${myhome}" ]]   && myhome="${DEF_USR_HOMEDIR}/${mylogname}"
    [[ ${myuid} -eq 0 ]]   && { let myuid=${DEF_USR_STARTID}+${i}; let i++; }
    [[ ${mygid} -eq 0 ]]   && mygid=${DEF_USR_GROUPID}
    [[ -z "${mysh}" ]]     && mysh="${DEF_USR_SHELL}"

    command="useradd -c \"${mycmt}\" -d \"${myhome}\" -m -g ${mygid} -u ${myuid}"
    command="${command} -p \"${mypasswd}\" -s \"${mysh}\" \"${mylogname}\""

    eval ${command}
}

if [ -n "${USERDELLIST}" ]
then
	echo "${USERDELLIST}" | while read account; do
		userdel -r $account
	done
fi

#
# The main function parses the .users.conf file sources above
#
if [ -n "${USERLIST}" ]
then
    typeset -i i=0
    echo "${USERLIST}" |
    (
	IFS=: 
	typeset -i myuid mygid
	while read mylogname mypasswd myuid mygid mycmt myhome mysh
	do
	    case "${mylogname}" in
		"")	# skip empty names
		;;

		+*)	# NIS users/netgroups -- but only if not there already
		    nis="${mylogname}:${mypasswd}:::${mycmt}:${myhome}:${mysh}"
		    if ! egrep -q "^${nis}" /etc/passwd
		    then
			echo "${nis}" >> /etc/passwd
		    fi
		;;

		*)	# local users
		    add_a_user
		;;
	    esac
	done
    )
fi

#
# Thank you and good Night!
#
GOOD_BYE 
