1#!/usr/bin/env bash
2
3# Provision a blank DokuWiki install and configure it for general use.
4
5# The root location may exist or not, depending on your needs, and you
6# should aim not to change this file, instead providing a specific
7# configuration script that sources this file.
8
9# To use this in Vagrant, call your provisioning script, and set the
10# shell environment variables, DW_DOWNLOAD, DW_PACKAGE, DW_PATH,
11# DW_TITLE, DW_ADMIN_EMAIL, and DW_ADMIN_PASS.
12
13# Configuration ========================================================
14
15# Script constants -----------------------------------------------------
16
17PROVISION_TIME=$(date '+%Y-%m-%dT%H:%M:%S%:z')
18
19SUCCESS=0
20FAILURE=1
21
22# Script variables -----------------------------------------------------
23
24dw_download="${DW_DOWNLOAD}"
25dw_package="${DW_PACKAGE}"
26dw_path="${DW_PATH}"
27dw_title="${DW_TITLE}"
28dw_admin_email="${DW_ADMIN_EMAIL}"
29dw_admin_pass="${DW_ADMIN_PASS}"
30
31# Installation steps ===================================================
32
33function prepare_dokuwiki_path {
34	if [[ -d "${dw_path}" ]] ; then
35		echo "DokuWiki path ${dw_path} already exists"
36		return ${SUCCESS}
37	fi
38
39	echo "Making DokuWiki path ${dw_path}"
40	mkdir -p "${dw_path}"
41	status=$?
42	if [[ ${status} -gt ${SUCCESS} ]] ; then
43		echo "Error ${status} from mkdir while making DokuWiki path ${dw_path}"
44		return ${FAILURE}
45	fi
46
47	if [[ ! -d "${dw_path}" ]] ; then
48		echo "No error from mkdir, but DokuWiki path ${dw_path} doesn't exist"
49		return ${FAILURE}
50	fi
51}
52
53function download_dokuwiki {
54	if [[ -e "${dw_package}" ]] ; then
55		echo "DokuWiki package ${dw_package} is already downloaded"
56		echo "If you want to replace it, delete this file"
57		return ${SUCCESS}
58	fi
59
60	echo "Downloading DokuWiki from ${dw_download}"
61	curl --output "${dw_package}" --silent --show-error "${dw_download}"
62	status=$?
63	if [[ ${status} -gt ${SUCCESS} ]] ; then
64		echo "Error ${status} from curl while dowloading DokuWiki from ${dw_download}"
65		return ${FAILURE}
66	fi
67
68	if [[ -e "${dw_package}" ]] ; then
69		echo "Downloaded DokuWiki package ${dw_package}"
70		return ${SUCCESS}
71	else
72		echo "No error from curl, but ${dw_package} doesn't exist"
73		return ${FAILURE}
74	fi
75}
76
77function unpack_dokuwiki {
78	if [[ ! -e "${dw_package}" ]] ; then
79		echo "No package ${dw_package} to unpack"
80		return ${FAILURE}
81	fi
82
83	# Check for a container directory in the archive in the format dokuwiki-nnnn-nn-nnx/
84	contained_files=$(tar -tf "${dw_package}" | grep '^dokuwiki-[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9][a-z]\?/' | wc -l)
85#	echo "There are ${contained_files} contained files in the package"
86	if [[ ${contained_files} -gt 0 ]] ; then
87		# DokuWiki's files are in a container
88		strip_components=1
89		echo "Stripping container directory from the compressed files"
90	else
91		strip_components=0
92	fi
93
94	echo "Unpacking DokuWiki into ${dw_path}"
95	tar --strip ${strip_components} --directory="${dw_path}" -zxf "${dw_package}"
96	status=$?
97	if [[ ${status} -gt ${SUCCESS} ]] ; then
98		echo "Error ${status} from tar while unpacking DokuWiki from ${dw_package}"
99		return ${FAILURE}
100	fi
101
102	if [[ ! -e "${dw_path}/doku.php" ]] ; then
103		echo "No error from tar, but DokuWiki isn't unpacked in ${dw_path}"
104		return ${FAILURE}
105	fi
106
107	echo "DokuWiki unpacked in ${dw_path}"
108	return ${SUCCESS}
109}
110
111function configure_dokuwiki {
112	if [[ -e "${dw_path}/conf/local.php" ]] ; then
113		echo "DokuWiki is already configured in ${dw_path}/conf/local.php"
114		echo "If you want to replace the configuration, delete this file"
115		return ${SUCCESS}
116	fi
117
118	if [[ ! -d "${dw_path}/conf" ]] ; then
119		echo "Install didn't prepare a conf directory for DokuWiki"
120		return ${FAILURE}
121	fi
122
123# Local configuration --------------------------------------------------
124
125	config_file="${dw_path}/conf/local.php"
126	cat > "${config_file}" <<EOF
127<?php
128/**
129 * Dokuwiki's Main Configuration File - Local Settings
130 * Auto-generated by Vagrant provisioning
131 * Date: ${PROVISION_TIME}
132 */
133\$conf['title'] = '${dw_title}';
134EOF
135	cat >> "${config_file}" <<'EOF'
136$conf['lang'] = 'en';
137$conf['license'] = '0';
138$conf['useacl'] = 1;
139$conf['superuser'] = '@admin';
140$conf['disableactions'] = 'register';
141$conf['allowdebug'] = 1;
142EOF
143	if [[ ! -e "${config_file}" ]] ; then
144		echo "No error from cat command, but DokuWiki isn't configured in ${config_file}"
145		return ${FAILURE}
146	fi
147
148# ACL configuration ----------------------------------------------------
149
150	config_file="${dw_path}/conf/acl.auth.php"
151	cat > "${config_file}" <<EOF
152# acl.auth.php
153# <?php exit()?>
154# Don't modify the lines above
155#
156# Access Control Lists
157#
158# Auto-generated by Vagrant provisioning
159# Date: ${PROVISION_TIME}
160*               @ALL          1
161*               @user         8
162EOF
163	if [[ ! -e "${config_file}" ]] ; then
164		echo "No error from cat command, but DokuWiki isn't configured in ${config_file}"
165		return ${FAILURE}
166	fi
167
168# User configuration ---------------------------------------------------
169	DW_ADMIN_HASH=$(cd "${dw_path}" ; php /vagrant/dokuwiki_password.php "${DW_ADMIN_PASS}")
170
171	config_file="${dw_path}/conf/users.auth.php"
172	cat > "${config_file}" <<EOF
173# users.auth.php
174# <?php exit()?>
175# Don't modify the lines above
176#
177# Userfile
178#
179# Format:
180#
181# login:passwordhash:Real Name:email:groups,comma,seperated
182#
183# Auto-generated by Vagrant provisioning
184# Date: ${PROVISION_TIME}
185EOF
186	cat >> "${config_file}" <<EOF
187admin:${DW_ADMIN_HASH}:DokuWiki Administrator:${dw_admin_email}:admin,user
188EOF
189	if [[ ! -e "${config_file}" ]] ; then
190		echo "No error from cat command, but DokuWiki isn't configured in ${config_file}"
191		return ${FAILURE}
192	fi
193
194# Plugin configuration -------------------------------------------------
195
196	config_file="${dw_path}/conf/plugins.local.php"
197	cat > "${config_file}" <<EOF
198<?php
199/*
200 * Local plugin enable/disable settings
201 *
202 * Auto-generated by Vagrant provisioning
203 * Date: ${PROVISION_TIME}
204 */
205EOF
206	cat >> "${config_file}" <<'EOF'
207$plugins['authad']    = 0;
208$plugins['authldap']  = 0;
209$plugins['authmysql'] = 0;
210$plugins['authpgsql'] = 0;
211EOF
212	if [[ ! -e "${config_file}" ]] ; then
213		echo "No error from cat command, but DokuWiki isn't configured in ${config_file}"
214		return ${FAILURE}
215	fi
216
217	echo "DokuWiki configured in ${dw_path}/conf"
218	return ${SUCCESS}
219}
220
221# Full installation function ===========================================
222
223function install_dokuwiki {
224	if [[ -e "${dw_path}/doku.php" ]] ; then
225		echo "DokuWiki is already installed"
226		return ${SUCCESS}
227	fi
228
229	prepare_dokuwiki_path
230	status=$?
231	if [[ ${status} -gt ${SUCCESS} ]] ; then
232		return ${status}
233	fi
234
235	download_dokuwiki
236	status=$?
237	if [[ ${status} -gt ${SUCCESS} ]] ; then
238		return ${status}
239	fi
240
241	unpack_dokuwiki
242	status=$?
243	if [[ ${status} -gt ${SUCCESS} ]] ; then
244		return ${status}
245	fi
246
247	configure_dokuwiki
248	status=$?
249	if [[ ${status} -gt ${SUCCESS} ]] ; then
250		return ${status}
251	fi
252
253	return ${SUCCESS}
254}
255
256# Script ===============================================================
257
258install_dokuwiki
259status=$?
260if [[ ${status} -gt ${SUCCESS} ]] ; then
261	exit ${status}
262fi
263