chg: build script can be used as post-update hook now
[exim-website.git] / script / build
1 #!/bin/bash
2 # Usage: [TARGET=…] $0 [ref]
3 # - Build from either the ref (or refs/heads/master) or from the current working directory
4 # - Can be used as a post-update hook
5
6 set -eu
7 shopt -s extglob
8
9 [[ -t 0 ]] || exec &>/tmp/website-$(date +%s).log
10
11 tmp=$(mktemp -d)
12 trap "rm -rf '$tmp'" EXIT INT
13
14 staging="$tmp/staging"                          # the temporary build dir
15 pubdir=${TARGET:-/srv/www/vhosts/www.exim.org}  # publish here
16
17 if [[ $(git rev-parse --is-inside-work-tree) != true ]]
18 then
19   workdir=$tmp/workdir
20   install -d "$workdir"
21   git --work-tree="$workdir" checkout -f refs/heads/master      # FIXME: use the receiving branch
22   cd "$workdir"
23 fi
24
25 if ! test -d "$pubdir"
26 then
27         echo "Warning: $pubdir does not exist. Did you forget to set the TARGET env in \"local\" mode?" >&2
28         exit 1
29 fi
30
31
32 if getent group eximdev
33 then install -m 02775 -g eximdev -d "$staging"
34 else install -d "$staging"
35 fi
36
37 cp -r --preserve=timestamps "$pubdir"/exim-+(html|pdf)-* "$staging/" ||:
38
39 # start working
40 latest=$(cd docbook && compgen -G '[45].*' | sort -V | tail -n1)
41
42 script/gen \
43   --web \
44   --spec docbook/[45]*/spec.xml \
45   --filter  docbook/[45]*/filter.xml \
46   --tmpl templates \
47   --latest "$latest" \
48   --docroot "$staging"
49
50 mv "$staging" "$pubdir.$$"              # may take some time (tmp -> data volume)
51 mv "$pubdir" "$pubdir.$(date -Isecond)" # backup
52 mv "$pubdir.$$" "$pubdir"               # should be fast
53
54 echo "*** updated into $pubdir"