#!/bin/zsh -f

#######################################################################
## user_setup                                                        ##
##                                                                   ##
## by Wataru Kagawa                                                  ##
## March 05, 2006                                                    ##
## wkagawa@jota.gsc.riken.go.jp                                      ##
##                                                                   ##
## Sets up user names and its completion using Pashua gui.           ##
#######################################################################



#=======================
# Define local variables
#=======================

local use_etc_users use_custom_users usernames conf tx ch1 ch2 tb btn cn



#==================================
# Check current completion settings
#==================================

if [[ $USE_ETC_USERS == 'yes' ]]; then
	use_etc_users='1'
else
	use_etc_users='0'
fi

if [[ $USE_CUSTOM_USERS == 'yes' ]]; then
	use_custom_users='1'
else
	use_custom_users='0'
fi



#=============
# Starting gui
#=============

usernames=( $( < ~/.zsh/cache/custom_users ) )

if [[ -z $usernames ]]; then
	usernames=( ${$( command ls /Users/ ):#Shared} )
fi

conf=( '*.transparency=0.95' \
	'*.title=user completion setup' \
	tx.type=text \
	tx.width=500 \
	tx.text='Choose user name completion options:' \
	ch1.type=checkbox \
	ch1.label='Use user names generated by zsh _user function.' \
	ch1.default=$use_etc_users \
	ch2.type=checkbox \
	ch2.label='Use custom-defined list of users.' \
	ch2.default=$use_custom_users \
	tb.type=textbox \
	tb.label='Edit user names:' \
	tb.width=300 \
	tb.height=20 \
	btn.type=button \
	btn.label='Revert to default settings' \
	cn.type=cancelbutton \
	db.type=defaultbutton \
	db.label=Save \
	tb.default="${(j:[return]:)usernames}" )

pashua_run "${(F)conf}"



#=================================
# Interpreting changes made in gui
#=================================

if [[ $cn == '1' ]]; then

	#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	# Exit program if Cancel button is pressed
	#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

	return 0


elif [[ $btn == '1' ]]; then

	#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	# Change to initial settings if 'Revert to default settings' button is pressed
	#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

		#-----------------------------------------------
		# Use user names generated by zsh _user function
		#-----------------------------------------------

		ch1='1'


		#----------------------------------------
		# Do not use custom-defined list of users
		#----------------------------------------

		ch2='0'


else

	#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	# Save changes made in user names if Save button is pressed
	#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

	print ${tb//_RETURN_/\\n} >| ~/.zsh/cache/custom_users

fi



#=======================================================================
# Save completion options in host_user_completion_settings and source it
#=======================================================================

if [[ $ch1 == '1' ]]; then
	command perl -pi -e "s|USE_ETC_USERS.+|USE_ETC_USERS=\'yes\'|g" ~/.zsh/cache/host_user_completion_settings
else
	command perl -pi -e "s|USE_ETC_USERS.+|USE_ETC_USERS=\'no\'|g" ~/.zsh/cache/host_user_completion_settings
fi

if [[ $ch2 == '1' ]]; then
	command perl -pi -e "s|USE_CUSTOM_USERS.+|USE_CUSTOM_USERS=\'yes\'|g" ~/.zsh/cache/host_user_completion_settings
else
	command perl -pi -e "s|USE_CUSTOM_USERS.+|USE_CUSTOM_USERS=\'no\'|g" ~/.zsh/cache/host_user_completion_settings
fi

source ~/.zsh/cache/host_user_completion_settings



#========================================================
# Issue a warning message if custom_users is empty and if
# USE_CUSTOM_USERS variable is set to 'yes'.
#========================================================

if [[ -z $( < ~/.zsh/cache/custom_users ) && $USE_CUSTOM_USERS == 'yes' ]]; then

	if [[ $ENCODING == '1:14' ]]; then
		print ""
		print "\e[1m  custom_usersファイルが空です. user_setupコマンドを実行してください.\e[0m"
		print ""
	else
		print ""
		print "\e[1m  custom_users file is empty.  Please execute user_setup.\e[0m"
		print ""
	fi

fi



#=======================
# Update $USERS variable
#=======================

	#~~~~~~~~~~~~~~~~~~~~~~~~~
	# Clear elements in $USERS
	#~~~~~~~~~~~~~~~~~~~~~~~~~

	USERS=()


	#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	# Define a list of users for user name completion
	#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

	if [[ $USE_ETC_USERS == 'yes' ]]; then

		zmodload -i zsh/parameter
		etc_users=( $userdirs[(I)*] )

		USERS+=( $etc_users )

	fi

	if [[ $USE_CUSTOM_USERS == 'yes' && -f ~/.zsh/cache/custom_users ]]; then

		custom_users=(

		$( < ~/.zsh/cache/custom_users )

		)

		USERS+=( $custom_users )

	fi


	#~~~~~~~~~~~~~~~~~~~~~~~
	# get rid of the repeats
	#~~~~~~~~~~~~~~~~~~~~~~~

	typeset -U USERS
