1 #! /bin/bash
2 #
3 # PostRR test setup helper script
4 #
5 # Copyright (C) 2012 Sebastian 'tokkee' Harl <sh@tokkee.org>
6 # All rights reserved.
7 #
8 # Redistribution and use in source and binary forms, with or without
9 # modification, are permitted provided that the following conditions
10 # are met:
11 # 1. Redistributions of source code must retain the above copyright
12 # notice, this list of conditions and the following disclaimer.
13 # 2. Redistributions in binary form must reproduce the above copyright
14 # notice, this list of conditions and the following disclaimer in the
15 # documentation and/or other materials provided with the distribution.
16 #
17 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 # ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19 # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
21 # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22 # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23 # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
24 # OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25 # WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26 # OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
27 # ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 PG_CONFIG=`which pg_config`
30 if test -z "$PG_CONFIG"; then
31 echo "pg_config not found!" >&2
32 exit 1
33 fi
35 BIN_DIR=`pg_config --bindir`
36 if test -z "$TARGET"; then
37 TARGET=`pwd`/target
38 fi
40 PWD_esc="$( echo "$PWD" | sed -e 's/\//\\\//g' )"
41 TARGET_esc="$( echo "$TARGET" | sed -e 's/\//\\\//g' )"
43 set -e
45 case "$1" in
46 setup)
47 if test $# -ne 1; then
48 echo "Too many arguments!" >&2
49 echo "Usage: $0 setup" >&2
50 exit 1
51 fi
52 mkdir -p $TARGET/var/lib/postgresql/main
53 mkdir -p $TARGET/var/run/postgresql
54 mkdir -p $TARGET/var/log/postgresql/main
55 $BIN_DIR/initdb -D $TARGET/var/lib/postgresql/main
56 sed -r -i -e 's/^#port = 5432/port = 5435/' \
57 $TARGET/var/lib/postgresql/main/postgresql.conf
58 sed -r -i -e "s/^#dynamic_library_path = '\\\$libdir'/dynamic_library_path = '\$libdir:$PWD_esc\/src'/" $TARGET/var/lib/postgresql/main/postgresql.conf
59 sed -r -i -e "s/^#unix_socket_directory = ''/unix_socket_directory = '$TARGET_esc\/var\/run\/postgresql'/" $TARGET/var/lib/postgresql/main/postgresql.conf
60 $0 start -B
61 $BIN_DIR/createdb -e -h $TARGET/var/run/postgresql/ -p 5435 "$( id -un )"
62 $0 stop
63 ;;
64 client)
65 libedit=$( ldd $BIN_DIR/psql 2> /dev/null \
66 | grep -Eo '=> [^ ]+\/libedit\.so\..? ' | cut -d' ' -f2 )
67 if test -n "$libedit"; then
68 libdir=${libedit/libedit.*/}
69 libreadline=$( ls $libdir/libreadline.so* 2> /dev/null | head -n1 )
70 if test -n "$libreadline"; then
71 export LD_PRELOAD="$LD_PRELOAD:$libreadline"
72 fi
73 fi
74 shift
75 $BIN_DIR/psql -h $TARGET/var/run/postgresql/ -p 5435 "$@"
76 ;;
77 stop)
78 if test $# -ne 1; then
79 echo "Too many arguments!" >&2
80 echo "Usage: $0 setup" >&2
81 exit 1
82 fi
83 $BIN_DIR/pg_ctl -D $TARGET/var/lib/postgresql/main stop
84 ;;
85 start)
86 shift
87 if test "$1" = "-B"; then
88 shift
89 $BIN_DIR/pg_ctl -D $TARGET/var/lib/postgresql/main -l logfile -w start "$@"
90 else
91 $BIN_DIR/postgres -D $TARGET/var/lib/postgresql/main "$@"
92 fi
93 ;;
94 *)
95 echo "Usage: $0 setup|client|stop|start" >&2
96 exit 1
97 ;;
98 esac
100 # vim: set tw=0 :