datefmt: Define 1 year as 365.2425 days instead of 365.25
[sunny256-utils.git] / build-postgres
blobf6e5da7da2bd434a63e93b5023dfe76d1d44003a
1 #!/usr/bin/env bash
3 #=======================================================================
4 # build-postgres
5 # File ID: 86126f82-2e60-11e5-bf33-fefdb24f8e10
7 # Compile and install the Postgres version stored in $srcdir .
9 # Author: Øyvind A. Holm <sunny@sunbase.org>
10 # License: GNU General Public License version 2 or later.
11 #=======================================================================
13 progname=build-postgres
14 VERSION=0.3.0
16 ARGS="$(getopt -o "\
21 " -l "\
22 help,\
23 quiet,\
24 update,\
25 verbose,\
26 version,\
27 " -n "$progname" -- "$@")"
28 test "$?" = "0" || exit 1
29 eval set -- "$ARGS"
31 opt_help=0
32 opt_quiet=0
33 opt_update=0
34 opt_verbose=0
35 while :; do
36 case "$1" in
37 -h|--help) opt_help=1; shift ;;
38 -q|--quiet) opt_quiet=$(($opt_quiet + 1)); shift ;;
39 -u|--update) opt_update=1; shift ;;
40 -v|--verbose) opt_verbose=$(($opt_verbose + 1)); shift ;;
41 --version) echo $progname $VERSION; exit 0 ;;
42 --) shift; break ;;
43 *) echo $progname: Internal error >&2; exit 1 ;;
44 esac
45 done
46 opt_verbose=$(($opt_verbose - $opt_quiet))
48 if test "$opt_help" = "1"; then
49 test $opt_verbose -gt 0 && { echo; echo $progname $VERSION; }
50 cat <<END
52 Usage: $progname [options]
54 Options:
56 -h, --help
57 Show this help.
58 -q, --quiet
59 Be more quiet. Can be repeated to increase silence.
60 -u, --update
61 Fetch new commits and update the source code.
62 -v, --verbose
63 Increase level of verbosity. Can be repeated.
64 --version
65 Print version information.
67 END
68 exit 0
71 msg() {
72 unset no_lf
73 if test "$1" = "-n"; then
74 # If -n is first argument, don't terminate with \n
75 local no_lf="-n"
76 shift
78 if test "$1" = "-s"; then
79 # If -s is specified, skip initial \n
80 shift
81 else
82 echo >&2
84 echo $no_lf "$progname: $*" >&2
85 return
88 srcdir="$HOME/src/other/postgres"
89 cd "$srcdir" || exit 1
91 if test "$opt_update" = "1"; then
92 git fetch origin
93 git merge --ff-only
94 git allbr origin
95 shift
98 user=sunny
99 pgdesc="$(git desc --long --tags)"
100 pgdir="postgres-$pgdesc"
101 prefix="/usr/local"
102 prgdir="$prefix/prg"
103 destdir="$prefix/varprg/$pgdir"
104 dbroot="/var/lib/postgresql"
105 dumpfile="$dbroot/$(date -u +"%Y%m%dT%H%M%SZ").all.pgdump.gz"
107 test -e "$destdir" && {
108 sudo rmdir "$destdir" || {
109 echo $progname: $destdir already exists >&2
110 exit 1
114 test -e "$dbroot/$pgdesc/main" && {
115 echo $progname: $dbroot/$pgdesc/main already exists >&2
116 exit 1
119 git wait-until-clean
120 msg Remove ignored files from $(pwd)/
121 git clean -fxd || {
122 msg \"git wait-until-clean\" or \"git clean\" failed
123 exit 1
126 old_version="$(
127 $prgdir/postgres/bin/postgres --version 2>/dev/null | fmt -1 | tail -1
130 if [ -n "$old_version" ]; then
131 msg git status in $(pwd)
132 GIT_PAGER=cat git status
133 unset choice
134 until [ "$choice" = "y" ]; do
135 echo
136 unset choice
137 echo $(
138 git log --format=oneline $old_version..$pgdesc | wc -l
139 ) new commits available in range $old_version..$pgdesc
140 echo Going to compile Postgres $pgdesc
141 echo
142 echo If that looks okay to you, press \'y\' to start the build, or:
143 echo \'d\' to diff
144 echo \'ds\' to diff --stat
145 echo \'dw\' to word-diff
146 echo \'l\' to list log
147 echo \'ld\' to list current databases
148 echo \'lp\' to list log with patch
149 echo \'lt\' to list log with commit tree
150 echo \'lw\' to list log with patch using word diff
151 echo \'n\' to abort
152 echo \'p\' to push new commits
153 echo \'t\' to show commit tree
154 read choice
155 [ "$choice" = "d" ] && git diff $old_version $pgdesc
156 [ "$choice" = "ds" ] && git diff --stat $old_version $pgdesc
157 [ "$choice" = "dw" ] && git diff --word-diff $old_version $pgdesc
158 [ "$choice" = "l" ] && git log --stat $old_version..$pgdesc
159 [ "$choice" = "ld" ] && psql -X -c '\l+'
160 [ "$choice" = "lp" ] && git log --patch $old_version..$pgdesc
161 [ "$choice" = "lt" ] && git log --graph --stat $old_version..$pgdesc
162 [ "$choice" = "lw" ] && git log --patch --word-diff $old_version..$pgdesc
163 [ "$choice" = "n" ] && exit 0
164 [ "$choice" = "p" ] && git pa
165 [ "$choice" = "t" ] && {
166 git log --abbrev-commit --date-order --decorate=short --graph \
167 --pretty=format:'%Cred%h %Cblue%p%Creset -%Creset %s %Cgreen(%cd %Cblue%an%Cgreen)%Creset%C(yellow)%d%Creset' \
168 $old_version..$pgdesc
170 echo
171 done
172 else
173 unset choice
174 until [ "$choice" = "y" ]; do
175 echo -n Press \'y\' to start the build, or \'n\' to abort...
176 read choice
177 [ "$choice" = "n" ] && exit 0
178 done
181 msg mkdir $destdir &&
182 sudo mkdir -p "$destdir" &&
184 msg chown $user.$user $destdir &&
185 sudo chown $user.$user "$destdir" &&
187 msg Run ./configure &&
188 ./configure --prefix="$destdir" --with-extra-version="+$pgdesc" \
189 --enable-debug --with-libxml --with-openssl --with-perl --with-uuid=ossp &&
191 msg make world &&
192 make world &&
194 msg make check &&
195 make check &&
197 msg make install-world &&
198 make install-world &&
200 until sudo -u postgres echo postgres pwd OK | grep -q "postgres pwd OK"; do
202 done &&
204 msg Dump all databases to $dumpfile &&
205 sudo -u postgres bash -c "pg_dumpall | gzip >\"$dumpfile\"" &&
207 msg pg_ctl stop &&
208 sudo -u postgres pg_ctl -D "$dbroot/current/main" stop &&
210 msg Update the $prgdir/postgres symlink &&
211 cd "$prgdir" &&
212 sudo ln -fnsv "../varprg/$pgdir" postgres &&
214 msg Create \'current\' symlink to $pgdesc &&
215 sudo -u postgres ln -fnsv "$pgdesc" "$dbroot/current" &&
217 msg initdb &&
218 sudo -u postgres initdb --lc-collate=C -E UTF-8 "$dbroot/$pgdesc/main" &&
220 msg pg_ctl start &&
221 sudo -u postgres pg_ctl -D "$dbroot/$pgdesc/main" \
222 -l "$dbroot/$pgdesc/logfile" start &&
224 sudo -u postgres bash -c "until pg_isready -q; do sleep 1; done" &&
226 msg build-postgis &&
227 build-postgis &&
229 msg Restore databases from $dumpfile &&
230 sudo -u postgres bash -c "zcat \"$dumpfile\" | psql postgres" &&
232 msg Create manpage symlinks &&
233 for f in 1 3 7; do
234 cd "$prefix/share/man/man$f" &&
235 sudo ln -nfsv ../../../prg/postgres/share/man/man$f/* . || true
236 done &&
238 echo &&
239 echo $progname: Old version: $old_version >&2 &&
240 echo $progname: New version: $(
241 $prgdir/postgres/bin/postgres --version | fmt -1 | tail -1
242 ) >&2 &&
244 if test -d "$prefix/.git/."; then
245 msg Commit the symlink &&
246 commitmsg=$(
247 echo $progname installed $pgdir on $(hostname)
248 echo
249 suuid -t commit,$progname
250 ) &&
251 cd "$prgdir" &&
252 sudo git add postgres &&
253 echo Commit message: &&
254 echo $commitmsg &&
255 sudo git commit -m "$commitmsg" &&
256 msg \"git pa\" in $(pwd) &&
257 (git pa || true)
258 fi &&
260 echo &&
261 echo $progname finished successfully >&2 &&
262 exit 0 || {
263 msg Something went wrong, aborted
264 sudo rmdir "$destdir"
265 exit 1