#!/bin/bash
#---------------------------------------------------------------------
##
##=head1 SYNOPSIS
##
## Functions for dealing with account and group generation etc.
##
##=head1 DESCRIPTION
##
##=head1 COPYRIGHT
##
## Copyright (C) 2004 The Source Mage Team <http://www.sourcemage.org>
##
##=head1 FUNCTIONS
#---------------------------------------------------------------------

#---------------------------------------------------------------------
## @param system account
## @param [home directory]
## @param [shell]
##
## Creates account (if account has been defined).
## @return 0 if success (or account already exists).
## @return 1 if failed (or account has not been defined).
##
#---------------------------------------------------------------------
function create_account() {

        HOME_DIR=/var/run/$1
        if [ -n "$2" ]; then
                HOME_DIR="$2"
        fi

        USER_SHELL=/bin/false
        if [ -n "$3" ]; then
                USER_SHELL="$3"
        fi

  if ! exists_account "$1" ; then
    debug "libgrimoire" "create_account() - $1 not defined!"
    return 0  # should return 0 to gracefully continue casting.
  fi

  ACCOUNT_UID=`get_uid_for_account $1`
  PRIMARY_GID=`get_primary_gid_for_account $1`
  PRIMARY_GNAME=`get_group_name $PRIMARY_GID`

  debug "libgrimoire" "create_account() - $1, UID=$ACCOUNT_UID, GID=$PRIMARY_GID:$PRIMARY_GNAME, HOME=$HOME_DIR, SHELL=$USER_SHELL"

  groupadd -g $PRIMARY_GID -f $PRIMARY_GNAME

        # check for adding user problems and try to notify user.
        useradd -u $ACCOUNT_UID -g $PRIMARY_GNAME -G "$SECONDARY_GIDS" -d "$HOME_DIR" -s "$USER_SHELL" $1
        USERADD_RETURN=$?
        debug "libgrimoire.create_account()" "useradd return code was: $USERADD_RETURN"

  if    [  $USERADD_RETURN  ==  0  ] ; then
                message "${MESAGE_COLOR}The user id $ACCOUNT_UID created!${DEFAULT_COLOR}"
        elif  [  $USERADD_RETURN  ==  4  ] ; then
                message "${MESAGE_COLOR}The user id $ACCOUNT_UID already exists, so continuing...${DEFAULT_COLOR}"
                return 0
        elif  [  $USERADD_RETURN  ==  9  ] ; then
                message "${MESAGE_COLOR}The user name $1 already exists, so continuing...${DEFAULT_COLOR}"
                return 0
        else
                debug "libgrimoire.create_account()"  "useradd fails with strange code : $USERADD_RETURN"
                message "${MESAGE_COLOR}Something went wrong with adding the user $1 with uid $ACCOUNT_UID"
                message "and gid $PRIMARY_GNAME so going to stop here...${DEFAULT_COLOR}"
                return 1
        fi

}

#---------------------------------------------------------------------
## @param gid
##
## @Stdout group name
## returns that name assigned to a group id.
##
#---------------------------------------------------------------------
function get_group_name() {

  if  grep  -q  ":$1:"  $GROUP_LIST;  then
    grep ":$1:" $GROUP_LIST | cut -d : -f1
  fi

}


#---------------------------------------------------------------------
## @param system account
##
## @Stdout group ids
## Return list of group id's assigned to account name.
## All except primary
##
#---------------------------------------------------------------------
function get_gids_for_account() {

  if  grep  -q  "^$1:"  $ACCOUNT_LIST;  then
    ENTRY=`grep "^1:" $ACCOUNT_LIST`
    i=4
    while  [[  `echo $ENTRY | cut -d : -f$i` ]] ; do
     NEW=`echo $ENTRY | cut -d : -f$i`
     GROUPS="$GROUPS $NEW"
      let i++
    done
  fi
  return $GROUPS
}


#---------------------------------------------------------------------
## @param system account
## @return 0 if exists
## @return 1 if not
##
#---------------------------------------------------------------------
function exists_account() {

  grep  -q  "^$1:"  $ACCOUNT_LIST

}


#---------------------------------------------------------------------
## @param named system account
## @Stdout UID
## Outputs the UID for the named system account
##
#---------------------------------------------------------------------
function get_uid_for_account()  {

  if  grep  -q  "$1:"  $ACCOUNT_LIST;  then
    grep "^$1:" $ACCOUNT_LIST | cut -d : -f2
  fi

}

#---------------------------------------------------------------------
## @param named system account
## @Stdout GID
##
## Outputs the GID for the named system account
##
#---------------------------------------------------------------------
function get_primary_gid_for_account()  {

  if  grep  -q  "$1:"  $ACCOUNT_LIST;  then
    grep "^$1:" $ACCOUNT_LIST | cut -d : -f3
  fi

}


#---------------------------------------------------------------------
## @License
##
## This software 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 software 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 software; if not, write to the Free Software
## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
##
#---------------------------------------------------------------------

