#!/bin/sh

# This script should be run to build a development environment
# ============================================================

# Start by making sure we're running from the directory containing this script
# (probably OCD, but hey...)
cd `dirname $0`

# Check for local changes
# We're not doing anything destructive in this script, when we start we may need the following
#CHANGES=`svn status --quiet ../ | wc -l`
#if [ $CHANGES -ne 0 ]
#then
#echo "You have changes in this local SVN working copy. Please commit, or revert, then try running this script again."	
#exit
#fi

# Update everything we're going to need (it's all in the working copies dir)
# N.B. We'll need to change the tags as and when new releases are issued
svn up ../externals

# Clean out the export directory
rm -rf ../exports/*

# Export the various working copies
# WordPress Mu
svn export ../externals/wordpress-mu ../exports/wordpress-mu
# bbPress
svn export ../externals/bbpress ../exports/bbpress
# (need to export backpress separately into bbpress)
svn export ../externals/bbpress/bb-includes/backpress ../exports/bbpress/bb-includes/backpress
# BuddyPress plugins
svn export ../externals/buddypress-core ../exports/buddypress-core
svn export ../externals/buddypress-activity-streams ../exports/buddypress-activity-streams
svn export ../externals/buddypress-blogs ../exports/buddypress-blogs
svn export ../externals/buddypress-extended-profiles ../exports/buddypress-extended-profiles
svn export ../externals/buddypress-forums ../exports/buddypress-forums
svn export ../externals/buddypress-friends ../exports/buddypress-friends
svn export ../externals/buddypress-groups ../exports/buddypress-groups
svn export ../externals/buddypress-messages ../exports/buddypress-messages
svn export ../externals/buddypress-wire ../exports/buddypress-wire
# BuddyPress themes
svn export ../externals/buddypress-home-theme ../exports/buddypress-home-theme
svn export ../externals/buddypress-member-theme ../exports/buddypress-member-theme
# bbPress integration plugins
svn export ../externals/wpmu-enable-bbpress-caps ../exports/wpmu-enable-bbpress-caps
svn export ../externals/bbpress-integration ../exports/bbpress-integration

# It's at this point that we could apply patches to the exports, if we need to and
# applying the patches as part of this script would make them controllable. Although
# of course hacking or even patching the core is evil and should be avoided lest
# we end up with something really hard to upgrade.

# rsync everything into the docroot
# WP Mu files
rsync -aW --delete --exclude-from=build-rsync.txt ../exports/wordpress-mu/* ../site/
# BuddyPress plugins
rsync -aW --delete --exclude-from=build-rsync.txt ../exports/buddypress-core/* ../site/wp-content/mu-plugins/
rsync -aW --delete --exclude-from=build-rsync.txt ../exports/buddypress-activity-streams/* ../site/wp-content/mu-plugins/
rsync -aW --delete --exclude-from=build-rsync.txt ../exports/buddypress-blogs/* ../site/wp-content/mu-plugins/
rsync -aW --delete --exclude-from=build-rsync.txt ../exports/buddypress-extended-profiles/* ../site/wp-content/mu-plugins/
rsync -aW --delete --exclude-from=build-rsync.txt ../exports/buddypress-forums/* ../site/wp-content/mu-plugins/
rsync -aW --delete --exclude-from=build-rsync.txt ../exports/buddypress-friends/* ../site/wp-content/mu-plugins/
rsync -aW --delete --exclude-from=build-rsync.txt ../exports/buddypress-groups/* ../site/wp-content/mu-plugins/
rsync -aW --delete --exclude-from=build-rsync.txt ../exports/buddypress-messages/* ../site/wp-content/mu-plugins/
rsync -aW --delete --exclude-from=build-rsync.txt ../exports/buddypress-wire/* ../site/wp-content/mu-plugins/
# BuddyPress themes
rsync -aW --delete --exclude-from=build-rsync.txt ../exports/buddypress-home-theme/* ../site/wp-content/themes/
rsync -aW --delete --exclude-from=build-rsync.txt ../exports/buddypress-member-theme/* ../site/wp-content/member-themes/
# bbPress
rsync -aW --delete --exclude-from=build-rsync.txt ../exports/bbpress/* ../site/forums/
# bbPress integration plugins
rsync -aW --delete --exclude-from=build-rsync.txt ../exports/bbpress-integration/* ../site/wp-content/mu-plugins/
rsync -aW --delete --exclude-from=build-rsync.txt ../exports/wpmu-enable-bbpress-caps/* ../site/wp-content/mu-plugins/


# Make blogs.dir writeable
chmod 777 ../site/wp-content/blogs.dir

# ...and finally

# Clean out the export directory (again)
rm -rf ../exports/*

echo "All done. TTFN"
