NEWS[4.16.0]: Samba 4.16.0 Available for Download
[samba-web.git] / prepare_news.sh
1 #!/bin/bash
2 #
3
4 SAVE_LC_ALL="${LC_ALL}"
5 SAVE_LANG="${LANG}"
6 SAVE_LANGUAGE="${LANGUAGE}"
7
8 LC_ALL=C
9 export LC_ALL
10 LANG=C
11 export LANG
12 LANGUAGE=C
13 export LANGUAGE
14
15 set -u
16 set -e
17 umask 0022
18
19 test -d ".git" || {
20         echo "Run this script from the top-level directory in the"
21         echo "repository"
22         exit 1
23 }
24
25 usage() {
26         echo "usage: $0 <NAME> <HEADLINE>"
27         echo ""
28         echo "This will open \$EDITOR in order to fill in the body content"
29         echo ""
30 }
31
32 test -n "${EDITOR-}" || {
33         usage
34         exit 1
35 }
36
37 NAME=${1-}
38 test -n "${NAME}" || {
39         usage
40         exit 1
41 }
42 shift 1
43 HEADLINE=$(echo "$@" | xargs)
44 test -n "${HEADLINE}" || {
45         usage
46         exit 1
47 }
48
49 grep -q "<a name=\"${NAME}\"" posted_news/*.body.html && {
50         echo "NAME[${NAME}] is not unique, choose another name"
51         grep "<a name=\"${NAME}\"" posted_news/*.body.html
52         exit 1
53 }
54
55 nd=$(git diff -p --stat HEAD | wc -l)
56 test x"${nd}" = x"0" || {
57         echo "You have uncommited changes your working tree"
58         git status
59         exit 1
60 }
61
62 trap_handler() {
63         echo ""
64         echo "ERROR: cleaning up"
65         echo ""
66
67         for f in ${CLEANUP_FILES}; do
68                 echo "Removing file[${f}]"
69                 test -f "${f}" && {
70                         rm "${f}" || {
71                                 echo "failed to remove ${f}"
72                         }
73                 }
74         done
75
76         test -n "${CLEANUP_RESET_COMMIT}" && {
77                 echo "Reverting to commit[${CLEANUP_RESET_COMMIT}]"
78                 git reset "${CLEANUP_RESET_COMMIT}"
79         }
80 }
81
82 CLEANUP_FILES=""
83 trap trap_handler INT QUIT TERM EXIT
84
85 href="#${NAME}"
86 utcdate=$(date --utc +"%d %B %Y")
87 utctime=$(date --utc +"%Y%m%d-%H%M%S")
88
89 headlinefile="posted_news/${utctime}.${NAME}.headline.html"
90 bodyfile="posted_news/${utctime}.${NAME}.body.html"
91 snipfile=""
92 echo "generating ${headlinefile}"
93 CLEANUP_FILES="${CLEANUP_FILES} ${headlinefile}"
94 {
95         echo "<!-- BEGIN: ${headlinefile} -->"
96         echo "<li> ${utcdate} <a href=\"${href}\">${HEADLINE}</a></li>"
97         echo "<!-- END: ${headlinefile} -->"
98 } > ${headlinefile}
99
100 echo "generating ${bodyfile}"
101 CLEANUP_FILES="${CLEANUP_FILES} ${bodyfile}"
102 {
103         echo "<!-- BEGIN: ${bodyfile} -->"
104         echo "<h5><a name=\"${NAME}\">${utcdate}</a></h5>"
105         echo "<p class=headline>${HEADLINE}</p>"
106         echo "<p>"
107         echo "<!-- TODO: add your content here -->"
108         echo "</p>"
109         echo "<p>"
110         echo "<!-- TODO: add more content here -->"
111         echo "</p>"
112         echo "<!-- SNIP -->"
113         echo "<!-- TODO: remove the above line or move it to where you want the front-page preview to end, also remove this comment -->"
114         echo "<!-- END: ${bodyfile} -->"
115 } > ${bodyfile}
116
117 LC_ALL="${SAVE_LC_ALL}" \
118 LANG="${SAVE_LANG}" \
119 LANGUAGE="${SAVE_LANGUAGE}" \
120 ${EDITOR} ${bodyfile}
121
122 if grep -q "<!-- SNIP -->" $bodyfile; then
123         snipfile="posted_news/${utctime}.${NAME}.snip.html"
124         echo "generating ${snipfile}"
125         CLEANUP_FILES="${CLEANUP_FILES} ${snipfile}"
126         {
127                 echo "<!-- BEGIN: ${snipfile} -->"
128                 while read -r line || [[ -n "$line" ]]; do
129                         if [[ $line == *"<!-- BEGIN"* ]]; then
130                                 continue
131                         elif [[ $line == *"<!-- SNIP -->"* || $line == *"<!-- END"* ]]; then
132                                 echo ${line%%<!--*}"<p><a href=\"/samba/latest_news.html#${NAME}\">(Read more)</a></p>"
133                                 break
134                         else
135                                 echo $line
136                         fi
137                 done <"${bodyfile}"
138                 echo "<!-- END: ${snipfile} -->"
139         } > ${snipfile}
140 fi
141
142 CLEANUP_FILES=""
143 CLEANUP_RESET_COMMIT="HEAD"
144 git add ${headlinefile} ${bodyfile} ${snipfile}
145 git commit --signoff --message "NEWS[${NAME}]: ${HEADLINE}"
146 CLEANUP_RESET_COMMIT="HEAD^"
147
148 echo ""
149 echo "Once you have pushed the commit a cronjob updates"
150 echo "the webserver content every 5-10 mins."
151 echo ""
152 echo "Please verify the commit carefully before pushing:"
153 echo ""
154 echo "  git show -p --stat HEAD"
155 echo "  git push ..."
156 echo ""
157
158 trap - INT QUIT TERM EXIT
159 exit 0