3 # Build the DAViCal database
9 DBADIR
="`dirname \"$0\"`"
11 INSTALL_NOTE_FN
="`mktemp -t tmp.XXXXXXXXXX`"
14 [ -f "${1}/dba/awl-tables.sql" ]
18 # Attempt to locate the AWL directory
19 AWLDIR
="${DBADIR}/../../awl"
20 if ! testawldir
"${AWLDIR}"; then
21 AWLDIR
="/usr/share/awl"
22 if ! testawldir
"${AWLDIR}"; then
23 AWLDIR
="/usr/local/share/awl"
24 if ! testawldir
"${AWLDIR}"; then
25 echo "Unable to find AWL libraries"
31 export AWL_DBAUSER
=davical_dba
32 export AWL_APPUSER
=davical_app
34 # Get the major version for PostgreSQL
35 export DBVERSION
="`psql -qXAt -c "SELECT version
();" template1 | cut -f2 -d' ' | cut -f1-2 -d'.'`"
38 cat >>"${INSTALL_NOTE_FN}"
42 psql
-qXAt -c "SELECT usename FROM pg_user;" template1
46 if ! db_users |
grep "^${1}$" >/dev
/null
; then
47 psql
-qXAt -c "CREATE USER ${1} NOCREATEDB NOCREATEROLE;" template1
48 cat <<EONOTE | install_note
49 * You will need to edit the PostgreSQL pg_hba.conf to allow the
50 '${1}' database user access to the 'davical' database.
56 create_plpgsql_language
() {
57 if ! psql
${DBA} -qXAt -c "SELECT lanname FROM pg_language;" "${DBNAME}" |
grep "^plpgsql$" >/dev
/null
; then
58 createlang plpgsql
"${DBNAME}"
63 [ "XtestX`psql -U "${1}" -qXAt -c \"SELECT usename FROM pg_user;\" \"${DBNAME}\" 2>/dev/null`" != "XtestX" ]
66 # Hide all the annoying NOTICE... messages
67 export PGOPTIONS
='--client-min-messages=warning'
69 create_db_user
"${AWL_DBAUSER}"
70 create_db_user
"${AWL_APPUSER}"
72 # FIXME: Need to check that the database was actually created.
73 if ! createdb
--encoding UTF8
--template template0
--owner "${AWL_DBAUSER}" "${DBNAME}" ; then
74 echo "Unable to create database"
79 # Try a few alternatives for a database user or give up...
80 if try_db_user
"${AWL_DBAUSER}" ; then
81 export DBA
="-U ${AWL_DBAUSER}"
83 if try_db_user
"postgres" ; then
84 export DBA
="-U postgres"
86 if try_db_user
"${USER}" ; then
89 if try_db_user
"${PGUSER}" ; then
94 I cannot find a usable database user to construct the DAViCal database with, but
95 may have successfully created the davical_app and davical_dba users (I tried :-).
97 You should edit your pg_hba.conf file to give permissions to the davical_app and
98 davical_dba users to access the database and run this script again. If you still
99 continue to see this message then you will need to make sure you run the script
100 as a user with full permissions to access the local PostgreSQL database.
102 If your PostgreSQL database is non-standard then you will need to set the PGHOST,
103 PGPORT and/or PGCLUSTER environment variables before running this script again.
105 See: http://wiki.davical.org/w/Install_Errors/No_Database_Rights
114 create_plpgsql_language
117 # Load the AWL base tables and schema management tables
118 psql
-qXAt ${DBA} -f "${AWLDIR}/dba/awl-tables.sql" "${DBNAME}" 2>&1
119 psql -qXAt ${DBA} -f "${AWLDIR}/dba/schema-management.sql" "${DBNAME}" 2>&1
122 # Load the DAViCal tables
123 psql
-qXAt ${DBA} -f "${DBADIR}/davical.sql" "${DBNAME}" 2>&1
126 # Set permissions for the application DB user on the database
127 if ! ${DBADIR}/update-davical-database --dbname "${DBNAME}" --appuser "${AWL_APPUSER}" --nopatch --owner "${AWL_DBAUSER}" ; then
129 * * * * ERROR * * * *
130 The database administration utility failed. This may be due to database
131 permissions for the davical_dba user, or because the Perl DBD::Pg or YAML
132 libraries are not available.
134 Check that your pg_hba.conf allows the davical_dba user to connect to the
135 database (and make sure you've reloaded PostgreSQL since changing that).
137 Also see: http://wiki.davical.org/w/Install_Errors/No_Perl_YAML
143 # Load the required base data
144 psql
-qXAt ${DBA} -f "${DBADIR}/base-data.sql" "${DBNAME}" | egrep -v '^10'
147 # We can override the admin password generation for regression testing predictability
148 if [ "${ADMINPW}" = "" ] ; then
150 # Generate a random administrative password. If pwgen is available we'll use that,
151 # otherwise try and hack something up using a few standard utilities
152 ADMINPW="`pwgen -Bcny 2>/dev/null | tr \"\\\\\'\" '^='`"
155 if [ "$ADMINPW" = "" ] ; then
156 # OK. They didn't supply one, and pwgen didn't work, so we hack something
157 # together from /dev/random ...
159 ADMINPW="`dd if=/dev/urandom bs=512 count=1 2>/dev/null | tr -c -d 'a-km-zA-HJ-NP-Y0-9' | cut -c2-9`"
162 if [ "$ADMINPW" = "" ] ; then
163 # Right. We're getting desperate now. We'll have to use a default password
164 # and hope that they change it to something more sensible.
165 ADMINPW="please change this password
"
168 psql -qX ${DBA} -c "UPDATE usr SET password = '**${ADMINPW}' WHERE user_no = 1;" "${DBNAME}"
172 cat "${INSTALL_NOTE_FN}"
173 rm "${INSTALL_NOTE_FN}"
176 * The password for the 'admin' user has been set to '${ADMINPW}'
178 Thanks for trying DAViCal! Check in /usr/share/doc/davical/examples/ for
179 some configuration examples. For help, visit #davical on irc.oftc.net.