config / .fgfs / fgaddon /
Newer Older
495 lines | 19.795kb
ajout de gestion de bdd pour...
Sébastien MARQUE authored on 2020-08-24
1
#!/bin/bash
2

            
ré-écriture complète
Sébastien MARQUE authored on 2020-09-20
3
set -e
4

            
make resilient data types + ...
Sébastien MARQUE authored on 2021-03-25
5
declare -A datatypes=(
ré-écriture complète
Sébastien MARQUE authored on 2020-09-20
6
        [/sim/description]=text
7
        [/sim/long-description]=text
8
        [/sim/author]=text
9
        [/sim/flight-model]=text
10
        [/sim/type]=text
11
        [/sim/model/path]=text
add new retrieved data
Sébastien MARQUE authored on 2021-03-25
12
        [/sim/rating/FDM]="integer DEFAULT 0"
13
        [/sim/rating/systems]="integer DEFAULT 0"
14
        [/sim/rating/cockpit]="integer DEFAULT 0"
15
        [/sim/rating/model]="integer DEFAULT 0"
ré-écriture complète
Sébastien MARQUE authored on 2020-09-20
16
)
complete rewrite
Sébastien MARQUE authored on 2020-11-07
17

            
18
missing_data_check=( /sim/model/path )
19

            
ré-écriture complète
Sébastien MARQUE authored on 2020-09-20
20
database=${DB:-$0.db}
complete rewrite
Sébastien MARQUE authored on 2020-11-07
21
test -r "$0.conf" && source "$0.conf"
22

            
ré-écriture complète
Sébastien MARQUE authored on 2020-09-20
23
#locale=fr
24

            
complete rewrite
Sébastien MARQUE authored on 2020-11-07
25
tempid=$(mktemp --dry-run XXXXXXX)
26
temppath=/dev/shm
ré-écriture complète
Sébastien MARQUE authored on 2020-09-20
27

            
complete rewrite
Sébastien MARQUE authored on 2020-11-07
28
setxml=$temppath/setxml-$tempid
29
json_file=$temppath/github_json-$tempid
30
in_ram_database=$temppath/${database##*/}-$tempid
ajout de gestion de bdd pour...
Sébastien MARQUE authored on 2020-08-24
31

            
ré-écriture complète
Sébastien MARQUE authored on 2020-09-20
32
function xmlgetnext () {
33
    local IFS='>'
34
    read -d '<' TAG VALUE
35
    # by design, the first TAG/VALUE pair is empty
36
    # to avoid infinite loops at end of file parsing we return an error
37
    # the next time we find an empty TAG
38
    if test -z "$TAG"; then
better infinite loop check
Sébastien MARQUE authored on 2020-10-19
39
        test ${xmlgetnext_empty_tag:-0} -gt 0 && return 1
40
        xmlgetnext_empty_tag=$(( xmlgetnext_empty_tag + 1 ))
ré-écriture complète
Sébastien MARQUE authored on 2020-09-20
41
    fi
42
    # process $TAG only if necessary
43
    local _TAG=$(printf '%q' $TAG)
44
    if test ${_TAG:0:1} = '$'; then
45
        TAG=$(tr '\n' ' ' <<< $TAG | sed 's/  */ /g; s/ *$//')
46
    fi
ajout de gestion de bdd pour...
Sébastien MARQUE authored on 2020-08-24
47
}
48

            
complete rewrite
Sébastien MARQUE authored on 2020-11-07
49
function json () {
50
    jq --raw-output "$1" < ${2:-${json_file:?}}
51
}
52

            
53
rm -f $temppath/sqlite_request
ajout de gestion de bdd pour...
Sébastien MARQUE authored on 2020-08-24
54
function sqlite_request () {
complete rewrite
Sébastien MARQUE authored on 2020-11-07
55
    echo -e "## REQ $(( ++sqlite_request_count ))\n${1}\n" >> $temppath/sqlite_request
56
    sqlite3 "$in_ram_database" <<< "$1"
ré-écriture complète
Sébastien MARQUE authored on 2020-09-20
57
}
58

            
59
function xmlremovecomments () {
60
    sed -ri 's/<(!--|script>)/\n&/;s/(<\/script|--)>/&\n/' $setxml
61
    sed -ri '/<(script>|!--).*(<\/script|--)>/d;/<(script>|!--)/,/(<\/script|--)>/d' $setxml
complete rewrite
Sébastien MARQUE authored on 2020-11-07
62
    sed -i 's/\xef\xbb\xbf//;s/\r//' $setxml # removes BOM and ^M
ré-écriture complète
Sébastien MARQUE authored on 2020-09-20
63
}
64

            
65
function trap_break () {
66
    trap '' INT
67
    echo "stop requested"
ajout de gestion de bdd pour...
Sébastien MARQUE authored on 2020-08-24
68
}
69

            
70
function trap_exit () {
ré-écriture complète
Sébastien MARQUE authored on 2020-09-20
71
    trapped_rc=$?
72
    trap '' INT
complete rewrite
Sébastien MARQUE authored on 2020-11-07
73

            
74
    if declare -f on_exit > /dev/null; then
75
        on_exit
76
    fi
77

            
ré-écriture complète
Sébastien MARQUE authored on 2020-09-20
78
    if test ! -e $in_ram_database; then
79
        exit
80
    fi
many improvements
Sébastien MARQUE authored on 2020-09-02
81
    echo "updating installation status"
ré-écriture complète
Sébastien MARQUE authored on 2020-09-20
82
    for ac in $(sqlite_request 'select printf("%i:%s/%s", aircrafts.id, aircrafts.name, setxml.file)
83
                                from aircrafts inner join setxml
84
                                where aircrafts.id = setxml.variantof and setxml.installed != 0;'); do
many improvements
Sébastien MARQUE authored on 2020-09-02
85
        ac_path=${ac#*:}
complete rewrite
Sébastien MARQUE authored on 2020-11-07
86
        if test ! -e ${hangar[path]}/$ac_path-set.xml; then
many improvement
Sébastien MARQUE authored on 2020-09-08
87
            sqlite_request "update setxml set installed = 0 where file = '${ac_path#*/}' and variantof = ${ac%:*}"
88
        fi
89
    done
complete rewrite
Sébastien MARQUE authored on 2020-11-07
90
    for ac in ${hangar[path]}/*/*-set.xml; do
91
        ac=${ac/${hangar[path]}}
many improvement
Sébastien MARQUE authored on 2020-09-08
92
        sx=${ac##*/}
93
        ac=${ac%/*}
complete rewrite
Sébastien MARQUE authored on 2020-11-07
94
        if test -d ${hangar[path]}/$ac/.svn; then
many improvement
Sébastien MARQUE authored on 2020-09-08
95
            install_type=1
complete rewrite
Sébastien MARQUE authored on 2020-11-07
96
        elif test -d ${hangar[path]}/$ac/.git; then
many improvement
Sébastien MARQUE authored on 2020-09-08
97
            install_type=2
98
        else
99
            install_type=3
100
        fi
101
        sqlite_request "update setxml set installed = $install_type
102
                        where exists (
ré-écriture complète
Sébastien MARQUE authored on 2020-09-20
103
                            select 1
104
                            from aircrafts
105
                            where name = '${ac/\/}' and setxml.variantof = id
many improvement
Sébastien MARQUE authored on 2020-09-08
106
                        )"
many improvements
Sébastien MARQUE authored on 2020-09-02
107
    done
complete rewrite
Sébastien MARQUE authored on 2020-11-07
108
    local missing_setxml=$(sqlite_request "select printf(' - %s (%s)', aircrafts.name, hangars.name)
109
                                     from aircrafts inner join hangars
110
                                     where hangars.id = aircrafts.hangar and aircrafts.id not in (select variantof from setxml)")
ré-écriture complète
Sébastien MARQUE authored on 2020-09-20
111
    if test -n "$missing_setxml"; then
improve output for missing a...
Sébastien MARQUE authored on 2020-09-23
112
        echo -e "missing setxml config for :\n$missing_setxml"
ré-écriture complète
Sébastien MARQUE authored on 2020-09-20
113
    fi
complete rewrite
Sébastien MARQUE authored on 2020-11-07
114

            
115
    for data_presence_check in ${missing_data_check[@]}; do
make resilient data types + ...
Sébastien MARQUE authored on 2021-03-25
116
        if [[ -v datatypes[$data_presence_check] ]]; then
complete rewrite
Sébastien MARQUE authored on 2020-11-07
117
            local missing_data=$(sqlite_request "select count(setxml.file)
118
                                                 from aircrafts inner join setxml
119
                                                 where aircrafts.id = setxml.variantof and setxml.\`$data_presence_check\` = ''")
120
            if test $missing_data -gt 0; then
121
                echo "$missing_data aircrafts without $data_presence_check information"
122
                if test $missing_data -le 10; then
123
                    echo "aircrafts without $data_presence_check information:"
124
                    sqlite_request "select printf(' - %s/%s (%s)', aircrafts.name, setxml.file, hangars.name)
125
                                    from aircrafts inner join setxml, hangars
126
                                    where
127
                                        aircrafts.id = setxml.variantof
128
                                    and
129
                                        aircrafts.hangar = hangars.id
130
                                    and
131
                                        setxml.\`$data_presence_check\` = ''"
132
                fi
133
            fi
list missing models
Sébastien MARQUE authored on 2020-10-19
134
        fi
complete rewrite
Sébastien MARQUE authored on 2020-11-07
135
    done
136

            
ajout de gestion de bdd pour...
Sébastien MARQUE authored on 2020-08-24
137
    if test -r "$database" && md5sum $in_ram_database | sed "s,$in_ram_database,$database," | md5sum --status -c -; then
many improvements
Sébastien MARQUE authored on 2020-09-02
138
        echo "no changes in $database"
ajout de gestion de bdd pour...
Sébastien MARQUE authored on 2020-08-24
139
    elif test -w "$database"; then
complete rewrite
Sébastien MARQUE authored on 2020-11-07
140
        rm -f "$database"
141
        sqlite_request '.dump' | sqlite3 "$database"
many improvements
Sébastien MARQUE authored on 2020-09-02
142
        echo "database $database updated"
complete rewrite
Sébastien MARQUE authored on 2020-11-07
143
    elif test ! -e "$database" -a -w ${database%/*}; then
144
        sqlite_request '.dump' | sqlite3 "$database"
many improvements
Sébastien MARQUE authored on 2020-09-02
145
        echo "database $database created"
ajout de gestion de bdd pour...
Sébastien MARQUE authored on 2020-08-24
146
    else
many improvements
Sébastien MARQUE authored on 2020-09-02
147
        echo "nothing can be done with $database !"
ajout de gestion de bdd pour...
Sébastien MARQUE authored on 2020-08-24
148
    fi
better syntax for removal of...
Sébastien MARQUE authored on 2021-03-25
149
    find $temppath -type f -name "*-$tempid" -delete
ré-écriture complète
Sébastien MARQUE authored on 2020-09-20
150
}
151

            
152
function update_database () {
complete rewrite
Sébastien MARQUE authored on 2020-11-07
153
    sqlite_request "insert into aircrafts (name, author, revision, date, hangar)
154
                    select  name, author, revision, date, hangar from recover_aircrafts
155
                        where recover_aircrafts.name = '$ac' and recover_aircrafts.hangar = ${hangar[id]}
156
                    on conflict (name, hangar) where aircrafts.name = '$ac' and aircrafts.hangar = ${hangar[id]} do
157
                        update set
158
                            author   = (select author   from recover_aircrafts where name = '$ac'),
159
                            revision = (select revision from recover_aircrafts where name = '$ac'),
160
                            date     = (select date     from recover_aircrafts where name = '$ac')
161
                        where aircrafts.name = '$ac' and aircrafts.hangar = ${hangar[id]}"
ajout de gestion de bdd pour...
Sébastien MARQUE authored on 2020-08-24
162

            
complete rewrite
Sébastien MARQUE authored on 2020-11-07
163
    id=$(sqlite_request "select id from aircrafts where name is '${ac}' and hangar = ${hangar[id]}")
try to fix left to do count
Sébastien MARQUE authored on 2020-10-19
164

            
complete rewrite
Sébastien MARQUE authored on 2020-11-07
165
    echo $(sqlite_request "select printf('[ %i/%i ] $ac', count(sx), count(distinct ac)) from recover_setxml")
166

            
fix when aircraft updated mu...
Sébastien MARQUE authored on 2020-11-13
167
    for sx in $(sqlite_request "select distinct sx from recover_setxml where ac = '$ac'"); do
make resilient data types + ...
Sébastien MARQUE authored on 2021-03-25
168
        unset data
169
        declare -A data
complete rewrite
Sébastien MARQUE authored on 2020-11-07
170

            
remove -set.xml from filenam...
Sébastien MARQUE authored on 2020-09-02
171
        echo " -> $sx"
complete rewrite
Sébastien MARQUE authored on 2020-11-07
172
        getfromrepo ${ac}/$sx-set.xml > $setxml
ré-écriture complète
Sébastien MARQUE authored on 2020-09-20
173

            
complete rewrite
Sébastien MARQUE authored on 2020-11-07
174
        unset xmlgetnext_empty_tag property include include_rootpath ac_save
175
        while xmlgetnext; do
ré-écriture complète
Sébastien MARQUE authored on 2020-09-20
176
            if [[ "$TAG" =~ ^"PropertyList include=" ]]; then
177
                include_rootpath=${include%/*}
178
                test $include = $include_rootpath && unset include_rootpath
complete rewrite
Sébastien MARQUE authored on 2020-11-07
179

            
180
                eval $(echo ${TAG#* }) # include="..."
181

            
182
                if [[ "$include" =~ ^Aircraft/Generic/ ]]; then
183
                    unset include include_rootpath
184
                    continue
185

            
186
                elif [[ "$include" =~ ^'../' ]]; then
ré-écriture complète
Sébastien MARQUE authored on 2020-09-20
187
                    if test -n "$include_rootpath"; then
188
                        if [[ "$include_rootpath" =~ '/' ]]; then
189
                            include_rootpath=${include_rootpath%/*}
190
                        else
191
                            unset include_rootpath
192
                        fi
193
                    else
194
                        ac_save=$ac
195
                        unset ac
196
                    fi
197
                    include=${include/\.\.\/}
198
                fi
complete rewrite
Sébastien MARQUE authored on 2020-11-07
199
                getfromrepo ${ac}/${include_rootpath:+$include_rootpath/}$include >> $setxml
ré-écriture complète
Sébastien MARQUE authored on 2020-09-20
200
            fi
complete rewrite
Sébastien MARQUE authored on 2020-11-07
201
        done < $setxml
202

            
203
        test -n "$ac_save" && ac=$ac_save
204

            
205
# some aircrafts (mostly from the helijah's files architecture template)
206
# break because of infinite loop in middle of file
207
# I can't find the reason of this infinite loop
208
# this is the reason of this double-pass
209
        unset xmlgetnext_empty_tag property
210
        while xmlgetnext; do
211
        case "${TAG:0:1}" in
212
                ''|'?'|'!')
213
                    continue;;
214
                /)
215
                    property=${property%/*};;
216
                *)
217
                    if test "${TAG: -1}" != '/'; then
218
                        property+=/${TAG%% *}
219
                    fi;;
220
            esac
ré-écriture complète
Sébastien MARQUE authored on 2020-09-20
221

            
222
            if [[ "$property" = /PropertyList@($data_pattern) ]]; then
fix overriding descs with in...
Sébastien MARQUE authored on 2020-09-27
223
                if test -z "${data[${property/\/PropertyList}]}"; then
224
                    eval "data[${property/\/PropertyList}]=\"${VALUE//\"/\\\"}\""
225
                    data[${property/\/PropertyList}]=$(tr '\n' ' ' <<< ${data[${property/\/PropertyList}]} | sed -r 's/^\s*//;s/\s+/ /g;s/\s*$//')
226
                fi
many improvement
Sébastien MARQUE authored on 2020-09-08
227
            fi
ajout de gestion de bdd pour...
Sébastien MARQUE authored on 2020-08-24
228

            
ré-écriture complète
Sébastien MARQUE authored on 2020-09-20
229
            # continue parsing (while loop) until everything's found
make resilient data types + ...
Sébastien MARQUE authored on 2021-03-25
230
            for col in ${!datatypes[@]}; do
ré-écriture complète
Sébastien MARQUE authored on 2020-09-20
231
                test -z "${data[$col]}" && continue 2
232
            done
233
            break # everything's found
234
        done < $setxml
ajout de gestion de bdd pour...
Sébastien MARQUE authored on 2020-08-24
235

            
ré-écriture complète
Sébastien MARQUE authored on 2020-09-20
236
        if eval "test -z \"$data_test_null\""; then
237
            echo "WARNING: no info found, skipping"
complete rewrite
Sébastien MARQUE authored on 2020-11-07
238
            mkdir -p $temppath/no-data-ac
239
            cp -f $setxml $temppath/no-data-ac/${ac}-${sx}
ajout de gestion de bdd pour...
Sébastien MARQUE authored on 2020-08-24
240
        else
complete rewrite
Sébastien MARQUE authored on 2020-11-07
241
            insert_values="'$sx', $id, "
242
            insert_col='file, variantof, '
243
            update_values=''
ré-écriture complète
Sébastien MARQUE authored on 2020-09-20
244
            for col in ${!data[@]}; do
allow multiple types in DB (...
Sébastien MARQUE authored on 2021-03-29
245
                if test ${datatypes[$col]%% *} = 'text'; then
246
                    single_quote="'"
247
                elif [[ ${datatypes[$col]%% *} = 'integer' && "${data[$col]// }" = +([0-9]) ]]; then
248
                    single_quote=""
249
                else
250
                    unset datatypes[$col]
251
                    continue
252
                fi
complete rewrite
Sébastien MARQUE authored on 2020-11-07
253
                insert_col+="\`$col\`, "
make resilient data types + ...
Sébastien MARQUE authored on 2021-03-25
254
                insert_values+="$single_quote${data[$col]//\'/\'\'}$single_quote, "
complete rewrite
Sébastien MARQUE authored on 2020-11-07
255
                if test -n "${data[$col]}"; then
make resilient data types + ...
Sébastien MARQUE authored on 2021-03-25
256
                    update_values+="\`$col\` = $single_quote${data[$col]//\'/\'\'}$single_quote, "
complete rewrite
Sébastien MARQUE authored on 2020-11-07
257
                fi
ré-écriture complète
Sébastien MARQUE authored on 2020-09-20
258
            done
complete rewrite
Sébastien MARQUE authored on 2020-11-07
259
            sqlite_request "insert into setxml (${insert_col%,*}, installed) values (${insert_values%,*}, 0)
260
                            on conflict (file, variantof) where file = '$sx' and variantof = $id do
261
                                update set
262
                                    ${update_values%,*}, installed = 0
263
                                where
264
                                    file = '$sx' and variantof = $id"
ré-écriture complète
Sébastien MARQUE authored on 2020-09-20
265
        fi
complete rewrite
Sébastien MARQUE authored on 2020-11-07
266

            
267
        sqlite_request "delete from recover_setxml where ac = '$ac' and sx = '$sx'"
268
    done
269
}
270

            
271
function add_record () {
272
    ac_ass_array[$1]="$2"
273
}
274

            
275
function get_record () {
276
    if test -n "$1"; then
277
        echo "${ac_ass_array[$1]}"
278
    else
279
        for k in ${!ac_ass_array[@]}; do
280
            echo $k = ${ac_ass_array[$k]}
281
        done
282
    fi
283
}
284

            
285
function add_aircraft () {
286
    for key in name revision date author; do
better comment explanation
Sébastien MARQUE authored on 2020-11-20
287
        test -n "${ac_ass_array[$key]}" # exit if missing data (with the help of "set -e")
complete rewrite
Sébastien MARQUE authored on 2020-11-07
288
    done
fix issue with multiple revi...
Sébastien MARQUE authored on 2020-11-20
289
    local new_revision=$(sqlite_request "select revision from recover_aircrafts
290
                                         where name = '${ac_ass_array[name]}'")
291
    if test -z "${new_revision}"; then
292
        sqlite_request "insert into recover_aircrafts (name, revision, date, author, hangar)
293
                            values (
294
                            '${ac_ass_array[name]}',
295
                             ${ac_ass_array[revision]},
296
                             ${ac_ass_array[date]},
297
                            '${ac_ass_array[author]}',
298
                             ${hangar[id]})"
299
    elif test ${new_revision} -lt ${ac_ass_array[revision]//\"}; then
300
        sqlite_request "update recover_aircrafts
301
                        set
302
                            revision =  ${ac_ass_array[revision]},
303
                            date     =  ${ac_ass_array[date]},
304
                            author   = '${ac_ass_array[author]}',
305
                            hangar   =  ${hangar[id]}
306
                        where name = '${ac_ass_array[name]}'"
307
    fi
complete rewrite
Sébastien MARQUE authored on 2020-11-07
308
    for key in name revision date author; do
309
        ac_ass_array[$key]=''
ré-écriture complète
Sébastien MARQUE authored on 2020-09-20
310
    done
311
}
312

            
complete rewrite
Sébastien MARQUE authored on 2020-11-07
313
function add_setxml_for_aircraft () {
fix issue with multiple revi...
Sébastien MARQUE authored on 2020-11-20
314
    sqlite_request "insert into recover_setxml values ('$1', '${2/%-set.xml}')
315
                    on conflict (ac, sx) where ac = '$1' and sx = '${2/%-set.xml}'
316
                    do nothing"
complete rewrite
Sébastien MARQUE authored on 2020-11-07
317
}
318

            
ré-écriture complète
Sébastien MARQUE authored on 2020-09-20
319
function apply_revision () {
complete rewrite
Sébastien MARQUE authored on 2020-11-07
320
    for ac in $(sqlite_request "select name from recover_aircrafts"); do
321
        # delete aircrafts that have been deleted from the repo
322
        sqlite_request "delete from setxml
323
                        where (file, variantof) in (
324
                            select file, variantof from setxml
325
                              inner join aircrafts
326
                              where aircrafts.id = setxml.variantof
327
                                and aircrafts.name = '$ac'
328
                                and aircrafts.hangar = ${hangar[id]}
329
                                and setxml.file not in (
330
                                    select sx from recover_setxml where ac = '$ac'
331
                                )
332
                            )"
333

            
334
        # delete aircrafts without setxml found
335
        sqlite_request "delete from recover_aircrafts
336
                        where name not in (select distinct ac from recover_setxml)"
337

            
ré-écriture complète
Sébastien MARQUE authored on 2020-09-20
338
        update_database
complete rewrite
Sébastien MARQUE authored on 2020-11-07
339
        if test -d ${hangar[path]}/${ac}/.${hangar[type]} \
340
        && \
341
        case ${hangar[type]} in
342
           svn) test "$(svn info --show-item=url ${hangar[path]}/${ac})" != "${hangar[url]}/${ac}";;
fix git discovering of aircr...
Sébastien MARQUE authored on 2020-11-13
343
           git) test "$(git -C ${hangar[path]}/${ac} config --get remote.origin.url)" != "${hangar[url]}/${ac}.git";;
complete rewrite
Sébastien MARQUE authored on 2020-11-07
344
        esac \
345
        || test -d ${hangar[path]}/${ac} -a ! -d ${hangar[path]}/${ac}/.${hangar[type]}; then
346
            echo "INFO: local ${ac} installed out from repo" >&2
ajout de gestion de bdd pour...
Sébastien MARQUE authored on 2020-08-24
347
        fi
complete rewrite
Sébastien MARQUE authored on 2020-11-07
348
        sqlite_request "delete from recover_aircrafts where name = '$ac'"
ajout de gestion de bdd pour...
Sébastien MARQUE authored on 2020-08-24
349
    done
350
}
351

            
ré-écriture complète
Sébastien MARQUE authored on 2020-09-20
352
trap trap_break INT
ajout de gestion de bdd pour...
Sébastien MARQUE authored on 2020-08-24
353
trap trap_exit EXIT
354

            
ré-écriture complète
Sébastien MARQUE authored on 2020-09-20
355
stty -echoctl
356

            
complete rewrite
Sébastien MARQUE authored on 2020-11-07
357
declare -A hangar
make resilient data types + ...
Sébastien MARQUE authored on 2021-03-25
358
data_pattern=$(printf "%s|" ${!datatypes[@]})
ré-écriture complète
Sébastien MARQUE authored on 2020-09-20
359
data_pattern=${data_pattern:0:-1}
make resilient data types + ...
Sébastien MARQUE authored on 2021-03-25
360
data_test_null=$(printf '${data[%s]}' ${!datatypes[@]})
ré-écriture complète
Sébastien MARQUE authored on 2020-09-20
361

            
362
if test -e $database; then
363
    cp $database $in_ram_database
complete rewrite
Sébastien MARQUE authored on 2020-11-07
364

            
ré-écriture complète
Sébastien MARQUE authored on 2020-09-20
365
    sql_cols=$(sqlite_request "pragma table_info(setxml)" | awk -F'|' '{printf("%s %s ", $2, $3)}')
366
    script_cols="file text variantof integer "
make resilient data types + ...
Sébastien MARQUE authored on 2021-03-25
367
    for col in ${!datatypes[@]}; do
368
        script_cols+="$col ${datatypes["$col"]%% *} "
ré-écriture complète
Sébastien MARQUE authored on 2020-09-20
369
    done
370
    script_cols+="installed integer " # last space is important
371
    if test "$sql_cols" != "$script_cols"; then
372
        echo "ALERT: datbase version mismatch !"
373
        exit 1
374
    fi
complete rewrite
Sébastien MARQUE authored on 2020-11-07
375
    if sqlite_request '.tables' | grep -q 'recover_'; then
ré-écriture complète
Sébastien MARQUE authored on 2020-09-20
376
        echo "recovering from previous saved state"
complete rewrite
Sébastien MARQUE authored on 2020-11-07
377
        hangar[id]=$(sqlite_request "select hangar from recover_aircrafts limit 1")
378
        eval $(sqlite_request "select printf('hangar[name]=%s;hangar[url]=%s;hangar[type]=%s;hangar[source]=%s',
379
                                              name,           url,           type,           source)
380
                               from hangars
381
                               where id = '${hangar[id]}'")
382
        source $(grep -l "^\s*hangar\[name\]=${hangar[name]}\s*$" ${0%*/}.d/*.hangar)
383
        eval "getfromrepo () {$(declare -f getfromrepo | sed '1,2d;$d'); xmlremovecomments;}"
ré-écriture complète
Sébastien MARQUE authored on 2020-09-20
384
        apply_revision
385
        exit
386
    fi
387
fi
388

            
complete rewrite
Sébastien MARQUE authored on 2020-11-07
389
sqlite_request "create table if not exists hangars (
390
                    id     integer primary key,
391
                    name   text,
392
                    source text,
393
                    type   text,
394
                    url    text,
395
                    path   text,
396
                    active integer)"
397

            
398
sqlite_request 'create unique index if not exists "index_hangars" on hangars (url)'
399

            
ré-écriture complète
Sébastien MARQUE authored on 2020-09-20
400
sqlite_request "create table if not exists aircrafts (
cosmetics
Sébastien MARQUE authored on 2020-10-19
401
                    id       integer primary key,
402
                    name     text,
ré-écriture complète
Sébastien MARQUE authored on 2020-09-20
403
                    revision integer,
cosmetics
Sébastien MARQUE authored on 2020-10-19
404
                    date     integer,
complete rewrite
Sébastien MARQUE authored on 2020-11-07
405
                    author   text,
406
                    hangar   integer)"
407

            
408
sqlite_request 'create unique index if not exists "index_aircrafts" on aircrafts (name, hangar)'
ajout de gestion de bdd pour...
Sébastien MARQUE authored on 2020-08-24
409

            
ré-écriture complète
Sébastien MARQUE authored on 2020-09-20
410
sqlite_request "create table if not exists setxml (
411
                    file text,
412
                    variantof integer,
make resilient data types + ...
Sébastien MARQUE authored on 2021-03-25
413
                    $(for col in ${!datatypes[@]}; do printf "'%s' %s, " $col "${datatypes[$col]}"; done)
ré-écriture complète
Sébastien MARQUE authored on 2020-09-20
414
                    installed integer)"
ajout de gestion de bdd pour...
Sébastien MARQUE authored on 2020-08-24
415

            
complete rewrite
Sébastien MARQUE authored on 2020-11-07
416
sqlite_request 'create unique index if not exists "index_setxml" on setxml (file, variantof)'
417

            
418
for file in $(find ${0%*/}.d -type f -name "*.hangar"); do
419
    unset hangar error_message
420
    unset -f getfromrepo parse_repo_history
421
    declare -A hangar
422
    source $file
423

            
424
    test -n "${hangar[name]}"      \
425
      -a -n "${hangar[source]}"    \
426
      -a -n "${hangar[type]}"      \
427
      -a -n "${hangar[url]}"       \
428
      -a -n "${hangar[active]}" || \
429
        error_message="${error_message:+$error_message, }missing hangar data"
430

            
431
    declare -f getfromrepo > /dev/null || \
432
        error_message="${error_message:+$error_message, }missing getfromrepo function"
433

            
434
    declare -f parse_repo_history > /dev/null || \
435
        error_message="${error_message:+$error_message, }missing parse_repo_history function"
436

            
437
    if test -n "$error_message"; then
438
        echo "file $file isn't a valid hangar ($error_message)"
439
        continue
ré-écriture complète
Sébastien MARQUE authored on 2020-09-20
440
    fi
441

            
complete rewrite
Sébastien MARQUE authored on 2020-11-07
442
    sqlite_request "insert into hangars (name, source, type, url, path, active)
443
                    values (
444
                        '${hangar[name]}', '${hangar[source]}', '${hangar[type]}',
445
                        '${hangar[url]}',  '${hangar[path]}',    ${hangar[active]})
446
                    on conflict (url) where url = '${hangar[url]}' do
447
                    update set
448
                        name = '${hangar[name]}',
449
                        path = '${hangar[path]}',
450
                        active = ${hangar[active]}
451
                    where url = '${hangar[url]}'"
452
done
453

            
454
unset hangar
455
unset -f getfromrepo parse_repo_history
456
declare -A hangar ac_ass_array
457
for h_id in $(sqlite_request "select id from hangars where active = 1"); do
458

            
459
    sqlite_request 'create table if not exists recover_aircrafts (
460
                        name     text,
461
                        revision integer,
462
                        date     integer,
463
                        author   text,
464
                        hangar   integer)'
complete rewrite (use svn in...
Sébastien MARQUE authored on 2020-09-07
465

            
complete rewrite
Sébastien MARQUE authored on 2020-11-07
466
    sqlite_request 'create table if not exists recover_setxml (
467
                        ac text,
468
                        sx text)'
ré-écriture complète
Sébastien MARQUE authored on 2020-09-20
469

            
fix issue with multiple revi...
Sébastien MARQUE authored on 2020-11-20
470
    sqlite_request 'create unique index if not exists "index_recover_setxml" on recover_setxml (ac, sx)'
471

            
complete rewrite
Sébastien MARQUE authored on 2020-11-07
472
    eval $(sqlite_request "select printf('hangar[id]=%i;hangar[source]=%s;', id, source)
473
                           from hangars
474
                           where id = '${h_id}'")
475

            
476
    source $(grep -l "^\s*hangar\[source\]=${hangar[source]}\s*$" ${0%*/}.d/*.hangar)
477

            
478
    eval "getfromrepo () {$(declare -f getfromrepo | sed '1,2d;$d'); xmlremovecomments;}"
479

            
480
    echo -e "=${hangar[name]//?/=}=\n ${hangar[name]} \n=${hangar[name]//?/=}="
481

            
482
    latest_revision=$(( $(sqlite_request "select max(revision)
483
                                          from aircrafts inner join hangars
484
                                          where hangars.id = aircrafts.hangar and hangars.name = '${hangar[name]}'") + 1 ))
485

            
486
    parse_repo_history
487

            
488
    if declare -f on_exit > /dev/null; then
489
        on_exit
490
    fi
fix issue with multiple revi...
Sébastien MARQUE authored on 2020-11-20
491
    sqlite_request "drop index 'index_recover_setxml'"
complete rewrite
Sébastien MARQUE authored on 2020-11-07
492
    sqlite_request "drop table recover_aircrafts"
493
    sqlite_request "drop table recover_setxml"
494
done
ré-écriture complète
Sébastien MARQUE authored on 2020-09-20
495