#! /bin/bash
# This script repeatedly prompts the user for their UP Portal password to gain internet access


( # Create subshell for private env vars

this_dir=$(dirname $BASH_SOURCE)
. $this_dir/.env # Load private env vars

first=1

while true; do
	#----- Prompt for password -----#
	pword=$(zenity --entry --hide-text --text="Please enter your UP Portal password")

	# Check password non-empty
	if [[ $? -ne 0 ]]; then
		zenity --error --ellipsize --text="UP Portal password is needed to allow internet access"
		exit 1;
	fi


	#----- Get username -----#
	usrname=$(whoami)
	if [[ "$USERNAME_OVERRIDE" != "" ]]; then
		usrname=$USERNAME_OVERRIDE
	fi


	#----- Write temp ad_cred file -----#
	cat > $AD_CRED <<-EOF
	username=$usrname
	password=$pword
	EOF


	#----- Try login -----#
	sudo $this_dir/mnt-ad.sh && break
	zenity --error --ellipsize --text="Incorrect password for UP login, please re-enter your password"
done

) # End subshell
