1245052Sdteskeif [ ! "$_VARIABLE_SUBR" ]; then _VARIABLE_SUBR=1
2245052Sdteske#
3263980Sdteske# Copyright (c) 2012-2014 Devin Teske
4252980Sdteske# All rights reserved.
5245052Sdteske#
6245052Sdteske# Redistribution and use in source and binary forms, with or without
7245052Sdteske# modification, are permitted provided that the following conditions
8245052Sdteske# are met:
9245052Sdteske# 1. Redistributions of source code must retain the above copyright
10245052Sdteske#    notice, this list of conditions and the following disclaimer.
11245052Sdteske# 2. Redistributions in binary form must reproduce the above copyright
12245052Sdteske#    notice, this list of conditions and the following disclaimer in the
13245052Sdteske#    documentation and/or other materials provided with the distribution.
14245052Sdteske#
15245052Sdteske# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16252987Sdteske# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17245052Sdteske# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18245052Sdteske# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19245052Sdteske# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20252987Sdteske# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21245052Sdteske# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22245052Sdteske# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23245052Sdteske# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24245052Sdteske# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25245052Sdteske# SUCH DAMAGE.
26245052Sdteske#
27245052Sdteske# $FreeBSD$
28245052Sdteske#
29245052Sdteske############################################################ INCLUDES
30245052Sdteske
31245052SdteskeBSDCFG_SHARE="/usr/share/bsdconfig"
32245052Sdteske. $BSDCFG_SHARE/common.subr || exit 1
33245052Sdteskef_dprintf "%s: loading includes..." variable.subr
34245052Sdteskef_include $BSDCFG_SHARE/dialog.subr
35251364Sdteskef_include $BSDCFG_SHARE/strings.subr
36245052Sdteske
37245052Sdteske############################################################ GLOBALS
38245052Sdteske
39245052SdteskeVARIABLES=
40245052Sdteske
41245052Sdteske#
42245052Sdteske# Default behavior is to call f_variable_set_defaults() when loaded.
43245052Sdteske#
44245052Sdteske: ${VARIABLE_SELF_INITIALIZE=1}
45245052Sdteske
46245052Sdteske#
47245052Sdteske# File to write when f_dump_variables() is called.
48245052Sdteske#
49245052Sdteske: ${VARIABLE_DUMPFILE:=/etc/bsdconfig.vars}
50245052Sdteske
51245052Sdteske############################################################ FUNCTIONS
52245052Sdteske
53245052Sdteske# f_variable_new $handle $variable
54245052Sdteske#
55245052Sdteske# Register a new variable named $variable with the given reference-handle
56245052Sdteske# $handle. The environment variable $handle is set to $variable allowing you to
57245052Sdteske# use the f_getvar() function (from common.subr) with $handle to get the value
58245052Sdteske# of environment variable $variable. For example:
59245052Sdteske#
60245052Sdteske# 	f_variable_new VAR_ABC abc
61245052Sdteske#
62245052Sdteske# allows the later indirection:
63245052Sdteske#
64245052Sdteske# 	f_getvar $VAR_ABC
65245052Sdteske#
66245052Sdteske# to return the value of environment variable `abc'. Variables registered in
67245052Sdteske# this manner are recorded in the $VARIABLES environment variable for later
68245052Sdteske# allowing dynamic enumeration of so-called `registered/advertised' variables.
69245052Sdteske#
70245052Sdteskef_variable_new()
71245052Sdteske{
72245052Sdteske	local handle="$1" variable="$2"
73245052Sdteske	[ "$handle" ] || return $FAILURE
74245052Sdteske	f_dprintf "variable.subr: New variable %s -> %s" "$handle" "$variable"
75245052Sdteske	setvar $handle $variable
76245052Sdteske	VARIABLES="$VARIABLES${VARIABLES:+ }$handle"
77245052Sdteske}
78245052Sdteske
79245052Sdteske# f_variable_get_value $var [ $fmt [ $opts ... ] ]
80245052Sdteske#
81245052Sdteske# Unless nonInteractive is set, prompt the user with a given value (pre-filled
82245052Sdteske# with the value of $var) and give them the chance to change the value.
83245052Sdteske#
84245052Sdteske# Unlike f_getvar() (from common.subr) which can return a variable to the
85245052Sdteske# caller on standard output, this function has no [meaningful] output.
86245052Sdteske#
87245052Sdteske# Returns success unless $var is either NULL or missing.
88245052Sdteske#
89245052Sdteskef_variable_get_value()
90245052Sdteske{
91245052Sdteske	local var="$1" cp
92245052Sdteske
93245052Sdteske	[ "$var" ] || return $FAILURE
94245052Sdteske
95245052Sdteske	if ! { f_getvar $var cp && ! f_interactive; }; then
96245052Sdteske		shift 1 # var
97251242Sdteske		f_dialog_input cp "$( printf "$@" )" "$cp" && setvar $var "$cp"
98245052Sdteske	fi
99245052Sdteske
100245052Sdteske	return $SUCCESS
101245052Sdteske}
102245052Sdteske
103245052Sdteske# f_variable_set_defaults
104245052Sdteske#
105245052Sdteske# Installs sensible defaults for registered/advertised variables.
106245052Sdteske#
107245052Sdteskef_variable_set_defaults()
108245052Sdteske{
109252744Sdteske	f_dprintf "f_variable_set_defaults: Initializing defaults..."
110252744Sdteske
111245052Sdteske	#
112245052Sdteske	# Initialize various user-edittable values to their defaults
113245052Sdteske	#
114247280Sdteske	setvar $VAR_EDITOR		"${EDITOR:-/usr/bin/ee}"
115251613Sdteske	setvar $VAR_FTP_STATE		"auto"
116247280Sdteske	setvar $VAR_FTP_USER		"ftp"
117247280Sdteske	setvar $VAR_HOSTNAME		"$( hostname )"
118247280Sdteske	setvar $VAR_MEDIA_TIMEOUT	"300"
119247280Sdteske	setvar $VAR_NFS_SECURE		"NO"
120247280Sdteske	setvar $VAR_NFS_TCP		"NO"
121247280Sdteske	setvar $VAR_NFS_V3		"YES"
122250323Sdteske	setvar $VAR_PKG_TMPDIR		"/var/tmp"
123247280Sdteske	setvar $VAR_RELNAME		"$UNAME_R"
124245052Sdteske
125252744Sdteske	#
126252744Sdteske	# Debugging
127252744Sdteske	#
128252744Sdteske	if f_debugging; then
129252744Sdteske		local var
130252744Sdteske		for var in \
131252744Sdteske			$VAR_EDITOR		\
132252744Sdteske			$VAR_FTP_STATE		\
133252744Sdteske			$VAR_FTP_USER		\
134252744Sdteske			$VAR_HOSTNAME		\
135252744Sdteske			$VAR_MEDIA_TIMEOUT	\
136252744Sdteske			$VAR_NFS_SECURE		\
137252744Sdteske			$VAR_NFS_TCP		\
138252744Sdteske			$VAR_NFS_V3		\
139252744Sdteske			$VAR_PKG_TMPDIR		\
140252744Sdteske			$VAR_RELNAME		\
141252744Sdteske		; do
142252744Sdteske			f_quietly f_getvar $var
143252744Sdteske		done
144252744Sdteske	fi
145252744Sdteske
146245052Sdteske	f_dprintf "f_variable_set_defaults: Defaults initialized."
147245052Sdteske}
148245052Sdteske
149245052Sdteske# f_dump_variables
150245052Sdteske#
151245052Sdteske# Dump a list of registered/advertised variables and their respective values to
152245052Sdteske# $VARIABLE_DUMPFILE. Returns success unless the file couldn't be written. If
153245437Sdteske# an error occurs, it is displayed using f_dialog_msgbox() (from dialog.subr).
154245052Sdteske#
155245052Sdteskef_dump_variables()
156245052Sdteske{
157251364Sdteske	local err
158245052Sdteske	if ! err=$(
159245052Sdteske		( for handle in $VARIABLES; do
160245052Sdteske			f_getvar $handle var || continue
161245052Sdteske			f_getvar $var value || continue
162251364Sdteske			f_shell_escape "$value" value
163245052Sdteske			printf "%s='%s'\n" "$var" "$value"
164245052Sdteske		  done > "$VARIABLE_DUMPFILE" ) 2>&1
165245052Sdteske	); then
166245437Sdteske		f_dialog_msgbox "$err"
167245052Sdteske		return $FAILURE
168245052Sdteske	fi
169245052Sdteske}
170245052Sdteske
171245052Sdteske# f_debugging
172245052Sdteske#
173245052Sdteske# Are we in debug mode? Returns success if extra DEBUG information has been
174245052Sdteske# requested (by setting $debug to non-NULL), otherwise false.
175245052Sdteske#
176245052Sdteskef_debugging()
177245052Sdteske{
178245052Sdteske	local value
179245052Sdteske	f_getvar $VAR_DEBUG value && [ "$value" ]
180245052Sdteske}
181245052Sdteske
182260675Sdteske# f_interactive
183245052Sdteske#
184245052Sdteske# Are we running interactively? Return error if $nonInteractive is set and non-
185245052Sdteske# NULL, otherwise return success.
186245052Sdteske#
187245052Sdteskef_interactive()
188245052Sdteske{
189245052Sdteske	local value
190245052Sdteske	! f_getvar $VAR_NONINTERACTIVE value || [ ! "$value" ]
191245052Sdteske}
192245052Sdteske
193260675Sdteske# f_netinteractive
194247280Sdteske#
195247280Sdteske# Has the user specifically requested the network-portion of configuration and
196247280Sdteske# setup to be performed interactively? Returns success if the user has asked
197247280Sdteske# for the network configuration to be done interactively even if perhaps over-
198247280Sdteske# all non-interactive mode has been requested (by setting nonInteractive).
199247280Sdteske#
200247280Sdteske# Returns success if $netInteractive is set and non-NULL.
201247280Sdteske#
202247280Sdteskef_netinteractive()
203247280Sdteske{
204247280Sdteske	local value
205247280Sdteske	f_getvar $VAR_NETINTERACTIVE value && [ "$value" ]
206247280Sdteske}
207247280Sdteske
208260675Sdteske# f_zfsinteractive
209256361Sdteske#
210256361Sdteske# Has the user specifically requested the ZFS-portion of configuration and
211256361Sdteske# setup to be performed interactively? Returns success if the user has asked
212256361Sdteske# for the ZFS configuration to be done interactively even if perhaps overall
213256361Sdteske# non-interactive mode has been requested (by setting nonInteractive).
214256361Sdteske#
215256361Sdteske# Returns success if $zfsInteractive is set and non-NULL.
216256361Sdteske#
217256361Sdteskef_zfsinteractive()
218256361Sdteske{
219256361Sdteske	local value
220256361Sdteske	f_getvar $VAR_ZFSINTERACTIVE value && [ "$value" ]
221256361Sdteske}
222256361Sdteske
223245052Sdteske############################################################ MAIN
224245052Sdteske
225245052Sdteske#
226245052Sdteske# Variables that can be tweaked from config files
227245052Sdteske#
228247280Sdteske#              Handle                   Variable Name
229245052Sdteskef_variable_new VAR_CONFIG_FILE		configFile
230245052Sdteskef_variable_new VAR_DEBUG		debug
231245052Sdteskef_variable_new VAR_DEBUG_FILE		debugFile
232247280Sdteskef_variable_new VAR_DIRECTORY_PATH	_directoryPath
233247280Sdteskef_variable_new VAR_DOMAINNAME		domainname
234247280Sdteskef_variable_new VAR_EDITOR		editor
235247280Sdteskef_variable_new VAR_EXTRAS		ifconfig_
236247280Sdteskef_variable_new VAR_FTP_DIR		ftpDirectory
237247280Sdteskef_variable_new VAR_FTP_HOST		ftpHost
238247280Sdteskef_variable_new VAR_FTP_PASS		ftpPass
239247280Sdteskef_variable_new VAR_FTP_PATH		_ftpPath
240247280Sdteskef_variable_new VAR_FTP_PORT		ftpPort
241247280Sdteskef_variable_new VAR_FTP_STATE		ftpState
242247280Sdteskef_variable_new VAR_FTP_USER		ftpUser
243247280Sdteskef_variable_new VAR_GATEWAY		defaultrouter
244263980Sdteskef_variable_new VAR_GROUP		group
245263980Sdteskef_variable_new VAR_GROUP_GID		groupGid
246263980Sdteskef_variable_new VAR_GROUP_MEMBERS	groupMembers
247263980Sdteskef_variable_new VAR_GROUP_PASSWORD	groupPassword
248247280Sdteskef_variable_new VAR_HOSTNAME		hostname
249252112Sdteskef_variable_new VAR_HTTP_DIR		httpDirectory
250247280Sdteskef_variable_new VAR_HTTP_FTP_MODE	httpFtpMode
251252112Sdteskef_variable_new VAR_HTTP_HOST		httpHost
252252112Sdteskef_variable_new VAR_HTTP_PATH		_httpPath
253252112Sdteskef_variable_new VAR_HTTP_PORT		httpPort
254247280Sdteskef_variable_new VAR_HTTP_PROXY		httpProxy
255247280Sdteskef_variable_new VAR_HTTP_PROXY_HOST	httpProxyHost
256247280Sdteskef_variable_new VAR_HTTP_PROXY_PATH	_httpProxyPath
257247280Sdteskef_variable_new VAR_HTTP_PROXY_PORT	httpProxyPort
258247280Sdteskef_variable_new VAR_IFCONFIG		ifconfig_
259247280Sdteskef_variable_new VAR_IPADDR		ipaddr
260247280Sdteskef_variable_new VAR_IPV6ADDR		ipv6addr
261247280Sdteskef_variable_new VAR_IPV6_ENABLE		ipv6_activate_all_interfaces
262256361Sdteskef_variable_new VAR_KEYMAP		keymap
263247280Sdteskef_variable_new VAR_MEDIA_TIMEOUT	MEDIA_TIMEOUT
264247280Sdteskef_variable_new VAR_MEDIA_TYPE		mediaType
265247280Sdteskef_variable_new VAR_NAMESERVER		nameserver
266247280Sdteskef_variable_new VAR_NETINTERACTIVE	netInteractive
267247280Sdteskef_variable_new VAR_NETMASK		netmask
268247280Sdteskef_variable_new VAR_NETWORK_DEVICE	netDev
269247280Sdteskef_variable_new VAR_NFS_HOST		nfsHost
270247280Sdteskef_variable_new VAR_NFS_PATH		nfsPath
271247280Sdteskef_variable_new VAR_NFS_SECURE		nfs_reserved_port_only
272247280Sdteskef_variable_new VAR_NFS_TCP		nfs_use_tcp
273247280Sdteskef_variable_new VAR_NFS_V3		nfs_use_v3
274247280Sdteskef_variable_new VAR_NONINTERACTIVE	nonInteractive
275250323Sdteskef_variable_new VAR_NO_CONFIRM		noConfirm
276245052Sdteskef_variable_new VAR_NO_ERROR		noError
277247280Sdteskef_variable_new VAR_NO_INET6		noInet6
278250323Sdteskef_variable_new VAR_PACKAGE		package
279250323Sdteskef_variable_new VAR_PKG_TMPDIR		PKG_TMPDIR
280250323Sdteskef_variable_new VAR_PORTS_PATH		ports
281245052Sdteskef_variable_new VAR_RELNAME		releaseName
282247280Sdteskef_variable_new VAR_SLOW_ETHER		slowEthernetCard
283247280Sdteskef_variable_new VAR_TRY_DHCP		tryDHCP
284247280Sdteskef_variable_new VAR_TRY_RTSOL		tryRTSOL
285247280Sdteskef_variable_new VAR_UFS_PATH		ufs
286263980Sdteskef_variable_new VAR_USER			user
287263980Sdteskef_variable_new VAR_USER_ACCOUNT_EXPIRE	userAccountExpire
288263980Sdteskef_variable_new VAR_USER_DOTFILES_CREATE	userDotfilesCreate
289263980Sdteskef_variable_new VAR_USER_GECOS		userGecos
290263980Sdteskef_variable_new VAR_USER_GID		userGid
291263980Sdteskef_variable_new VAR_USER_GROUPS		userGroups
292263980Sdteskef_variable_new VAR_USER_GROUP_DELETE	userGroupDelete
293263980Sdteskef_variable_new VAR_USER_HOME		userHome
294263980Sdteskef_variable_new VAR_USER_HOME_CREATE	userHomeCreate
295263980Sdteskef_variable_new VAR_USER_HOME_DELETE	userHomeDelete
296263980Sdteskef_variable_new VAR_USER_LOGIN_CLASS	userLoginClass
297263980Sdteskef_variable_new VAR_USER_PASSWORD	userPassword
298263980Sdteskef_variable_new VAR_USER_PASSWORD_EXPIRE	userPasswordExpire
299263980Sdteskef_variable_new VAR_USER_SHELL		userShell
300263980Sdteskef_variable_new VAR_USER_UID		userUid
301256361Sdteskef_variable_new VAR_ZFSINTERACTIVE	zfsInteractive
302245052Sdteske
303245052Sdteske#
304245052Sdteske# Self-initialize unless requested otherwise
305245052Sdteske#
306245052Sdteskef_dprintf "%s: VARIABLE_SELF_INITIALIZE=[%s]" \
307245052Sdteske          variable.subr "$VARIABLE_SELF_INITIALIZE"
308245052Sdteskecase "$VARIABLE_SELF_INITIALIZE" in
309245052Sdteske""|0|[Nn][Oo]|[Oo][Ff][Ff]|[Ff][Aa][Ll][Ss][Ee]) : do nothing ;;
310245052Sdteske*) f_variable_set_defaults
311245052Sdteskeesac
312245052Sdteske
313245052Sdteskef_dprintf "%s: Successfully loaded." variable.subr
314245052Sdteske
315245052Sdteskefi # ! $_VARIABLE_SUBR
316