#!/bin/bash
#

Usage() {
	echo
	echo "Parse results obtained by following STEPS.TXT"
	echo
	echo "USAGE: $0 [options]"
	echo
	echo "options:"
	echo
	echo "-v        .. turn on debug log msgs"
	echo
}

Debug=0

if [ $# -ne 0 ]; then
	while [ ! -z "$1" ]; do
		case "$1" in
			-v ) Debug=1;;
			-h ) Usage; exit 0;; 
			* ) echo "ERR: wrong option"; Usage; exit 1;; 
		esac
		shift
	done
fi

echo "# Generated on $(date +"%Y/%m/%d %H:%M:%S")"
echo "#USER GROUP UID GID HOME SHELL DESCRIPTION PACKAGE"

while read Input; do

	if [ "$Input" == "--" ]; then
		continue
	fi

	[ $Debug -eq 1 ] && echo "--------------------------------------------------------------------------------"
	[ $Debug -eq 1 ] && echo "INPUT: $Input"

	# TODO usermod/newusers not supported yet
	if [[ "$Input" == *"usermod"* ]] || [[ "$Input" == *"newusers"* ]]; then
		continue
	fi

	User=
	Group=
	Uid=
	Gid=
	HomeDir=
	Shell=
	Comment=

	if [[ "$Input" == *".rpm"* ]]; then
		RpmFileName=$Input
		[ $Debug -eq 1 ] && echo "RPM: $RpmFileName"
		continue
	fi

	if [[ "$Input" == *"groupadd"* ]]; then
		SkipNextOne=0   
		NextOneIsGid=0
		Cmd=( $Input )
		for i in "${Cmd[@]}"; do
			i="${i%\"}"
			i="${i#\"}"
			[ $Debug -eq 1 ] && echo $i
			if [ $SkipNextOne -eq 1 ]; then
				SkipNextOne=0
				continue
			fi
			if [ $NextOneIsGid -eq 1 ]; then
				Gid=$i
				NextOneIsGid=0
				continue
			fi
			case "$i" in
				*groupadd ) continue;;
				-r | --system ) continue;;
				-g | --gid ) NextOneIsGid=1;;
				-f | --force ) continue;;
				-K | --key ) SkipNextOne=1; continue;;
				-o | --non-unique ) continue;;
				-p | --password ) SkipNextOne=1; continue;;
				-R | --root ) SkipNextOne=1; continue;;
				-P | --prefix ) SkipNextOne=1; continue;;
				# Not on RHEL 8
				-U | --users ) continue;;
				* ) Group=$i; continue;;
			esac
		done
		[ $Debug -eq 1 ] && echo "RESULT: $Group ${Gid:--}"
		[ $Debug -eq 1 ] && echo "#USER GROUP UID GID HOME SHELL DESCRIPTION PACKAGE"
		echo "${User:--} ${Group:--} ${Uid:--} ${Gid:--} ${HomeDir:--} ${Shell:--} ${Comment:--} $RpmFileName"
		Group=
		Gid=
		continue
	fi

	if [[ "$Input" == *"useradd"* ]] || [[ "$Input" == *"adduser"* ]]; then
		SkipNextOne=0
		NextOneIsHomeDir=0
		NextOneIsGID=0
		NextOneIsUserGroup=0
		NextOneIsSuplGroups=0
		NextOneIsShell=0
		NextOneIsUID=0
		GroupIsTheSameAsUser=0
		# Handle -c | --comment first as it needs to reatain ""
		if [[ "$Input" == *"-c"* ]] || [[ "$Input" == *"--comment"* ]]; then
			Comment=`echo "$Input" | sed -e 's#^.*\"\(-c\|--comment\)\" \(\"[a-zA-Z0-9 '\'''\-''\/']\+\"\).*$#\2#'`
			Input=`echo "$Input" | sed -e 's#\(^.*\)\"\(-c\|--comment\)\" \"[a-zA-Z0-9 '\'''\-''\/']\+\" \(.*\)$#\1\3#'`
		else
			Comment=
		fi
		#echo INPUT: $Input
		#echo "COMMENT: ${Comment:--}"
		Cmd=( $Input )
		for i in "${Cmd[@]}"; do
			i="${i%\"}"
			i="${i#\"}"
			[ $Debug -eq 1 ] && echo $i
			if [ $SkipNextOne -eq 1 ]; then
				SkipNextOne=0
				continue
			fi
			if [ $NextOneIsHomeDir -eq 1 ]; then
				HomeDir=$i
				NextOneIsHomeDir=0
				continue
			fi
			if [ $NextOneIsGID -eq 1 ]; then
				Gid=$i
				NextOneIsGID=0
				continue
			fi
			if [ $NextOneIsSuplGroups -eq 1 ]; then
				SuplGroups=$i
				NextOneIsSuplGroups=0
				continue
			fi
			if [ $NextOneIsShell -eq 1 ]; then
				Shell=$i
				NextOneIsShell=0
				continue
			fi
			if [ $NextOneIsUID -eq 1 ]; then
				Uid=$i
				NextOneIsUID=0
				continue
			fi
			case "$i" in
				*useradd | *adduser ) continue;;
				--badname ) continue;;
				-b | --base-dir ) continue;;
				# -c | --comment handled already
				-d | --home-dir ) NextOneIsHomeDir=1; continue;;
				-D | --defaults ) continue;;
				-e | --expiredate ) ;;&
				-f | --inactive)
					SkipNextOne=1; continue;;
				-g | --gid) NextOneIsGID=1; continue;;
				-G | --groups) NextOneIsSuplGroups=1; continue;;
				-k | --skel ) ;;&
				-K | --key )
					SkipNextOne=1; continue;;
				-l | --no-log-init ) ;;&
				-m | --create-home ) ;;&
				-M | --no-create-home ) ;;&
				-N | --no-user-group ) ;;&
				-o | --non-unique )
					continue;;
				-p | --password ) SkipNextOne=1; continue;;
				-r | --system ) continue;;
				-R | --root ) ;;&
				-P | --prefix )
					continue;;
				-s | --shell ) NextOneIsShell=1; continue;;
				-u | --uid ) NextOneIsUID=1; continue;;
				-U | --user-group ) GroupIsTheSameAsUser=1; continue;;
				-Z | --selinux-user ) SkipNextOne=1; continue;;
				* ) User=$i; continue;;
			esac
		done
		[ $Debug -eq 1 ] && echo "RESULT: $User ${Uid:--} ${Gid:--}"
		if [ $GroupIsTheSameAsUser -eq 1 ]; then
			Gid=$User
			GroupIsTheSameAsUser=0
		fi
		[ $Debug -eq 1 ] && echo "#USER GROUP UID GID HOME SHELL DESCRIPTION PACKAGE"
		echo "${User:--} ${Group:--} ${Uid:--} ${Gid:--} ${HomeDir:--} ${Shell:--} ${Comment:--} $RpmFileName"
	fi
done

