#!/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
