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

            
3
fgaddon_url=https://sourceforge.net/p/flightgear/fgaddon/HEAD/tree/trunk/Aircraft
4
fgaddon_svn=https://svn.code.sf.net/p/flightgear/fgaddon/trunk/Aircraft
many improvement
Sébastien MARQUE authored on 2020-09-08
5
fgaddon_path=$HOME/.fgfs/flightgear-fgaddon/Aircraft
ajout de gestion de bdd pour...
Sébastien MARQUE authored on 2020-08-24
6

            
7
aircrafts=$(mktemp --dry-run /dev/shm/Aircraft-XXXXXXXXX)
8
aircraft=$(mktemp --dry-run /dev/shm/aircraft-XXXXXXX)
9
setxml=$(mktemp --dry-run /dev/shm/setxml-XXXXXXXX)
10
in_ram_database=$(mktemp --dry-run /dev/shm/XXXXXXX)
11
database=${DB:-$0.db}
12

            
many improvement
Sébastien MARQUE authored on 2020-09-08
13
attributes=(description long-description author flight-model type)
ajout de gestion de bdd pour...
Sébastien MARQUE authored on 2020-08-24
14

            
complete rewrite (use svn in...
Sébastien MARQUE authored on 2020-09-07
15
xmlgetnext () {
16
   local IFS='>'
17
   read -d '<' TAG VALUE
18
   TAG=$(tr '\n' ' ' <<< $TAG | sed 's/  */ /g; s/ *$//')
ajout de gestion de bdd pour...
Sébastien MARQUE authored on 2020-08-24
19
}
20

            
21
function sqlite_request () {
22
    sqlite3 "$in_ram_database" <<< "$1"
23
}
24

            
25
function trap_exit () {
many improvements
Sébastien MARQUE authored on 2020-09-02
26
    echo "updating installation status"
many improvement
Sébastien MARQUE authored on 2020-09-08
27
    for ac in $(sqlite_request 'select printf("%i:%s/%s", aircrafts.id, aircrafts.name, setxml.file) from aircrafts inner join setxml where aircrafts.id = setxml.variantof and setxml.installed != 0;'); do
many improvements
Sébastien MARQUE authored on 2020-09-02
28
        ac_path=${ac#*:}
many improvement
Sébastien MARQUE authored on 2020-09-08
29
        if test ! -e $fgaddon_path/$ac_path-set.xml; then
30
            sqlite_request "update setxml set installed = 0 where file = '${ac_path#*/}' and variantof = ${ac%:*}"
31
        fi
32
    done
33
    for ac in $fgaddon_path/*/*-set.xml; do
34
        ac=${ac/$fgaddon_path}
35
        sx=${ac##*/}
36
        ac=${ac%/*}
37
        if test -d $fgaddon_path/$ac/.svn; then
38
            install_type=1
39
        elif test -d $fgaddon_path/$ac/.git; then
40
            install_type=2
41
        else
42
            install_type=3
43
        fi
44
        sqlite_request "update setxml set installed = $install_type
45
                        where exists (
46
                            select 1 from aircrafts where name = '${ac/\/}' and setxml.variantof = id
47
                        )"
many improvements
Sébastien MARQUE authored on 2020-09-02
48
    done
ajout de gestion de bdd pour...
Sébastien MARQUE authored on 2020-08-24
49
    if test -r "$database" && md5sum $in_ram_database | sed "s,$in_ram_database,$database," | md5sum --status -c -; then
50
        rm -f $in_ram_database
many improvements
Sébastien MARQUE authored on 2020-09-02
51
        echo "no changes in $database"
ajout de gestion de bdd pour...
Sébastien MARQUE authored on 2020-08-24
52
    elif test -w "$database"; then
complete rewrite (use svn in...
Sébastien MARQUE authored on 2020-09-07
53
        sqlite_request "vacuum"
ajout de gestion de bdd pour...
Sébastien MARQUE authored on 2020-08-24
54
        mv -f $in_ram_database "$database"
many improvements
Sébastien MARQUE authored on 2020-09-02
55
        echo "database $database updated"
ajout de gestion de bdd pour...
Sébastien MARQUE authored on 2020-08-24
56
    elif ! test -e "$database"; then
57
        mv $in_ram_database "$database"
many improvements
Sébastien MARQUE authored on 2020-09-02
58
        echo "database $database created"
ajout de gestion de bdd pour...
Sébastien MARQUE authored on 2020-08-24
59
    else
60
        rm -f $in_ram_database
many improvements
Sébastien MARQUE authored on 2020-09-02
61
        echo "nothing can be done with $database !"
ajout de gestion de bdd pour...
Sébastien MARQUE authored on 2020-08-24
62
    fi
63
    rm -f $aircrafts $aircraft $setxml
64
}
65

            
66
update_database () {
67
    let progress++
68
    echo "[ ${progress}${total:+/$total} ] $ac"
69

            
many improvements
Sébastien MARQUE authored on 2020-09-02
70
    dbupdate=$(sqlite_request "select revision from aircrafts where name is '$ac'")
ajout de gestion de bdd pour...
Sébastien MARQUE authored on 2020-08-24
71
    if test -z "$dbupdate"; then
complete rewrite (use svn in...
Sébastien MARQUE authored on 2020-09-07
72
        sqlite_request "insert into aircrafts (name, revision, date, author)
73
                        values ('$ac', ${revision[$ac]}, ${revdate[$ac]}, '${revauthor[$ac]}')"
74
    elif test $dbupdate -lt ${revision[$ac]}; then
75
        sqlite_request "update aircrafts set
76
                            revision = ${revision[$ac]},
77
                            author   = '${revauthor[$ac]}',
78
                            date = ${revdate[$ac]}
79
                        where name is '$ac'"
ajout de gestion de bdd pour...
Sébastien MARQUE authored on 2020-08-24
80
    fi
81
    id=$(sqlite_request "select id from aircrafts where name is '$ac'")
82

            
complete rewrite (use svn in...
Sébastien MARQUE authored on 2020-09-07
83
    for sx in ${setxmlmodified[$ac]}; do
84
        test -n "${_sx[$ac/$sx]}" && continue
85
        _sx[$ac/$sx]="already done"
86
        unset description long_description author flight_model in_sim
remove -set.xml from filenam...
Sébastien MARQUE authored on 2020-09-02
87
        echo " -> $sx"
complete rewrite (use svn in...
Sébastien MARQUE authored on 2020-09-07
88
        svn export --quiet --force --revision ${revision[$ac]} $fgaddon_svn/$ac/$sx-set.xml $setxml
89
        while xmlgetnext; do
many improvement
Sébastien MARQUE authored on 2020-09-08
90
            if [[ "$TAG" = @($attributes_case) ]]; then
91
                test -n "$in_sim" && eval "${TAG//-/_}=\"$VALUE\""
92
                test -n "$description" \
93
                  -a -n "$long_description" \
94
                  -a -n "$author" \
95
                  -a -n "$flight_model" \
96
                  -a -n "$type" && break
97
            elif test "$TAG" = 'sim'; then
98
                in_sim=1
99
            elif test "$TAG" = '/sim'; then
100
                break
101
            fi
complete rewrite (use svn in...
Sébastien MARQUE authored on 2020-09-07
102
        done < $setxml
ajout de gestion de bdd pour...
Sébastien MARQUE authored on 2020-08-24
103

            
many improvement
Sébastien MARQUE authored on 2020-09-08
104
        test -z "$description" \
105
          -a -z "$long_description" \
106
          -a -z "$author" \
107
          -a -z "$flight_model" \
108
          -a -z "$type" && echo "WARNING: no info found, skipping" && continue
109

            
ajout de gestion de bdd pour...
Sébastien MARQUE authored on 2020-08-24
110
        if test -n "$description" -a -z "$flight_model"; then
111
            grep -qi 'jsbsim' <<< "$description" && flight_model='jsb'
112
            grep -qi 'yasim' <<< "$description" && flight_model='yasim'
113
        fi
114

            
complete rewrite (use svn in...
Sébastien MARQUE authored on 2020-09-07
115
        if test -n "$long_description"; then
116
            long_description=$(sed 's/^\s*//g' <<< $long_description | tr '\n' ' ')
117
        fi
118

            
ajout de gestion de bdd pour...
Sébastien MARQUE authored on 2020-08-24
119
        known=$(sqlite_request "select variantof from setxml where file is '$sx'")
120
        if test -n "$known"; then
121
            for attribute in ${attributes[@]}; do
122
                dbvalue=$(sqlite_request "select $attribute from setxml where file is '$sx'")
123
                eval "wgvalue=\$$attribute"
many improvement
Sébastien MARQUE authored on 2020-09-08
124
                if test "$dbvalue" != "$wgvalue" -a -n "$wgvalue"; then
ajout de gestion de bdd pour...
Sébastien MARQUE authored on 2020-08-24
125
                    sqlite_request "update setxml set $attribute = '$wgvalue' where file is '$sx'"
126
                fi
127
            done
128
        else
many improvement
Sébastien MARQUE authored on 2020-09-08
129
            sqlite_request "insert into setxml values ('$sx', $id, '$description', '$long_description', '$author', '$type', '$flight_model', 0)"
ajout de gestion de bdd pour...
Sébastien MARQUE authored on 2020-08-24
130
        fi
131
    done
132
}
133

            
134
trap trap_exit EXIT
135

            
136
test -e $database && cp $database $in_ram_database
137

            
many improvements
Sébastien MARQUE authored on 2020-09-02
138
sqlite_request "create table if not exists aircrafts (id integer primary key, name text, revision integer, date integer, author text)"
ajout de gestion de bdd pour...
Sébastien MARQUE authored on 2020-08-24
139
sqlite_request "create table if not exists setxml (file text, variantof integer, description text, long_description text, author text, type text, flight_model text, installed integer)"
140

            
many improvement
Sébastien MARQUE authored on 2020-09-08
141
attributes_case=$(printf "%s|" "${attributes[@]}")
142
attributes_case=${attributes_case:0:-1}
complete rewrite (use svn in...
Sébastien MARQUE authored on 2020-09-07
143
latest_revision=$(sqlite_request "select max(revision) from aircrafts")
144

            
145
echo "downloading FGADDON history from revision ${latest_revision:-0}"
146
svn log --revision ${latest_revision:-0}:HEAD --xml --verbose $fgaddon_svn > $aircrafts
147

            
148
while xmlgetnext; do
149
    case "$TAG" in
150
        'logentry revision='*)
151
            eval $(echo ${TAG#* })
152
            unset revdate revauthor ac path_list setxmlmodified
153
        ;;
154
        'author')
155
            revauthor=$VALUE
156
        ;;
157
        'date')
158
            revdate=$(date +%s -d "$VALUE")
159
        ;;
160
        'path '*)
161
            path_list[${#path_list[@]}]="$VALUE"
162
        ;;
163
        '/logentry')
164
            revlog[${#revlog[@]}]=$(
165
                printf '%s\n' "${path_list[@]}" | awk -F/ -v revision=$revision -v revauthor=$revauthor -v revdate=$revdate -v dq='"' '
many improvements
Sébastien MARQUE authored on 2020-09-02
166
                {
complete rewrite (use svn in...
Sébastien MARQUE authored on 2020-09-07
167
                    a[$4]++
168
                    if ($5 ~ "-set.xml$") b[$4]=b[$4] " " gensub("-set.xml$", "", "1", $5)
many improvements
Sébastien MARQUE authored on 2020-09-02
169
                }
170
                END {
complete rewrite (use svn in...
Sébastien MARQUE authored on 2020-09-07
171
                    for (i in a)
172
                        printf("<revision[%s]=%i;revauthor[%s]=%s;revdate[%s]=%i;%s>",
173
                                i, revision,
174
                                i, revauthor,
175
                                i, revdate,
176
                                b[i] != "" ? "setxmlmodified[" i "]=" dq gensub("^ ", "", "g", b[i]) " ${setxmlmodified[" i "]}" dq ";" : "")
177
                }')
178
        ;;
179
        '/log')
180
            unset revision revauthor revdate setxmlmodified
181
            declare -A revision revauthor revdate setxmlmodified
182
            analyse () {
183
                while xmlgetnext; do
184
                    test -z "$TAG" -a ${#revision[@]} -eq 0 && continue
185
                    case $TAG in
186
                        'BEGIN') continue;;
187
                        'END'  ) break 2;;
188
                    esac
189
                    echo $TAG
190
                done <<< "<BEGIN>${revlog[@]}<END>"
191
            }
192
            eval $(analyse)
193
            total=${#revision[@]}
194
            declare -A _sx
195
            for ac in "${!revision[@]}"; do
196
                update_database
many improvement
Sébastien MARQUE authored on 2020-09-08
197
                if test -d $fgaddon_path/$ac/.svn \
198
                && test "$(svn info --show-item=url $fgaddon_path/$ac)" != "$fgaddon_svn/$ac" \
199
                || test -d $fgaddon_path/$ac -a ! -d $fgaddon_path/$ac/.svn; then
200
                    echo "INFO: local $ac installed out from repo" >&2
201
                fi
complete rewrite (use svn in...
Sébastien MARQUE authored on 2020-09-07
202
            done
203
            break
204
        ;;
205
    esac
206
done < $aircrafts