#!/bin/bash
#------------------------------------------#
#     Change root password                 #
# Simple gui dialog root password changer  #
#------------------------------------------#

ICON="/usr/share/myrootpassword/icons/myrootpassword.png"
MICON="/usr/share/myrootpassword/icons/myrootpassword16.png"
EICON="/usr/share/myrootpassword/icons/warning.png"
ESICON="/usr/share/myrootpassword/icons/warning16.png"
OK="/usr/share/myrootpassword/icons/ok.svg"
CANCEL="/usr/share/myrootpassword/icons/cancel.svg"

if test -d /union ; then
$(yad --window-icon="$MICON" --image="$ICON" --title=$"Root Password" \
--width=400 --height=100 --fixed --center --borders=10 --on-top --button="Ok!$OK":0 \
--text=$"\nPlease run this program after installation!" )
exit 0	
else		
pass_dialog="root"
clean_log() {
	
	# clear the log of plain-text passwords
	if [ -n "$newpass" ]; then
		sed -i "s|$newpass|\*\*\*\*|"g $error_log
	fi
	
	if [ -n "$confirm_newpass" ]; then
		sed -i "s|$confirm_newpass|\*\*\*\*|"g $error_log
	fi
	newpass=""
	confirm_newpass=""
}

pass_error() {
	clean_log
	use_existing=""
	yad --window-icon="$ESICON" --title=$"Password Error" --center --borders=10 \
	--image="$EICON" --width=400 --height=100 --fixed --button="Close!$CANCEL":0 \
	--text=$"\nPasswords do not match!  Restart the dialog again."
exit 0	
}
pass_entry=$(yad --window-icon="$MICON" --form --title=$"Configure $pass_dialog password" \
--width=450 --height=125 --center --borders=10 --button="Ok!$OK":0 \
--text=$"Resetting the <span color=\"#e07b39\">$pass_dialog</span> password:            Use TAB to change fields." \
--field=$"Enter new <span color=\"#e07b39\">$pass_dialog</span> password::H" \
--field=$"Confirm new <span color=\"#e07b39\">$pass_dialog</span> password::H")  
newpass=$(echo $pass_entry|awk -F "|" '{print $1}')
confirm_newpass=$(echo $pass_entry|awk -F "|" '{print $2}')
use_existing=$(echo $pass_entry|awk -F "|" '{print $3}')

	if [ -n "$newpass" ] && [ "$use_existing" = "TRUE" ]; then
	pass_error

	fi

	if [ -z "$newpass" ] || [ "$newpass" != "$confirm_newpass" ]; then
	pass_error
	fi

	pass_dialog=root

	if [ -n "$newpass" ]; then
	/bin/bash -c "echo -e \"$newpass\n$newpass\n\" | passwd root"
	fi
	
$(yad --window-icon="$MICON" --image="$ICON" --title=$"Root Password" \
--width=400 --height=100 --fixed --center --borders=10 --on-top --button="Ok!$OK":0 \
--text=$"\nRoot password has been updated successfully!" )
exit 0			
	fi
      fi
