Showing 1 changed files with 87 additions and 44 deletions
+87 -44
.fgfs/fgaddon
... ...
@@ -25,14 +25,25 @@ function sqlite_request () {
25 25
 }
26 26
 
27 27
 function trap_exit () {
28
+    echo "updating installation status"
29
+    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;'); do
30
+        ac_path=${ac#*:}
31
+        test ! -e $fgaddon_path/$ac_path
32
+        installed=$?
33
+        sqlite_request "update setxml set installed = $installed where file = '${ac_path#*/}' and variantof = ${ac%:*}"
34
+    done
28 35
     if test -r "$database" && md5sum $in_ram_database | sed "s,$in_ram_database,$database," | md5sum --status -c -; then
29 36
         rm -f $in_ram_database
37
+        echo "no changes in $database"
30 38
     elif test -w "$database"; then
31 39
         mv -f $in_ram_database "$database"
40
+        echo "database $database updated"
32 41
     elif ! test -e "$database"; then
33 42
         mv $in_ram_database "$database"
43
+        echo "database $database created"
34 44
     else
35 45
         rm -f $in_ram_database
46
+        echo "nothing can be done with $database !"
36 47
     fi
37 48
     rm -f $aircrafts $aircraft $setxml
38 49
 }
... ...
@@ -41,11 +52,11 @@ update_database () {
41 52
     let progress++
42 53
     echo "[ ${progress}${total:+/$total} ] $ac"
43 54
 
44
-    dbupdate=$(sqlite_request "select date from aircrafts where name is '$ac'")
55
+    dbupdate=$(sqlite_request "select revision from aircrafts where name is '$ac'")
45 56
     if test -z "$dbupdate"; then
46
-        sqlite_request "insert into aircrafts (name, date) values ('$ac', $update)"
47
-    elif test $dbupdate -ne $update; then
48
-        sqlite_request "update aircrafts set date = $update where name is '$ac'"
57
+        sqlite_request "insert into aircrafts (name, revision, date, author) values ('$ac', $revision, $revdate, '$revauthor')"
58
+    elif test $dbupdate -lt $revision; then
59
+        sqlite_request "update aircrafts set revision = $revision, author = '$revauthor' where name is '$ac'"
49 60
     fi
50 61
     id=$(sqlite_request "select id from aircrafts where name is '$ac'")
51 62
 
... ...
@@ -57,9 +68,6 @@ update_database () {
57 68
             get_attribute $attribute
58 69
         done
59 70
 
60
-        test -e $fgaddon_path/$ac/$sx
61
-        installed=$?
62
-
63 71
         if test -n "$description" -a -z "$flight_model"; then
64 72
             grep -qi 'jsbsim' <<< "$description" && flight_model='jsb'
65 73
             grep -qi 'yasim' <<< "$description" && flight_model='yasim'
... ...
@@ -75,16 +83,23 @@ update_database () {
75 83
                 fi
76 84
             done
77 85
         else
86
+            test ! -e $fgaddon_path/$ac/$sx
87
+            installed=$?
78 88
             sqlite_request "insert into setxml values ('$sx', $id, '$description', '$long_description', '$author', '$type', '$flight_model', $installed)"
79 89
         fi
80 90
     done
81 91
 }
82 92
 
93
+xmlgetnext () {
94
+   local IFS='>'
95
+   read -d '<' TAG VALUE
96
+}
97
+
83 98
 trap trap_exit EXIT
84 99
 
85 100
 test -e $database && cp $database $in_ram_database
86 101
 
87
-sqlite_request "create table if not exists aircrafts (id integer primary key, name text, date integer)"
102
+sqlite_request "create table if not exists aircrafts (id integer primary key, name text, revision integer, date integer, author text)"
88 103
 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)"
89 104
 
90 105
 test -z "$1" && echo "mode RSS"
... ...
@@ -92,72 +107,100 @@ test -z "$1" && echo "mode RSS"
92 107
 case ${1:-rss} in
93 108
     'full')
94 109
         echo "downloading the FGADDON list"
95
-        wget -qO- $fgaddon_url > $aircrafts
110
+        wget -qO- $fgaddon_url | sed -n '/<table>/,/<\/table>/p' > $aircrafts
96 111
 
97 112
         total=$(grep -c '<a class="icon" href=".*class="fa fa-folder".*i>&nbsp;' $aircrafts)
98 113
 
99
-        for ac in $(sed -rn 's/^\s*<a class="icon" href="(.+)" title=.* class="fa fa-folder".+$/\1/p' $aircrafts); do
100
-            update=$(grep -A3 '<a class="icon" href=".*class="fa fa-folder".*i>&nbsp;'$ac'<' $aircrafts | sed -rn '$s/^\s*<span title="(.+)">.*$/\1/p')
101
-            update=$(date +%s -d"$update")
102
-            update_database
103
-        done
114
+        while xmlgetnext; do
115
+            case "$TAG" in
116
+                'tr')
117
+                    unset ac revision revauthor
118
+                    ;;
119
+                'a class="icon" href='*)
120
+                    eval $(echo "${TAG#* }")
121
+                    ac=$href
122
+                    ;;
123
+                'span title='*)
124
+                    eval $(echo "${TAG#* }")
125
+                    revdate=$(date +%s -d "$title")
126
+                    ;;
127
+                'a href="/p/flightgear/fgaddon/'*)
128
+                    eval $(echo "${TAG#* }")
129
+                    revision=$(tr -cd '[:digit:]' <<< $href)
130
+                    ;;
131
+                'span class="icon emboss x16" style="text-align:center;" title="User"'*)
132
+                    user=1
133
+                    ;;
134
+                '/span')
135
+                    test -n "$user" && revauthor=$(tr -cd '[:alnum:]' <<< $VALUE) && unset user
136
+                    ;;
137
+                '/tr')
138
+                    test -n "$revision" -a -n "$revauthor" -a -n "$ac" -a -n "$revdate" && update_database
139
+                    ;;
140
+            esac
141
+        done < $aircrafts
104 142
     ;;
105 143
     'rss')
106
-        xmlgetnext () {                                                         
107
-           local IFS='>'
108
-           read -d '<' TAG VALUE
109
-        }
110
-
111 144
         get_updates_from_rss () {
112
-            wget -qO- https://sourceforge.net/p/flightgear/fgaddon/feed | while xmlgetnext; do
145
+            while xmlgetnext; do
113 146
                 case "$TAG" in
114 147
                     'item')
115 148
                         let entry++
116
-                        unset link pubDate revision title file;;
149
+                        unset link revdate revauthor revision title;;
117 150
                     'link')
118 151
                         link="$VALUE";;
119 152
                     'title')
120 153
                         title="$VALUE";;
154
+                    'dc:creator'*)
155
+                        revauthor="$VALUE";;
121 156
                     'guid isPermaLink="false"')
122
-                        revision=$(sed -r 's,^.+/([0-9]+)/$,\1,' <<< $VALUE);;
123
-                    'pubDate')
124
-                        pubDate=$(date +%s -d "$VALUE")
125
-                        if test $entry -eq 1 -a $pubDate -eq ${latest_update:-0}; then
157
+                        revision=$(sed -r 's,^.+/([0-9]+)/$,\1,' <<< $VALUE)
158
+                        if test $entry -eq 1 -a $revision -eq ${latest_revision:-0}; then
126 159
                             echo "no new updates" >&2
127 160
                             break
128 161
                         fi
129 162
                     ;;
163
+                    'pubDate')
164
+                        revdate=$(date +%s -d "$VALUE")
165
+                    ;;
130 166
                     '/item')
131
-                        echo "checking $revision ($(date +"%d %B" -d@$pubDate)): $title" >&2
132
-                        wget -qO- "$link" > $aircrafts
133
-                        for file in $(egrep '/trunk/Aircraft/.*/.*-set.xml$' $aircrafts); do
134
-                            file=${file/\/trunk\/Aircraft\/}
135
-                            echo ${file%/*}
136
-                            echo $pubDate
137
-                        done
167
+                        echo "$revision ($(date +"%d %B" -d@$revdate), $revauthor): $title" >&2
168
+                        wget -qO- "$link" | awk -v data="revision=$revision revdate=$revdate revauthor=$revauthor" '
169
+                            />\/trunk\/Aircraft\/.+</ {
170
+                                ac[gensub("^.+>/trunk/Aircraft/([^/]+).+<.*$", "ac=\\1", "1", $0)]++
171
+                            }
172
+                            END {
173
+                                for (i in ac) print data " " i
174
+                            }'
138 175
                     ;;
139 176
                     '/channel')
140
-                        if test ${latest_update:-0} -lt $pubDate; then
141
-                            echo "WARNING: $(( ($pubDate - ${latest_update:-0}) / 86400 )) days between the oldest RSS entry and last DB entry" >&2
142
-                            echo "a $0 full may be useful" >&2
177
+                        if test ${latest_revision:-0} -lt $revision; then
178
+                            echo "WARNING: $(( ($revision - ${latest_revision:-0}) )) revisions between the oldest RSS entry and last DB entry" >&2
179
+                            echo "a \`$0 full' may be useful" >&2
143 180
                         fi
144 181
                     ;;
145 182
                 esac
146
-            done
183
+            done < $aircrafts | awk '
184
+                {
185
+                    if (a[$4] == "") a[$4]=$0
186
+                }
187
+                END {
188
+                    for (i in a) {
189
+                        total++
190
+                        command = command a[i] " update_database;"
191
+                    }
192
+                    printf("total=%i;%s", total, command)
193
+                }'
147 194
         }
148
-        latest_update=$(sqlite_request "select max(date) from aircrafts")
149
-        ac_list=($(get_updates_from_rss))
150
-        total=$(( ${#ac_list[@]} / 2 ))
151
-        for ((i=0; i < ${#ac_list[@]}; i=i+2)); do
152
-            ac=${ac_list[i]}
153
-            update=${ac_list[i+1]}
154
-            update_database
155
-        done
195
+        latest_revision=$(sqlite_request "select max(revision) from aircrafts")
196
+        wget -qO- https://sourceforge.net/p/flightgear/fgaddon/feed > $aircrafts
197
+        eval "$(get_updates_from_rss)"
156 198
     ;;
157 199
         *)
158 200
             echo "usage: [DB=path/to/database] $0 [rss|full]"
159 201
             echo
160 202
             echo "rss: updates only last repo changes"
161 203
             echo "full: updates from all repo"
204
+            exit 1
162 205
     ;;
163 206
 esac