#!/bin/bash

#To get the list of HSC users from /etc/passwd file
#The UserMObject.java will call this script

if [ "$1" == "" ]
then
  echo Usage: getUserList option
  echo        option    -list to list all the HSC users
  exit 1
fi

osname=`uname`

if [ "$1" == "-list" ]
then
USER=""
for i in hmc
do

   #to get the group id for HSC Groups
   GROUP_ID=`grep $i /etc/group | cut -f 3 -d:`
   #echo $GROUP_ID
   #to get the user list for the given group id
   GROUP_ID_SEARCH=$GROUP_ID
   if [ "$GROUP_ID_SEARCH" == "" ]
   then
      continue
   fi

   USER_NAME=`grep $GROUP_ID_SEARCH /etc/passwd | cut -f 1 -d:`
   #echo user$USER_NAME
   for m in $USER_NAME
   do
        if [ "$m" == "" ]
	then
	   continue
	fi
 	if [ "$m" == "invscout" ]
	then
	   continue
	fi
 	if [ "$m" == "ccfw" ]
	then
	   continue
	fi

	
   	GROUP_NAME=`/usr/bin/id -gn $m`
   	#echo groupname$GROUP_NAME
	if [ "$GROUP_NAME" == "$i" ] 
       	then
            USER_COUNT=`grep -c -F -w $m:x: /etc/passwd`
            if [ "$USER_COUNT" != "0" -a "$osname" == "AIX" ]
            then
              USER_COUNT=`grep -c -F -w $m:!: /etc/passwd`
            fi
            if [ "$USER_COUNT" == "1" ]
            then
              USER_ID_USER=`grep -F -w $m:x: /etc/passwd | cut -f 3 -d:`
              if [ "$USER_ID_USER" == "" -a "$osname" == "AIX" ]
              then
                USER_ID_USER=`grep -F -w $m:!: /etc/passwd | cut -f 3 -d:`
              fi
            fi

            if [ "$USER_COUNT" != "1" ]
            then

              USER_USER=`grep -F -w $m:x /etc/passwd | cut -f 3 -d:`
              if [ "$USER_USER" == "" -a "$osname" == "AIX" ]
              then
                USER_USER=`grep -F -w $m:! /etc/passwd | cut -f 3 -d:`
              fi

              for d in $USER_USER
              do

                USER_ID_NAME=`grep -F -w $m:x:$d /etc/passwd | cut -f 1 -d:`
                if [ "$USER_ID_NAME" == "" -a "$osname" == "AIX" ]
                then
                  USER_ID_NAME=`grep -F -w $m:x:$d /etc/passwd | cut -f 1 -d:`
                fi
                if [ "$USER_ID_NAME" == "$m" ]
                then
                    USER_ID_USER=$d
                    break;
                fi
              done
             fi

	     #USER_ID_USER=`grep -w $m:x /etc/passwd | cut -f 3 -d:`
      	     GROUP_USER=`grep -F $m:x:$USER_ID_USER /etc/passwd | cut -f 1,2,3,5 -d:`
             if [ "$GROUP_USER" == "" -a "$osname" == "AIX" ]
             then
      	       GROUP_USER=`grep -F $m:!:$USER_ID_USER /etc/passwd | cut -f 1,2,3,5 -d:`
             fi
    	     USER="$USER$GROUP_USER,"
         fi 
   done 	
done
ROOT_USER="root:x:0:root,"
if [ "$osname" == "AIX" ]
then
  ROOT_USER="root:!:0:root,"
fi
USER="$USER$ROOT_USER"
echo $USER
fi


if [ "$1" == "-properties" ]
then
	if [ "$2" != "" ]
        then
           result=`grep :x:$2: /etc/passwd | cut -f 1,3,5 -d:`	  
           if [ "$result" == "" -a "$osname" == "AIX" ]
           then
             result=`grep :!:$2: /etc/passwd | cut -f 1,3,5 -d:`	  
           fi
           echo $result
        fi
fi

if [ "$1" == "-Role" ]
then
     	if [ "$2" != "" ]
        then
           echo `cat /opt/hsc/data/RoleRegistration.db | cut -f 1,2 -d:`        
        fi

fi

if [ "$1" == "-userroles" ]
then
    USER=`grep :x:$2: /etc/passwd | cut -f 1 -d:`
    if [ "$USER" == "" -a "$osname" == "AIX" ]
    then
      USER=`grep :!:$2: /etc/passwd | cut -f 1 -d:`
    fi
    GROUP=`grep -F -w $USER /etc/group |cut -f 1 -d:`
    DESCRIPTION=`cat /opt/hsc/data/RoleRegistration.db | cut -f 2 -d:`
    #DESCRIPTION="System Administrator,Service Representative,Advanced Operator,Operator,User Administrator"
    PROPERTIES=`grep :x:$2: /etc/passwd | cut -f 1,3,5 -d:`
    if [ "$PROPERTIES" == "" -a "$osname" == "AIX" ]
    then
      PROPERTIES=`grep :!:$2: /etc/passwd | cut -f 1,3,5 -d:`
    fi
    echo "$GROUP~$DESCRIPTION~$PROPERTIES"

fi

if [ "$1" == "-userdelete" ]
then
    USER=`grep :x:$2: /etc/passwd | cut -f 1 -d:`
    if [ "$USER" == "" -a "$osname" == "AIX" ]
    then
      USER=`grep :!:$2: /etc/passwd | cut -f 1 -d:`
    fi
    GROUP=`grep -F $USER /etc/group |cut -f 1 -d:`
    echo $GROUP
fi
