1#!/bin/bash
2
3INTERACTIVE=
4
5usage () {
6    echo "usage: $0 [-i] rev changelog_dev changelog_full" >&2
7    exit 1
8}
9
10PAUSE () {
11    if [ -n "$INTERACTIVE" ]; then
12        read -p "Hit Enter to continue"
13    fi
14}
15
16# === main ===
17
18if [[ $1 = "-i" ]]; then
19    INTERACTIVE=1
20    shift
21fi
22
23rev=$1
24shift
25
26changelog_dev=$1
27shift
28
29changelog_full=$1
30shift
31
32case "$rev" in
33    "") ;;
34    *.*.*) ;;
35    *) usage ;;
36esac
37
38[ -f "$changelog_dev" ] || {
39    echo "$changelog_dev: file not found" >&2
40    usage
41}
42
43[ -f "$changelog_full" ] || {
44    echo "$changelog_full: file not found" >&2
45    usage
46}
47
48D=$(cd $(dirname "$0"); /bin/pwd)
49
50[[ -f $D/build.cfg ]] || {
51    echo "missing build.cfg" >&2
52    exit 1
53}
54. "$D"/build.cfg
55
56# sanity check config
57for v in WWW_{HOST,DIR} MAC_{HOST,DIR}{32,64}; do
58    [[ -v $v ]] || {
59        echo "missing build.cfg variable $v!" >&2
60        exit 1
61    }
62done
63
64# convert changelog paths to absolute paths
65case $changelog_dev in
66    /*) ;;
67    *) changelog_dev=$(/bin/pwd)/$changelog_dev ;;
68esac
69case $changelog_full in
70    /*) ;;
71    *) changelog_full=$(/bin/pwd)/$changelog_full ;;
72esac
73
74set -e
75
76# change to top-level phd2 dir
77cd "$D"/..
78
79git checkout -q master
80git pull -r
81
82REV="$rev"
83
84do_locales=1
85do_phd_h=1
86do_help=1
87do_build=1
88do_upload=1
89do_changelog_dev=1
90do_changelog_full=1
91do_changelog=1
92do_release=1
93
94# these will be set automatically
95need_push=
96need_tag=
97need_static_update=
98
99PAUSE
100
101#
102# update translation templates
103#
104if [[ $do_locales ]]; then
105    ./build/locales_update_win.sh
106    git status -uno --porcelain | awk '$1=="M"{print $2}' | xargs git commit -m "update translation templates"
107    need_push=1
108fi
109
110#
111# update version in phd.h
112#
113if [[ $do_phd_h ]]; then
114    echo "updating rev to $REV"
115    cp phd.h /tmp/
116    awk -v q='"' -v REV=$REV '
117        /^#define PHDVERSION/ { print "#define PHDVERSION _T(" q REV q ")"; next }
118        /^#define PHDSUBVER/  { print "#define PHDSUBVER _T(" q q ")"; next }
119                              { print }
120    ' /tmp/phd.h > phd.h
121    # normalize line endings
122    dos2unix phd.h
123    unix2dos phd.h
124    git commit -m "bump rev to $REV" phd.h
125    need_push=1
126fi
127
128#
129# update help file
130#
131if [[ $do_help ]]; then
132    echo "building help files"
133    ./build/build_help.sh -w
134    scp tmp/PHD2_User_Guide.pdf ${WWW_HOST}:${WWW_DIR}/PHD2-${REV}_User_Guide.pdf
135    scp tmp/man.tar.gz ${WWW_HOST}:${WWW_DIR}/man-${REV}.tar.gz
136    ssh $WWW_HOST "cd $WWW_DIR && mkdir man-${REV} && tar xfz man-${REV}.tar.gz -C man-${REV} && ln -sfn man-${REV} man-dev && ln -sfn man-${REV} man && ln -sfn PHD2-${REV}_User_Guide.pdf PHD2_User_Guide.pdf"
137    ssh $WWW_HOST "chmod -R o+rX ${WWW_DIR}/PHD2-${REV}_User_Guide.pdf ${WWW_DIR}/man-${REV}.tar.gz ${WWW_DIR}/man-${REV}"
138fi
139
140#
141# push to repo if needed
142#
143if [[ $need_push ]]; then
144    echo "push to repo"
145    PAUSE
146    git push
147    need_tag=1
148fi
149
150if [[ $do_build ]]; then
151
152    macpids=
153
154    #
155    # start OSX builds
156    #
157    if [[ $do_upload ]]; then
158        OSX_UPLOAD="-u"
159    fi
160    scp "$D"/build.cfg ${MAC_HOST32}:${MAC_DIR32}/build/
161    ssh $MAC_HOST32 "cd $MAC_DIR32 && git checkout -q master && git pull -r && ./build/publish_osx $OSX_UPLOAD 32" &
162    macpids="$macpids $!"
163    scp "$D"/build.cfg ${MAC_HOST64}:${MAC_DIR64}/build/
164    ssh $MAC_HOST64 "cd $MAC_DIR64 && git checkout -q master && git pull -r && ./build/publish_osx $OSX_UPLOAD 64" &
165    macpids="$macpids $!"
166
167    #
168    # start win build
169    #
170    if [[ $do_upload ]]; then
171        WIN_UPLOAD="-u"
172    fi
173    ./build/publish_win $WIN_UPLOAD
174
175    wait $macpids
176fi
177
178#
179# web site dev release page
180#
181if [[ $do_changelog_dev ]]; then
182    echo "adding dev release web page. Change Log:"
183    cat "$changelog_dev"
184    PAUSE
185    #changelog_test="-t"
186    scp "$changelog_dev" "${WWW_HOST}:/tmp/phd2changelog"
187    ssh $WWW_HOST "./bin/phd2_add_release_page -r $REV $changelog_test /tmp/phd2changelog"
188    need_static_update=1
189fi
190
191#
192# web site major release page
193#
194if [[ $do_changelog_full ]]; then
195    echo "adding full release web page. Change Log:"
196    cat "$changelog_full"
197    PAUSE
198    #changelog_test="-t"
199    scp "$changelog_full" "${WWW_HOST}:/tmp/phd2changelog"
200    ssh $WWW_HOST "./bin/phd2_add_release_page -r $REV -R $changelog_test /tmp/phd2changelog"
201    need_static_update=1
202fi
203
204if [[ $need_static_update ]]; then
205    echo "generating static site"
206    PAUSE
207    ssh $WWW_HOST "./bin/phd2_generate_static_site"
208fi
209
210#
211# tag the release
212#
213if [[ $need_tag ]]; then
214    echo "tagging the release"
215    PAUSE
216    git tag -a -m "v${REV}" "v${REV}"
217    git push --tags
218fi
219
220#
221# update auto-upgrade release files
222#
223if [[ $do_release ]]; then
224    echo "updating auto-upgrade release tags"
225    PAUSE
226    ./build/publish_win -r -d
227    ./build/publish_win -r
228    ssh $MAC_HOST64 "cd $MAC_DIR64 && ./build/publish_osx -r -d"
229    ssh $MAC_HOST64 "cd $MAC_DIR64 && ./build/publish_osx -r"
230    ssh $WWW_HOST "cd $WWW_DIR && cp release-dev-osx.txt release-dev-linux.txt && chmod o+r release-dev-linux.txt"
231    ssh $WWW_HOST "cd $WWW_DIR && cp release-main-osx.txt release-main-linux.txt && chmod o+r release-main-linux.txt"
232fi
233