#!/bin/sh
#
#
#
#
# Nasir Mehmood (nasir.mehmood@expertflow.com)
# Saturday 03-06-2023 @ 19:10 
# For Expertflow 
#

echo "Authenticating to local Keycloak Server at http://localhost:8080/auth"

kcadm.sh config credentials --config /tmp/.kcadmin.config --server http://localhost:8080/auth --realm master --user admin --password admin
if [[ $? == 0 ]]
then
	echo "checking file in /tmp/"
	if [[ -f /tmp/realm-export.json ]]
	then 
		echo "processing /tmp/realm-export.json"
		cat  /tmp/realm-export.json | kcadm.sh  create realms --config /tmp/.kcadmin.config -f -
		if [[ $? == 0 ]]
		then 
			echo "Realm created successfully"
			echo "Creating admin User "
			kcadm.sh create  --config /tmp/.kcadmin.config  users -r expertflow  -s username=admin  -s enabled=true
			[[ $? == 0 ]] && echo "Admin User Created Successfully" || exit $$
			kcadm.sh create  --config /tmp/.kcadmin.config  users -r expertflow  -s username=agent   -s enabled=true 
			[[ $? == 0 ]] && echo "Admin User Created Successfully" || exit $$
			echo "assigning admin role to admin user"
			kcadm.sh add-roles --config /tmp/.kcadmin.config  -r expertflow  --uusername admin   --rolename admin
			[[ $? == 0 ]] && echo "admin role assigned to admin user" || echo "Could not assign admin role to admin user"
			echo "assigning agent role to agent user"
			kcadm.sh add-roles --config /tmp/.kcadmin.config  -r expertflow  --uusername agent  --rolename agent
			[[ $? == 0 ]] && echo "agent  role assigned to agent  user" || echo "Could not assign agent  role to agent  user"
			echo "assigning realm-management client roles to the admin user"
			kcadm.sh get-roles --config /tmp/.kcadmin.config  -r expertflow  --cclientid realm-management -o --fields name |&  sed -e 's/[^-a-zA-Z0-9]/ /g' -e '/^[ ]*$/d' |& \
			while read client clientid; 
			do 
				echo $clientid; 
				kcadm.sh add-roles --config /tmp/.kcadmin.config  -r expertflow  --uusername admin  --cclientid realm-management --rolename $clientid
				[[ $? == 0 ]] && echo "$clientid assigned successfuly" && echo "there was a problem in assigning role $clientid to user admin"
				
			done
			echo "updating the  admin user"
			EF_SUPER_ADMIN_ID=$(kcadm.sh get users --config /tmp/.kcadmin.config  -r expertflow  -q username=admin -o --fields id |& sed -e 's/[^a-zA-Z0-9-]/ /g' -e '/^[ ]*$/d' -e 's/  */ /g'|cut -d " " -f 3)
			EF_AGENT_USER_ID=$(kcadm.sh get users --config /tmp/.kcadmin.config  -r expertflow  -q username=agent  -o --fields id |& sed -e 's/[^a-zA-Z0-9-]/ /g' -e '/^[ ]*$/d' -e 's/  */ /g'|cut -d " " -f 3)
			kcadm.sh update  --config /tmp/.kcadmin.config   users/${EF_SUPER_ADMIN_ID}  -r expertflow  -s enabled=true
			[[ $? == 0 ]] && echo "admin user updated succcessfully" || echo "Problem in updating the user admin"
			echo "updating the agent user"
			kcadm.sh update  --config /tmp/.kcadmin.config   users/${EF_AGENT_USER_ID}  -r expertflow  -s enabled=true
			[[ $? == 0 ]] && echo "agent user updated succcessfully" || echo "Problem in updating the user agent "
			echo "Assigning the password to admin user"
			kcadm.sh set-password --config /tmp/.kcadmin.config  -r expertflow  --username admin  --new-password admin
			[[ $? == 0 ]] && echo "admin user password updated succcessfully" || echo "Problem in updating password the user admin"
			kcadm.sh set-password --config /tmp/.kcadmin.config  -r expertflow  --username agent  --new-password agent 
			[[ $? == 0 ]] && echo "agent user password  updated succcessfully" || echo "Problem in updating password  the user agent"						
		else
			echo "Could not find the realm-export file in /tmp"
			echo "exiting "
			exit $$
		fi 


else
	echo "Could not authenicate successfully" 
	echo "Exiting ...."
	exit $$
fi 