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