summaryrefslogtreecommitdiffstats
path: root/debian/wp-setup
blob: 6efd577ad14269841fde738b7882e070577d6984 (plain)
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#!/bin/sh

WP_CONTENT_DIR=/var/lib/wordpress/wp-content
WP_CONTENT_ORIG_DIR=/usr/share/wordpress/wp-content
WP_CONTENT_SUBDIRS="themes plugins languages"

set -e

usage() {
    cat <<END
$0 <action> [<options>...]

Actions:
    --sync-wp-content	Update $WP_CONTENT_DIR based on files
			available in $WP_CONTENT_ORIG_DIR
    --purge-wp-content  Purge $WP_CONTENT_DIR of symlinks
                        pointing to $WP_CONTENT_ORIG_DIR
END
}

purge_wp_content() {
    for subdir in $WP_CONTENT_SUBDIRS; do
	list=$(purge_wp_content_subdir "$subdir")
	print_if_needed "Removed from $WP_CONTENT_DIR/$subdir" "$list"
    done
}

print_if_needed() {
    local message="$1"
    local list="$2"
    if [ -n "$list" ]; then
	echo "$message: $list"
    fi
}

purge_wp_content_subdir() {
    local subdir=$1
    for dir in $WP_CONTENT_DIR/$subdir/*; do
	if is_symlink_to_standard_directory "$dir"; then
	    remove_symlink "$dir"
	fi
    done
}

is_symlink_to_standard_directory() {
    local symlink="$1"
    if [ ! -h "$symlink" ]; then
	return 1
    fi
    target=$(readlink "$symlink")
    if [ "$target" = "${target##$WP_CONTENT_ORIG_DIR}" ]; then
	return 1
    fi
    return 0
}

remove_symlink() {
    local symlink="$1"
    echo -n "$(basename "$symlink") "
    rm -f "$symlink"
}

sync_wp_content() {
    for subdir in $WP_CONTENT_SUBDIRS; do
	ensure_wp_content_subdir_exists "$subdir"
	list=$(add_new_wp_content_files "$subdir")
	print_if_needed "Added to $WP_CONTENT_DIR/$subdir" "$list"
	list=$(drop_removed_wp_content_files "$subdir")
	print_if_needed "Removed from $WP_CONTENT_DIR/$subdir" "$list"
    done
}

ensure_wp_content_subdir_exists() {
    local subdir="$1"
    [ -e $WP_CONTENT_DIR ] || setup_wp_content
    mkdir -p "$WP_CONTENT_DIR/$subdir"
}

setup_wp_content() {
    mkdir -p $WP_CONTENT_DIR/blogs.dir $WP_CONTENT_DIR/uploads
    chown www-data:www-data $WP_CONTENT_DIR/blogs.dir $WP_CONTENT_DIR/uploads
}

add_new_wp_content_files() {
    local subdir="$1"
    for dir in $WP_CONTENT_ORIG_DIR/$subdir/*; do
	if [ ! -e "$dir" ]; then
	    continue
	fi
	name="$(basename "$dir")"
	symlink_path="$WP_CONTENT_DIR/$subdir/$name"
	if [ -e "$symlink_path" ]; then
	    continue
	fi
	echo -n "$name "
	ln -sf "$dir" "$symlink_path"
    done
}

drop_removed_wp_content_files() {
    local subdir="$1"
    for dir in $WP_CONTENT_DIR/$subdir/*; do
	if ! is_symlink_to_standard_directory "$dir"; then
	    continue
	fi
	# Ensure the symlink is broken before removing it
	if [ -e "$dir" ]; then
	    continue
	fi
	remove_symlink "$dir"
    done
}

TEMP=$(getopt -o sp -l sync-wp-content,purge-wp-content -- "$@")
eval set -- "$TEMP"

while true; do
    case "$1" in
	--sync-wp-content)
	    action="$1"
	    shift
	;;
	--purge-wp-content)
	    action="$1"
	    shift
	;;
	--)
	    shift
	    break
	;;
	*)
	    echo "$0: error: invalid option '$1'" >&2
	    usage
	    exit 1
	;;
    esac
done

case "$action" in
    --sync-wp-content)
	sync_wp_content
    ;;
    --purge-wp-content)
	purge_wp_content
    ;;
    *)
	echo "$0: error: no action specified!" >&2
	usage
	exit 1
    ;;
esac

# Script documentation follows
: <<=cut
=encoding UTF-8

=head1 NAME

wp-setup - wordpress setup for Debian

=head1 SYNOPSIS

B<wp-setup> I<action>

=head1 DESCRIPTION

This script's purpose is to make it easier to manage Wordpress
installations on Debian systems. For now, it's mainly for the
package's internal usage though.

=head1 ACTIONS

=over 4

=item B<--sync-wp-content>

Update /var/lib/wordpress/wp-content/{plugins,themes}/ based on what's
available in /usr/share/wordpress/wp-content/{plugins,themes}. This
command is run by the package's postinst script every time that dpkg
installs a package that modifies something in
/usr/share/wordpress/wp-content/.

=item B<--purge-wp-content>

Remove all symlinks from /var/lib/wordpress/wp-content/{plugins,themes}/
that point to corresponding directories in
/usr/share/wordpress/wp-content/{plugins,themes}. This command is run by
the package's prerm script to ensure everything is properly removed.

=back

=head1 AUTHOR

Raphaël Hertzog <hertzog@debian.org>

=cut