#!/bin/bash

SVNBASE=http://devel.egressive.com/egressive/egdrupalsites

comm=`basename $0`

usage() {
  cat <<EOT
usage: $comm [OPTIONS] -U svnuri | siteuri core

Create an SVN skeleton tree for a Drupal site.

OPTIONS

  -h - Show this help
  -u - SVN tree will belong to user (Default: $user)
  -b - Specify alternat SVN base URI (Default: $SVNBASE)
  -U - Manually specify SVN URI

  Create a Drupal subversion skel at svnuri.

  # Create an automatic SVN URL
  $comm -u egressive ned.egressive.com 6.x
  #   Creates $svnbase/egressive/ned.egressive.com/6.x/trunk/...

  # Specify a non-standard URI
  $comm -U svn://svn.server.com/some/path

EOT
}

while getopts "hu:U:" OPT ; do
  case $OPT in
    h) usage; exit 0 ;;
    u) SITEUSER=$OPTARG ;;
    b) SVNBASE=$OPTARG ;;
    U) svnuri=$OPTARG ;;
  esac
done

shift $(($OPTIND - 1))

user=${SITEUSER-$USER}

if [ -n "$svnuri" ] && [ $# -gt 0 ] ; then
  echo "-U incompatible with extra arguments"
  usage
  exit 1
fi

if [ $# -gt 0 ] ; then
  siteuri=$1
  core=$2
  if [ -z "$siteuri" ] || [ -z "$core" ] ; then
    usage
    exit 1
  fi
  svnuri="$SVNBASE/$user/$siteuri/$core/trunk"
fi

if [ -z "$svnuri" ] ; then
  usage
  exit 1
fi

# Exit on any error
set -e

# Make a temp directory
tmpdir=`mktemp -d /tmp/${comm}-XXXXXXXX`

# cd into it, retaining previous cwd for later
pushd $tmpdir

# Create the top level directory in subversion
svn mkdir --parents -m "$comm: Creating project base directory" $svnuri

# Checkout the newly created dir
svn co $svnuri .

# Make standard directories
mkdir -v modules themes libraries
svn add modules themes libraries

# Set ignore properties for standard files and dirs
cat <<EOT > ignoreprops
files
drushrc.php
settings.php
EOT
svn --file=ignoreprops propset svn:ignore .
svn commit -m "$comm: Adding standard Drupal layout directories"

# Cleanup
popd
rm -rvf $tmpdir

echo "SVN skel created at $svnuri"