4851ed5b890ba2258dc036958a53bb4c8828fbec
[samba-web.git] / generated_news.sh
1 #!/bin/bash
2 #
3
4 LC_ALL=C
5 export LC_ALL
6 LANG=C
7 export LANG
8 LANGUAGE=C
9 export LANGUAGE
10
11 set -u
12 set -e
13 umask 0022
14
15 generate() {
16         local dst="$1"
17         local count="$2"
18         local filter="$3"
19
20         test "${dst}" -nt "posted_news/" && {
21                 echo "${dst}: up to date"
22                 return 0
23         }
24
25         echo "${dst}: regenerating"
26         files=$(find posted_news/ -type f -regex "${filter}" -printf "%f\n" | sort -r | xargs)
27         {
28                 for f in ${files}; do
29                         if [[ "$filter" == *snip* ]] && [[ "$f" == *".body.html"* ]] && [[ "$files" == *"${f%.body.html}.snip.html"* ]]; then
30                                 continue
31                         fi
32                         echo "<!--#include virtual=\"/samba/posted_news/${f}\" -->"
33                         count=$((count-1))
34                         if [[ $count == 0 ]]; then break; fi
35                 done
36         } > ${dst}
37
38         return 0
39 }
40
41 generate_feed() {
42         local dst="$1"
43         local count="$2"
44         local filter="$3"
45
46         test "${dst}" -nt "posted_news/" && {
47                 echo "${dst}: up to date"
48                 return 0
49         }
50
51         echo "${dst}: regenerating"
52         files=$(find posted_news/ -type f -regex "${filter}" -printf "%f\n" | sort -r | xargs)
53         {
54                 echo '<?xml version="1.0" encoding="utf-8"?>'
55                 echo '<feed xmlns="http://www.w3.org/2005/Atom">'
56                 echo '<id>https://www.samba.org/samba/news.atom</id>'
57                 echo '<link href="https://pubsubhubbub.appspot.com/" rel="hub" />'
58                 echo '<link href="https://www.samba.org/samba/news.atom" rel="self" />'
59                 echo '<title>Samba - News</title>'
60                 echo '<updated>'`date +%Y-%m-%dT%H:%m:%SZ`'</updated>'
61                 echo
62                 for f in ${files}; do
63                         if [[ "$filter" == *snip* ]] && [[ "$f" == *".body.html"* ]] && [[ "$files" == *"${f%.body.html}.snip.html"* ]]; then
64                                 continue
65                         fi
66                         echo '<entry>'
67                         MY_UPDATED=`echo $f | sed 's/^\(....\)\(..\)\(..\)-\(..\)\(..\).*/\1-\2-\3T\4:\5:00Z/'`
68                         echo "<id>https://www.samba.org/samba/news.atom#$MY_UPDATED</id>"
69                         echo "<link rel='alternate' href='https://www.samba.org/samba/latest_news.html' />"
70                         echo '<author><name>Samba Team</name><email>webmaster@samba.org</email></author>'
71                         echo "<updated>$MY_UPDATED</updated>"
72
73                         cat posted_news/$f \
74                                 | perl -pe '$/=""; s/<p class=headline>(.*?)<\/p>/<title>$1<\/title>/s' \
75                                 | grep -v '<h5>' \
76                                 | grep -v '<!--' \
77                                 | sed 's:</\?br>:<br />:ig' \
78                                 | sed "/<\/title>/a <content type='xhtml' xml:base='https:\/\/www.samba.org\/samba\/news.atom'><div xmlns='http:\/\/www.w3.org\/1999\/xhtml'>"
79                         echo '</div></content></entry>'
80                         count=$((count-1))
81                         if [[ $count == 0 ]]; then break; fi
82                 done
83                 echo '</feed>'
84         } > ${dst}.tmp
85         mv ${dst}.tmp ${dst}
86
87         return 0
88 }
89
90 generate_latest_stable_release() {
91         local dst="$1"
92         local download_url="$2"
93
94         pushd history >/dev/null
95         ALL_VERSIONS=$(ls samba-*.html | cut -d '-' -f2- | cut -d '.' -f1-3 | sort -t. -k 1,1n -k 2,2n -k 3,3n -u)
96         LATEST_VERSION=$(echo "${ALL_VERSIONS}" | tail -1)
97         popd >/dev/null
98
99         echo "LATEST_VERSION: ${LATEST_VERSION}"
100
101         local tgz="samba-${LATEST_VERSION}.tar.gz"
102         local asc="samba-${LATEST_VERSION}.tar.asc"
103         local release_notes="history/samba-${LATEST_VERSION}.html"
104
105         test "${dst}" -nt "${release_notes}" && {
106                 echo "${dst}: up to date"
107                 return 0
108         }
109
110         echo "${dst}: regenerating"
111         {
112                 echo "<!-- BEGIN: ${dst} -->"
113                 echo "<p>"
114                 echo "<a href=\"${download_url}/${tgz}\">Samba ${LATEST_VERSION} (gzipped)</a><br>"
115                 echo "<a href=\"/samba/${release_notes}\">Release Notes</a> &middot;"
116                 echo "<a href=\"${download_url}/${asc}\">Signature</a>"
117                 echo "</p>"
118                 echo "<!-- END: ${dst} -->"
119
120         } > ${dst}.tmp
121         mv ${dst}.tmp ${dst}
122 }
123
124 generate "generated_news/latest_10_headlines.html" "10" ".*\.headline\.html"
125 generate "generated_news/latest_10_bodies.html" "10" ".*\.body\.html"
126 generate "generated_news/latest_2_bodies.html" "2" ".*\.\(snip\|body\)\.html"
127 generate_feed "news.atom" "10" ".*\.\(snip\|body\)\.html"
128
129 if ! `w3m -dump 'https://validator.w3.org/feed/check.cgi?url=https%3A%2F%2Fwww.samba.org%2Fsamba%2Fnews.atom' | grep -q Congratulations`
130 then
131         {
132         echo "NEWS feed error? Maybe a recent web repo checkin contained wrong html"
133         echo "Check https://validator.w3.org/feed/check.cgi?url=https%3A%2F%2Fwww.samba.org%2Fsamba%2Fnews.atom"
134         } | mail -s "ATOM feed eror" janger@samba.org root@samba.org
135 fi
136
137
138 download_url="https://download.samba.org/pub/samba/stable"
139 generate_latest_stable_release "generated_news/latest_stable_release.html" "${download_url}"