Add home page link.
[gsu.git] / config
1 #!/bin/bash
2 # Copyright (C) 2006 Andre Noll
3 # Licensed under the LGPL, version 3. See COPYING and COPYING.LESSER.
4
5 # Syntactically check the gsu_options array for errors and parse the config
6 # file.
7 gsu_check_options()
8 {
9         local i conf="${gsu_config_file:=${HOME:-}/.$gsu_name.rc}" val orig_val
10         local name option_type default_value required description help_text
11
12         for ((i=0; i < ${#gsu_options[@]}; i++)); do
13                 eval "${gsu_options[$i]}"
14                 eval val='"'\${${name}:-}'"'
15                 eval orig_${gsu_config_var_prefix}_$name='"'${val}'"'
16         done
17
18         [[ -r "$conf" ]] && source "$conf"
19
20         for ((i=0; i < ${#gsu_options[@]}; i++)); do
21                 name=
22                 option_type=
23                 default_value=
24                 required=
25                 description=
26                 help_text=
27
28                 eval "${gsu_options[$i]}"
29
30                 # Check name. It must be non-empty and consist of [a-zA-Z_0-9]
31                 # only.  Moreover it must start with [a-zA-Z].
32                 ret=-$E_GSU_BAD_CONFIG_VAR
33                 result="name: '$name'"
34                 [[ "$name" =~ ^[a-zA-Z][a-zA-Z_0123456789]*$ ]] || return
35
36                 eval orig_val='"'\$orig_${gsu_config_var_prefix}_$name'"'
37                 if [[ -z "$orig_val" ]]; then
38                         eval val='"'\${$name:-}'"'
39                 else
40                         val="$orig_val"
41                 fi
42                 case "$required" in
43                 true|yes)
44                         ret=-$E_GSU_NEED_VALUE
45                         result="$name"
46                         [[ -z "$val" ]] && return
47                         ;;
48                 false|no)
49                         ;;
50                 *)
51                         ret=-$E_GSU_BAD_BOOL
52                         result="required: $required, name: $name, val: $val"
53                         return
54                 esac
55
56                 eval ${gsu_config_var_prefix}_$name='"'\${val:="$default_value"}'"'
57                 # Check option type. ATM, only num and string are supported
58                 # Other types may be added without breaking compatibility
59                 case "$option_type" in
60                 string)
61                         ;;
62                 num)
63                         gsu_is_a_number "$val"
64                         ((ret < 0)) && return
65                         ;;
66                 *)
67                         ret=-$E_GSU_BAD_OPTION_TYPE
68                         result="$name/$option_type"
69                         return
70                 esac
71         done
72         ret=$GSU_SUCCESS
73 }
74
75 # Call gsu_check_options(), die on errors.
76 gsu_check_options_or_die()
77 {
78         gsu_check_options
79         if ((ret < 0)); then
80                 gsu_err_msg
81                 exit 1
82         fi
83 }
84
85 if [[ "$(type -t _gsu_setup)" != "function" ]]; then
86         gsu_dir=${gsu_dir:-${BASH_SOURCE[0]%/*}}
87         . "$gsu_dir/common" || exit 1
88         _gsu_setup
89 fi