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