#!/bin/ksh
#
#
ARGV="$@"


Number="$#"
OS=$(uname)
Root=@@ServerRoot@@


#Set Path for "groupadd and adduser" in Linux
if [ $OS = "Linux" ]; then
	export PATH=/usr/sbin:$PATH
fi

#Set Path for SunOS in order to use "grep" command
if [ $OS = "SunOS" ]; then
	PATH=/usr/xpg4/bin:$PATH
	export PATH
	if   echo "$1" | grep -iE  "help|-h|-help" >/dev/null    ; then
		HELPDISPLAY=1
	else HELPDISPLAY=0
	fi
elif [ $OS = "AIX" ]||[ $OS = "Linux" ]||[ $OS = "HP-UX" ]; then
	if   echo "$1" | grep -iE  "help|-h|-help" >/dev/null    ; then
		HELPDISPLAY=1
	else HELPDISPLAY=0
	fi
fi
	
##HELP

function helpdisplay {

## Display help only for  non silent execution 

if [ silent -eq 0 ] ; then
	echo "usage: $0 (no arguments)/n"
	cat <<EOF | more


EXECUTION MODE:

        COMMAND LINE Arguments:
                Run this script with command line arguments:
                -usr <userName> -grp <groupname> -cfg <IHS web server configfileName>  
                -adm <IHS admin server configfilename> -plg <plugin-cfg.xml>(optional)
ARGUMENTS:


        -silent:
                This allows this script to run without any messsage text.  
        -create:
 		This  parameter  indicates that we will attempt to create 
		a User and Group. You must be running as Root.
        -usr: (Required)
        	This UserID will be used to run the Administration Server.
		This UserID will be updated in the <User> directive of the 
		Administration Server configuration file (admin.conf).

        -grp: (Required)
                This Group Name will be used to run the Administration Server.
                The Group Name will be used to change the file Permissions for
                the configuration files and User or Group Authentication files.
                This Group Name  will be updated in the <Group> directive 
                of the Administration Server configuration file (admin.conf). 
                We recommend a unique Group Name for Administration Server.

         -cfg: (Required)
                Fully qualified File name of the IHS webserver configuration
		file.  This is the file name for which the permission and 
                 group will be changed. The Administration Server requires read 
                and write access to IHS configuration files.

         -plg: (Optional)
                This is the fully qualifed file name for the plugin-cfg.xml
                for which file permissions will be changed.
                
         -adm: (Required)
                Fully qualified filename for the 
                Administration server configuration file. If no parameter 
                is supplied a default administration configuration file 
                will be used based on the installRoot of the webserver and
		default path for the admin.conf (<InstallRoot>conf/admin.conf)

	PROCESS
	        -Create a UserID and Group Name using the UserID and
           	     Group Name input.
	        -Change the Group Name associated with the configuration file 
			to the Group Name specified on the input. 
		-Update the Administration Server Configuration file
                 	(admin.conf). The Administration server configuration
                 	file is expected to be found in <ServerRoot/conf>.
               	        A backup file will be created for the admin.conf each 
               	        time you execute this script.

EOF
fi
	return

}

##Functions for update of the admin.conf file 
function endProc  {  
    if [ silent -eq 1 ] ; then
      exit $retcde  
    else
      case $retcde  in
  	60) 
          echo "Using existing UserID: '$UserID' RC(60) "
 	;;
  	62) 
          	echo "You did NOT supply required parameter <-usr> UserID RC(62)"
	;;
  	64) 
		echo ">>>UserID '$UserID' does NOT exist and  option <-create> was not specified>>> RC(64)"
	;;
 	68) 
		echo ">>>Error Invalid UserID: '$UserID'<<< RC(68)"
	;;
  	50) 
          echo "Using existing Group: '$GroupN' RC(50) "
 	;;
  	52) 
          	echo "You did NOT supply required parameter <-grp> Group Name RC(52)"
	;;
  	54) 
		echo ">>>Group '$GroupN' does NOT exist and  option <-create> was not specified>>> RC(54)"
	;;
  	58)
		echo ">>>Error Invalid Group Name: '$GroupN'<<< RC(58) "
	;;
  	70) echo "Last complete return code RC(70)" 
	;;
  	72) 
          	echo "You did NOT supply  required parameter <-cfg> configuration file RC(72)"
	;;
  	78) 
          	echo "Parameter <-cfg>, file name '$cfgfile' does not exist RC(78)"
	;;
  	7C) 
       	      echo "Command: 'chgrp $GroupN $cfgfile' failed RC(7C)"
	;;
  	7F) 
       	         echo "Command: 'chmod g+rw $cfgfile' failed RC(7F)"
	;;
  	80) echo  "Last completed return code RC(80)"
	;;
  	82) 
## 		Not an error condition
##          	echo "You did NOT supply a pluign-cfg.xml filename"
	;;
  	88) 
          	echo "Parameter <-plg>, file name '$plgfile' does not exist RC(88)"
	;;
  	8C) 
       	      echo "Command: 'chgrp $GroupN $plgfile' failed RC(8C)"
	;;
  	8F) 
       	         echo "Command: 'chmod g+rw $plgfile' failed RC(8F)"
	;;
  	90) echo  "Script Completed RC(90)"
	;;
  	92) 
          	echo "You did NOT supply required parameter <-adm> Administration Server Configuration filename RC(92)"
	;;
  	98) 
          	echo "Parameter <-adm>, file name '$admfile' does not exist RC(98)"
	;;
  	9C) 
          	echo "Admin File '$admfile' is not writable RC(9C)"
	;;
  	9F)
		echo "Update failed for Administration Server Configuration file directive 'User' and 'Group' RC(9F)"	
	;;
  	99) 
     		echo  "Comand '$cmd' "
   		echo  "Error on Option  '$option'"
     		echo  "Syntax error: '$ARGV'"
     		echo
     		helpdisplay
	;;
  	FF) 
     		echo  "End of Help"
	;;

  	*) echo "return code '$retcde'" 
     	;;
      esac

    fi
    exit $retcde  
   
}


##Functions for update of the admin.conf file 
function updadmfile  {  
    
          if [ ! -f $admfile ] ; then
                retcde=98
                return 98
    	  elif [  ! -w $admfile ] ; then
                retcde=9C
                return 9C
          fi 

          adminnew=$admfile.`date -u +"%T_%j"`
          tempfile1=$admfile"_1".`date -u +"%T_%j"`
          tempfile2=$admfile"_2".`date -u +"%T_%j"`
         

     	  if sed "s/User .*$/User $UserID/" $admfile > $tempfile1 ; then
     	      if sed "s/Group .*$/Group $GroupN/" $tempfile1 > $tempfile2 ; then
  		cp $admfile $adminnew
  		cp $tempfile2  $admfile
  		rm $tempfile1 
  		rm $tempfile2
                retcde=90
                return 90
	      else	
                retcde=9F
                return 9F
              fi 
	  else	
                retcde=9F
                return 9F
          fi 

 } 

## function for  changing file permission
function chgcfgfile  {  

          if [ ! -f $cfgfile ] ; then
                retcde=78
                return 78
          fi 

       	  if chgrp  $GroupN $cfgfile ; then
  	       if chmod g+rw $cfgfile ; then
       	         retcde=70
       	         return 70
               else
       	         retcde=7F
       	         return 7F
               fi
          else 
       	      retcde=7C
       	      return 7C
          fi
 		
}

function chgplgfile  {  
	## Change Group and permissions for the plugin-cfg.xml
          if [ ! -f $plgfile ] ; then
                retcde=88
                return 88
          fi 
       	  if chgrp  $GroupN $plgfile ; then
  	       if chmod g+rw $plgfile ; then
       	         retcde=80
       	         return 80
               else
       	         retcde=8F
       	         return 8F
               fi
          else 
       	      retcde=8C
       	      return 8C
          fi
 		
}


##Functions used for group processing

##Check User exists
function chkUser {
     temp=`awk " /^$UserID:/ " /etc/passwd`
     if  [ "x$temp" != x"" ]  || [  "x$temp" != "x" ] ; then
          retcde=60
	  return 60
     else
          ## User does NOT exist    
          retcde=64
	  return 64
     fi
}

##Create User PROCESSING
function addUser {
  if [ $OS = "AIX" ]; then
	if mkuser login=false rlogin=false home=none groups=$GroupN $UserID 2>/dev/error ; then
          	retcde=60
	  	return 60
	else	
          	retcde=68
	  	return 68
	fi

  elif [ $OS = "SunOS" ]; then
  	if useradd -s /bin/false -g $GroupN $UserID    2>/dev/null ; then
          	retcde=60
	  	return 60
       	else
          	retcde=68
	  	return 68
	fi
  elif [ $OS = "Linux" ]; then
  	if useradd -s /bin/false -g $GroupN $UserID    2>/dev/null ; then
          	retcde=60
	  	return 60
       	else
          	retcde=68
	  	return 68
	fi
  elif [ $OS = "HP-UX" ]; then
  	if useradd -s /bin/false -g $GroupN $UserID    2>/dev/null ; then
          	retcde=60
	  	return 60
       	else
          	retcde=68
	  	return 68
        fi
  fi
}

##Check GROUP exists
function chkGroup {

         ## check to see if the Group Exists 
         temp=`awk " /^$GroupN:/ " /etc/group`
         if  [ "x$temp" != x"" ]  || [  "x$temp" != "x" ] ; then
                ## Group already exist
		retcde=50
		return 50
         else
                ## Group does NOT exist    
		retcde=54
		return 54
         fi

}

##Create GROUP PROCESSING
function addGroup {

		   if [ $OS = "AIX" ]; then
			addGroupAIX
		   elif [ $OS = "SunOS" ]; then
			addGroupSOLARIS
		   elif [ $OS = "Linux" ]; then
			addGroupLINUX
		   elif [ $OS = "HP-UX" ]; then
			addGroupHPUX
	
		   fi

}

##
function addGroupAIX {
	if mkgroup $GroupN 2>/dev/null ; then
                return 50
        else
                return 58
        fi
}
function addGroupSOLARIS {
	if groupadd $GroupN 2>/dev/null ; then
                return 50
        else
                return 58
        fi
}
function addGroupLINUX {

#Note that Caldera openLinux requires a GID praram to groupadd
##Choose GID < 65500
## Check for OpenLinux
##	if [ -d /usr/src/openLinux ]; then
##		GroupNum=`awk -F: '{if ($3 > x && $3 < 65500) x = $3} END {print (x+1)}' /etc/group`	
##                echo "OpenLinux"
##        fi

	if groupadd $GroupN 2>/dev/null ; then
               	return 50
       	else
               	return 58
	fi
}
function addGroupHPUX {
	if groupadd $GroupN 2>/dev/null ; then
               	return 50
        else
               	return 58
        fi
}

##End of functions  used for Group Processing

## Start of function for  other processing
## start logic

create=0
silent=0
retcde=0

cmd=$0


##  No input show helpDisplay and END
if [ "$Number" = 0 ] ; then
   helpdisplay
   retcde=FF
   endProc
fi

while [ "$1" != "" ]
do
  case $1
  in
  -h|-help|-hlp|/?)
     	helpdisplay
        retcde=FF
     	endProc
	;;
  -silent)
        silent=1
        option=-silent
	shift;;
  -create) 
        create=1
        option=-create
	shift;;
  -usr) 
        UserID=$2
        option=-usr
	shift 2 
        ;;
  -grp) 
        GroupN=$2
        option=-grp
	shift 2 ;;
  -cfg)
        cfgfile=$2
        option=-cfg
	shift 2 ;;
  -plg)
        plgfile=$2
        option=-plg
	shift 2 ;;
  -adm)
        admfile=$2
        option=-adm
	shift 2 ;;

      *) 
     option=$1 
     retcde=99
     endProc
  esac


done


## Check input options in case of NON-silent mode

if [ silent -eq 0 ] ; then
	echo "$ARGV"
fi
        if  [ "x$UserID" = x"" ]  || [  "x$UserID" = "x" ] ; then
                retcde=62
          	endProc
        fi

        if  [ "x$GroupN" = x"" ]  || [  "x$GroupN" = "x" ] ; then
                retcde=52
          	endProc
	fi

        if  [ "x$cfgfile" = x"" ]  || [  "x$cfgfile" = "x" ] ; then
                retcde=72
          	endProc
        elif [ ! -f $cfgfile ] ; then
                retcde=78
          	endProc
	fi

##        if  [ "x$plgfile" = x"" ]  || [  "x$plgfile" = "x" ] ; then
##                continue
##                retcde=82
##          	endProc
##        elif [ ! -f $plgfile ] ; then
##                retcde=88
##          	endProc
##	  fi

        if  [ "x$admfile" = x"" ]  || [  "x$admfile" = "x" ] ; then
                retcde=92
          	endProc
        elif [ ! -f $admfile ] ; then
                retcde=98
          	endProc
	fi

 
## Begining of logic 


	if  [  create -eq 1  ] ; then
	    ## check if Group exsists before creating group
            ## Add Group only if  -create option used 
            chkGroup 
            if [ $? -ne 50 ] ; then   
            	addGroup 
            	if [ $? -ne 50 ] ; then   
              		endProc
		fi
            fi
	    ## check if User exists before creating User
            ## Add User only if  -create option used 
            chkUser 
            if [ $? -ne 60 ] ; then   
            	addUser 
            	if [ $? -ne 60 ] ; then   
              		endProc
		fi
 		
            fi
	else
           ## check if Group exsists before file permission change
           ## Group MUST exist because the option  create is no specified 
	   chkGroup 
           if [ $? -ne 50 ] ; then   
              endProc
           fi
           ## check if Group exsists before file permission change
           ## Group MUST exist because the option  create is no specified 
	   chkUser 
           if [ $? -ne 60 ] ; then   
              endProc
           fi
	fi
	
	## Continue process with User and Group created and verified
	chgcfgfile
        if [ $? -gt 70 ] ; then  
              endProc
        elif [ $? -eq 70 ] ; then
              endProc
        fi   

        if  [ "x$plgfile" != x"" ]  || [  "x$plgfile" != "x" ] ; then
		chgplgfile
        	if [ $? -gt 80 ] ; then  
              		endProc
        	elif [ $? -eq 80 ] ; then
              		endProc
        	fi   
	fi

        updadmfile
        endProc




