update to upstream 5.4.6
[automated-distro-installer] / fai / config / class / 40-parse-profiles.sh
1
2 #! /bin/bash
3
4 # parse *.profile and build a curses menu, so the user can select a profile
5 #
6 # (c) 2015 by Thomas Lange, lange@informatik.uni-koeln.de
7 # Universitaet zu Koeln
8
9 if [ X$FAI_ACTION = Xinstall -o X$FAI_ACTION = Xdirinstall -o X$FAI_ACTION = X ]; then
10 :
11 else
12 return 0
13 fi
14
15 [ "$flag_menu" ] || return 0
16
17 out=$(tty)
18 tempfile=`(tempfile) 2>/dev/null`
19 tempfile2=`(tempfile) 2>/dev/null`
20 trap "rm -f $tempfile $tempfile2" EXIT INT QUIT
21
22 # declare the data structure, use associative arrays
23 declare -A arshort
24 declare -A ardesc
25 declare -A arlong
26 declare -A arclasses
27 declare -a list
28
29
30 parse_profile() {
31
32 # read a profile and add all info to the data structure
33
34 local short
35 local long
36 local desc
37 local name
38 local classes
39 local lflag=0
40
41 # disable word splitting when reading a line, this helps reading a keyword without a value
42 local OIF=$IFS
43 IFS=
44
45 while read -r line || [[ -n $line ]]; do
46
47 if [[ $line =~ "Name: " ]]; then
48 if [ -n "$long" ]; then
49 arlong[$name]="$long"
50 fi
51 short=
52 desc=
53 long=
54 classes=
55 lflag=0
56 name=${line##Name: }
57 [ $debug ] && echo "XX NAME $name found"
58 list+=("$name") # add new item to list
59 continue
60 fi
61
62 if [[ $line =~ "Description: " ]]; then
63 lflag=0
64 desc=${line##Description: }
65 [ $debug ] && echo "XX $desc found"
66 ardesc[$name]="$desc"
67 continue
68 fi
69
70 if [[ $line =~ "Short: " ]]; then
71 lflag=0
72 short=${line##Short: }
73 [ $debug ] && echo "XX $short found"
74 arshort[$name]="$short"
75 continue
76 fi
77
78 if [[ $line =~ "Classes: " ]]; then
79 lflag=0
80 classes=${line##Classes: }
81 [ $debug ] && echo "XX classes found"
82 arclasses[$name]="$classes"
83 continue
84 fi
85
86 if [[ $line =~ "Long: " ]]; then
87 lflag=1
88 long=${line##Long: }
89 [ $debug ] && echo "XX long found"
90
91 # else it's another long line
92 elif [ $lflag -eq 1 ]; then
93 long+="\n$line"
94 fi
95
96 if [[ $line =~ "Default: " ]]; then
97 lflag=0
98 default=${line##Default: }
99 continue
100 fi
101
102 done < $1
103
104 if [ -n "$long" ]; then
105 arlong[$name]="$long"
106 fi
107 IFS=$OIF
108 }
109
110 prtresult() {
111
112 # set newclasses which is used by fai-class(1)
113 local res=$(<$tempfile)
114 echo "$BASH_SOURCE defined new classes: ${arclasses[$res]}"
115 newclasses="${arclasses[$res]}"
116 }
117
118
119 # read all files with name matching *.profile
120 _parsed=0
121 shopt -s nullglob
122 for _f in *.profile; do
123 parse_profile $_f
124 _parsed=1
125 done
126 unset _f
127
128 # do nothing if no profile was read
129 if [ $_parsed -eq 0 ]; then
130 unset _parsed
131 return 0
132 fi
133
134 # create the argument list containing the menu entries
135 # and the help text file
136 for i in "${list[@]}"; do
137 par+=("$i")
138 par+=("${ardesc[${i}]}")
139 par+=("${arshort[${i}]}")
140 echo "Name: ${i}" >> $tempfile2
141 echo -e ${arlong[${i}]} >> $tempfile2
142 echo -e "Classes: " ${arclasses[${i}]} "\n" >> $tempfile2
143 done
144 unset i
145
146 while true; do
147
148 dialog --clear --item-help --title "FAI - Fully Automatic Installation" --help-button \
149 --default-item "$default" \
150 --menu "\nSelect your FAI profile\n\nThe profile will define a list of classes,\nwhich are used by FAI.\n\n\n"\
151 15 70 0 "${par[@]}" 2> $tempfile 1> $out
152
153 _retval=$?
154 case $_retval in
155 0)
156 prtresult
157 break ;;
158 1)
159 echo "No profile selected."
160 break ;;
161 2)
162 dialog --title "Description of all profiles" --textbox $tempfile2 0 0 1> $out;;
163 esac
164
165 done
166 unset par ardesc arshort arlong arclasses list tempfile tempfile2 _parsed _retval line