-
Notifications
You must be signed in to change notification settings - Fork 0
/
init-script.sh
125 lines (107 loc) · 3.56 KB
/
init-script.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#!/usr/bin/env bash
set -uo pipefail
CONF_DIR="/dokuwiki/conf"
LOCAL_FILE="$CONF_DIR/local.php"
ACL_FILE="$CONF_DIR/acl.auth.php"
USERS_FILE="$CONF_DIR/users.auth.php"
INSTALL_FILE="/var/www/html/install.php"
FORCE_CONF="${DOKUWIKI_FORCE_CONF:-0}"
# Check if any DOKUWIKI_ variable exists
has_dokuwiki_vars() {
printenv | grep -q "^DOKUWIKI_" && return 0
return 1
}
create_local_file() {
local file="$1"
{
echo "<?php"
echo "// DokuWiki configuration file"
echo "// Generated automatically"
echo
} > "$file"
}
update_config() {
local key="$1"
local value="$2"
[ ! -f "$LOCAL_FILE" ] && create_local_file "$LOCAL_FILE"
if grep -q "^\$conf\['${key}'\]" "$LOCAL_FILE"; then
sed -i "s|\$conf\['${key}'\] *= *.*|\$conf['${key}'] = ${value};|" "$LOCAL_FILE"
else
echo "\$conf['${key}'] = ${value};" >> "$LOCAL_FILE"
fi
}
setup_acl_policy() {
local policy="${DOKUWIKI_ACL_POLICY:-open}"
case "$policy" in
open) echo "* @ALL 16" > "$ACL_FILE" ;;
public) {
echo "* @ALL 1"
echo "* @user 8"
} > "$ACL_FILE" ;;
closed) echo "* @user 15" > "$ACL_FILE" ;;
*) echo "* @ALL 16" > "$ACL_FILE" ;;
esac
}
process_basic_configs() {
local configs=(
"DOKUWIKI_TITLE|title"
"DOKUWIKI_LANG|lang"
"DOKUWIKI_LICENSE|license"
"DOKUWIKI_TAGLINE|tagline"
"DOKUWIKI_BASEURL|baseurl"
"DOKUWIKI_USEACL|useacl"
"DOKUWIKI_DISABLEACTIONS|disableactions"
"DOKUWIKI_AUTHTYPE|authtype"
"DOKUWIKI_DEFAULTGROUP|defaultgroup"
"DOKUWIKI_DATEFORMAT|dformat"
"DOKUWIKI_USEREWRITE|userewrite"
)
for config in "${configs[@]}"; do
local env_var=${config%|*}
local key=${config#*|}
[ -n "${!env_var:-}" ] && {
update_config "$key" "'${!env_var}'"
}
done
}
setup_superadmin() {
[ -n "${DOKUWIKI_SUPERUSER:-}" ] || return 1
[ -n "${DOKUWIKI_SUPERPASS:-}" ] || return 1
[ -n "${DOKUWIKI_FULLNAME:-}" ] || return 1
[ -n "${DOKUWIKI_EMAIL:-}" ] || return 1
grep -q "^\$conf\['superuser'\]" "$LOCAL_FILE" || update_config "superuser" "'@admin'"
local hashed_password=$(php -r "echo password_hash('${DOKUWIKI_SUPERPASS}', PASSWORD_BCRYPT);")
local user_line="${DOKUWIKI_SUPERUSER}:${hashed_password}:${DOKUWIKI_FULLNAME}:${DOKUWIKI_EMAIL}:admin,user"
echo "$user_line" > "$USERS_FILE"
return 0
}
initialize_dokuwiki() {
if [ -z "$(ls -A /dokuwiki)" ]; then
if [ ! -L "/var/www/html/conf" ]; then
mv /var/www/html/conf /dokuwiki/
ln -sf /dokuwiki/conf /var/www/html/conf
fi
if [ ! -L "/var/www/html/data" ]; then
mv /var/www/html/data /dokuwiki/
ln -sf /dokuwiki/data /var/www/html/data
fi
if [ ! -L "/var/www/html/lib/plugins" ]; then
mv /var/www/html/lib/plugins /dokuwiki/plugins
ln -sf /dokuwiki/plugins /var/www/html/lib/plugins
fi
if [ ! -L "/var/www/html/lib/tpl" ]; then
mv /var/www/html/lib/tpl /dokuwiki/tpl
ln -sf /dokuwiki/tpl /var/www/html/lib/tpl
fi
chown -R www-data:www-data /dokuwiki
fi
}
# Main configuration process
if has_dokuwiki_vars; then
process_basic_configs
setup_acl_policy
setup_superadmin
[ -f "$INSTALL_FILE" ] && rm "$INSTALL_FILE"
fi
initialize_dokuwiki
exec apache2-foreground