#!/bin/ksh
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# bos720 src/bos/usr/sbin/sv-usercmds/listusers.sh 1.2 
#  
# Licensed Materials - Property of IBM 
#  
# Restricted Materials of IBM 
#  
# COPYRIGHT International Business Machines Corp. 2001,2006 
# All Rights Reserved 
#  
# US Government Users Restricted Rights - Use, duplication or 
# disclosure restricted by GSA ADP Schedule Contract with IBM Corp. 
#  
# IBM_PROLOG_END_TAG 
# ************************************************************************
# *  Program: /usr/sbin/listusers                                        *
# *  Purpose: To list the valid users of a system                        *
# ************************************************************************

export PATH="/usr/bin:/usr/sbin:$PATH"  #exporting AIX PATH followed by user's path for this script

function print_usage
   {
   USAGE=`/usr/bin/dspmsg -s 2 cmdsolsysmgt.cat 2 "Usage: listusers [ -g group1,group2 ... ] [ -l login1,login2 ... ]"`
   print "$USAGE"
   } # end of print_usage


# constants
SEPERATOR='     '							# Seperator to use between displayed fields
USERS_TO_SUPPRESS='root|bin|sys|adm|daemon|imnadm|lpd|uucp|ldap'	# The users to not show
RETURN=`print "\n\r"`

# default command, switches, and operands to run
command='/usr/sbin/lsuser'		
switches='-a gecos'							# show comments
operands='ALL'								# show these users
groupsToShow='ALL'							# show from these groups

# Process switches and operands
while getopts :l:g: arguments
    do
    case $arguments in 
       l) 	operands="$OPTARG";;					# set the users to list
       g) 	switches="-a groups gecos"				# list group attribute also, will be used for matching groups later
                groupsToShow="$OPTARG"		
                groupsToShow=` echo "${groupsToShow}" \
		   | sed 's/,/|/g' `;;    				# replace "," with "|" ('or' character) for use with awk
       :) 	print_usage						# print the usage statement
       		return "2";; 
       \?) 	print_usage						# print the usage statement
       		return "2";; 
    esac
    done
((positionsOccupiedBySwitches = OPTIND - 1))
shift $positionsOccupiedBySwitches					# remove positional parameters that are switches or switch arguments
if (($# > 0)) then 							# should be no operands for this command
   print_usage
   return "2"
fi


# Call lsuser command grabbing output
output=''
erroutput=''
OIFS="${IFS}"
IFS=","
for operand in $operands
   do
   IFS="${OIFS}"
   outputTemp=`$command $switches $operand`				# call to lsuser outputting error messages each time
   rc="$?"
   if [[ $rc = "0" ]] then						# call to lsuser command worked
      output="${output}${outputTemp}${RETURN}"
   fi
   IFS=","
   done
IFS="${OIFS}"
erroutput="${erroutput%%$RETURN}"					# remove last return
output="${output%$RETURN}"						# remove last return
   
# return if command failed
if [[ $output = "" ]] then						# no users were found
   return "$rc"	
fi					

# Reformat lsuser output to match look of listusers
if [[ $groupsToShow != 'ALL' ]] then
   output=` echo "${output}" \
          | awk '$2"," ~ '"/(groups=|,)($groupsToShow)"",/" `  		# only keep users that are in one of the groups specified
   output=` echo "${output}" | awk '{ $2="" ; print $0}' ` 		# remove "group=..." field
fi
output=` echo "${output}" | awk '$1 !~ '"/^$USERS_TO_SUPPRESS/" `	# remove certain users from the list
output=` echo "${output}" | sed "s/gecos=/$SEPERATOR/g" `    		# replace "gecos=" with the field seperator
output=` echo "${output}" | sort`					# sort the list of users


# Display reformated output
print "$output"


return "0"						
