aboutsummaryrefslogtreecommitdiff
path: root/fai/config/class/40-parse-profiles.sh
blob: 7885c7ff51ba5282a797a66b3782fa4f98fad75c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#! /bin/bash

# parse *.profile and build a curses menu, so the user can select a profile
#
# (c) 2015 by Thomas Lange, lange@informatik.uni-koeln.de
# Universitaet zu Koeln
# (c) 2023 by Mike Gabriel, mike.gabriel@das-netzwerkteam.de
# Fre(i)e Software GmbH

if [ X$FAI_ACTION = Xinstall -o X$FAI_ACTION = Xdirinstall -o X$FAI_ACTION = X ]; then
    :
else
    return 0
fi

[ "$flag_menu" ] || return 0

out=$(tty)
tempfile=`(tempfile) 2>/dev/null`
tempfile2=`(tempfile) 2>/dev/null`
trap "rm -f $tempfile $tempfile2" EXIT INT QUIT

# declare the data structure, use associative arrays
declare -A arshort
declare -A ardesc
declare -A arlong
declare -A arclasses
declare -a list


parse_profile() {

    # read a profile and add all info to the data structure

    local short
    local long
    local desc
    local name
    local classes
    local lflag=0

    # disable word splitting when reading a line, this helps reading a keyword without a value
    local OIF=$IFS
    IFS=

    while read -r line || [[ -n $line ]]; do

	if [[ $line =~ "Name: " ]]; then
	    if [ -n "$long" ]; then
		arlong[$name]="$long"
	    fi
	    short=
	    desc=
	    long=
	    classes=
	    lflag=0
            name=${line##Name: }
	    [ $debug ] && echo "XX NAME $name found"
	    list+=("$name")  # add new item to list
	    continue
	fi

	if [[ $line =~ "Description: " ]]; then
	    lflag=0
            desc=${line##Description: }
	    [ $debug ] && echo "XX $desc found"
	    ardesc[$name]="$desc"
	    continue
	fi

	if [[ $line =~ "Short: " ]]; then
	    lflag=0
            short=${line##Short: }
	    [ $debug ] && echo "XX $short found"
	    arshort[$name]="$short"
	    continue
	fi

	if [[ $line =~ "Classes: " ]]; then
            lflag=0
            classes=${line##Classes: }
	    [ $debug ] && echo "XX classes found"
	    arclasses[$name]="$classes"
	    continue
	fi
	
	if [[ $line =~ "Long: " ]]; then
            lflag=1
            long=${line##Long: }
	    [ $debug ] && echo "XX long found"

	# else it's another long line
	elif [ $lflag -eq 1 ]; then
	    long+="\n$line"
	fi

	if [[ $line =~ "Default: " ]]; then
	    lflag=0
            default=${line##Default: }
	    continue
	fi

    done < $1

    if [ -n "$long" ]; then
	arlong[$name]="$long"
    fi
    IFS=$OIF
}

prtresult() {

    # set newclasses which is used by fai-class(1)
    local res=$(<$tempfile)
    echo "$BASH_SOURCE defined new classes: ${arclasses[$res]}"
    newclasses="${arclasses[$res]}"
}

# a very simple OS + version detection mechanism
OS_SUFFIX="$(source /etc/os-release 2>/dev/null && printf "%s_%s" "${ID^^}" "${VERSION_ID}")"

if [ -z "${OS_SUFFIX}" ]; then
	# Use VERSION_CODENAME instead, this happens when installing based on Debian testing.
	OS_SUFFIX="$(source /etc/os-release 2>/dev/null && printf "%s_%s" "${ID^^}" "${VERSION_CODENAME}")"
fi

# read all files with name matching *.profile 
_parsed=0
shopt -s nullglob
for _f in *.profile; do
    parse_profile $_f
    _parsed=1
done

# additionally read all files with name matching *.profile.<OS_SUFFIX>
for _f in *.profile.${OS_SUFFIX}; do
    parse_profile $_f
    _parsed=1
done
unset _f

# do nothing if no profile was read
if [ $_parsed -eq 0 ]; then
    unset _parsed
    return 0
fi

# create the argument list containing the menu entries
# and the help text file
for i in "${list[@]}"; do
    par+=("$i")
    par+=("${ardesc[${i}]}")
    par+=("${arshort[${i}]}")
    echo "Name: ${i}" >> $tempfile2
    echo -e ${arlong[${i}]} >> $tempfile2
    echo -e "Classes: " ${arclasses[${i}]} "\n" >> $tempfile2
done
unset i

while true; do

    dialog --clear --item-help --title "FAI - Fully Automatic Installation" --help-button \
	--default-item "$default" \
	--menu "\nSelect your FAI profile\n\nThe profile will define a list of classes,\nwhich are used by FAI.\n\n\n"\
	15 75 0 "${par[@]}" 2> $tempfile  1> $out
    _retval=$?
    case $_retval in
	0)
	    prtresult
	    break ;;
	1)
	    echo "No profile selected."
	    break ;;
	2)
	    dialog --title "Description of all profiles" --textbox $tempfile2 0 0 1> $out;;
    esac

done
unset par ardesc arshort arlong arclasses list tempfile tempfile2 _parsed _retval line