#!/bin/bash

# DISCLAIMER   :   It is recomended to test this script on a test machine.
#                  ManageEngine will not be responsible for any
#                  damage/loss to the data/setup based on the behavior of the script.

# DESCRIPTION  :   Script to add user(s) in /etc/passwd to group in agent machines.
#
# ARGUMENT(S):
#
#  1) To add single user group
#
#       ARGUMENT FORMAT: <group_name> <user_name>
#       EXAMPLE        : mygroup testuser
#
#  2) To add multiple users group
#
#       ARGUMENT FORMAT: <group_name> <user_name_1> <user_name_2> ...
#       EXAMPLE        : mygroup user1 user2 user3

#           RETURN VALUE                  MEANING

#                0             User(s) added to group successfully
#                1             Error while adding user(s) to group
#                2             Invalid arguments.

errorCode=2

euid=$(id -u)
for i in 1; do
  #check sudo access
  if [ $euid -ne 0 ]; then
    echo "This script must be run as root"
    break
  fi

  #check arguments are valid
  if [ $# -lt 2 ]; then
    echo "Incorrect Usage : Arguments mismatch."
    echo "Refer ARGUMENT(S) section in the script."
    break
  fi

  errorCode=0
  groupName=$1

  #check Group name exist or not
  IsGrp=$(getent group | grep -c '^'$groupName':')
  if [ $IsGrp -eq 0 ]; then
    echo "Group : $groupName does not exist "
    break
  fi

  #shift used here starts arguments from $2
  #check username is exist or not
  shift
  for i in $@; do
    IsUser=$(grep -c '^'$i':' /etc/passwd)
    if [ $IsUser -eq 0 ]; then
      inValUser="$inValUser""$i"","
    else
      valUser="$valUser""$i"","
    fi
  done
  inValUser=$(echo $inValUser | sed 's/,$//') #remove last comma character
  valUser=$(echo $valUser | sed 's/,$//')     #remove last comma character

  #number of valid user
  NumofUser=$(expr $(echo $valUser | tr -cd , | wc -c) + 1)

  i=1
  while [ $NumofUser -ne 0 ]; do
    user=$(echo $valUser | awk -F',' '{ print $'$i' }')
    IsGrpUsr=$(getent group | grep -c -E '^'$groupName':.*'$user'$|^'$groupName':.*'$user',')
    if [ $IsGrpUsr -eq 1 ]; then
      oldUser="$oldUser""$user"","
    else
      newUser="$newUser""$user"","
    fi
    i=$(expr $i + 1)
    NumofUser=$(expr $NumofUser - 1)
  done

  oldUser=$(echo $oldUser | sed 's/,$//') #remove last comma character
  newUser=$(echo $newUser | sed 's/,$//') #remove last comma character

  #do not add existing user
  existUser=$(getent group | grep '^'$groupName':' | awk -F':' '{print $4}')
  valUser="$existUser"",""$newUser"

  valUser=$(echo $valUser | sed -e 's/,$//' -e 's/^,//')

  #adding only valid and new users to the group
  gpasswd -M $valUser $groupName

  if [ $? -eq 0 ]; then
    if [ $newUser ]; then
      echo "User(s) \"$newUser\" added to group \"$groupName\" successfully"
    fi
    if [ $oldUser ]; then
      echo "User(s) already present in group \"$groupName\" : $oldUser"
    fi
    if [ $inValUser ]; then
      echo "User(s) does not exist in /etc/passwd : $inValUser "
    fi
  else
    echo "Error while adding user(s) to group"
    errorCode=1
  fi
done

errorFunc() {
  return $errorCode
}
