#!/usr/bin/env bash
#===============================================================================
#
#          FILE:  create-custom-cow-site.sh
# 
#         USAGE:  ./create-custom-cow-site.sh 
# 
#   DESCRIPTION:  a script that takes a grails plugin with a CoW guest layout
#                 creates a directory tree with that layout installed
# 
#       CREATED:  19/02/09 13:23:22 GMT
# 
#        AUTHOR:  Hamish Cunningham
#===============================================================================

# avoid bash gotcha with cd'ing into ambiguous dirs...
CDPATH=

# parameters
P=`basename $0`				
USAGE="${P} [-h] -n site-name [-p abs-path-to-plugin.zip] [-s server-software-version]\n"\
"\n"\
"-h: print help message and exit\n"\
"-p: absolute path to this site's layout plugin - optional\n"\
"-s: server software version (as a date, e.g. 29-apr-2009) - optional\n"\
"-n: name of the new site (must be a valid directory name)\n"\
"\n"\
"Mode 1: if server software version not specified, create new server tree.\n"\
"Mode 2: create an update for this site.\n"\
"\n"\
"Note: you have to run this script in the gatewiki directory.\n"\
""
OPTIONSTRING=hp:s:n:
PLUGINZIP=
SERVERVERSION=
CREATESERVER=
SITENAME=
SITEDIR=
SERVERDIR=
PDATE=`date +%b-%d-%Y |tr '[A-Z]' '[a-z]'`

# message & exit
usage() { echo -e '\n'Usage: $USAGE; [ ! -z "$1" ] && exit $1; }

# process options
while getopts $OPTIONSTRING OPTION
do
  case $OPTION in
    h)	usage 0 ;;
    p)  PLUGINZIP="$OPTARG" ;;
    s)  SERVERVERSION="$OPTARG" ;;
    n)  SITENAME="$OPTARG" ;;
    *)	usage 1 ;;
  esac   
done 
shift `expr $OPTIND - 1`
[ ! -z $1 ] && usage 2
[ -z "${SITENAME}" ] && usage 5
[ ! -z "${PLUGINZIP}" ] && case "${PLUGINZIP}" in
  /*.zip) ;;
  *) echo the path \"${PLUGINZIP}\" should be absolute and a zip; usage 6; ;;
esac

# make sure we're not in a random location
case `pwd` in
  *gatewiki) ;;
  *) echo 'The script should be running in the gatewiki directory'; usage 3; ;;
esac

# does the plugin exist?
[ ! -z "${PLUGINZIP}" ] && if [ ! -e ${PLUGINZIP} ]
then
  echo oops - ${PLUGINZIP} doesn\'t seem to exist....
  usage 4
fi

# the site dir is derived from the site name plus the date
SITEDIR=${SITENAME}-${PDATE}

# mode 1 or mode 2?
SERVERDIR=gatewiki-${PDATE}
if [ x${SERVERVERSION} != x ]
then
  CREATESERVER=no
else
  CREATESERVER=yes
fi

# store the current SVN URL
SVNURL=`svn info . |grep ^URL`
set $SVNURL
SVNURL=$2
SVNTAG=`echo $SVNURL | sed -e 's,trunk$,tags,' -e 's,branches$,tags,'`
SVNTAG=${SVNTAG}/${SERVERDIR}

# report
echo -en '\n*** about to '
if [ $CREATESERVER == yes ]
then
  echo -n "create a new server tree for site $SITENAME "
  echo -n "in `pwd`/../${SERVERDIR} - ok? "
else
  echo -n "create an update for site $SITENAME "
  echo -n "in `pwd`/${SITEDIR} - ok? "
fi
read ANSWER
[ "${ANSWER}" == y -o "${ANSWER}" == yes ] || \
  { echo -e 'giving up in a huff\n'; exit 6; }
echo

# creating a new server software deploy, or just a site update?
if [ ${CREATESERVER} == yes ]
then 
  cd ..
  echo -en rsyncing gatewiki to ${SERVERDIR} in `pwd`...
  rsync \
    -aH --delete --delete-excluded \
    --exclude='.svn/' \
    --include=cow \
    --include=grails \
    --include=gwt \
    --include=nutch-solr \
    --include=sventon \
    --exclude=site-plugins \
    --exclude=dev-user-home \
    --exclude=user-home \
    --exclude='/*' \
    gatewiki/ ${SERVERDIR}
  echo -e done
  cd ${SERVERDIR}
  mv cow ${SITEDIR}
else
  echo rsyncing cow to ${SITEDIR}...
  rsync \
    -aH --delete --delete-excluded \
    --exclude='.svn/' \
    --exclude=dev-user-home \
    --exclude=user-home \
    cow/ ${SITEDIR}
  echo -e done
fi
cd ${SITEDIR}
#echo -en '\nthe new site directory contains: '
#ls -a
echo

# install the plugin
if [ ! -z "${PLUGINZIP}" ]
then
  echo installing plugin...
  ant -Dgate.cow.grails.command="install-plugin ${PLUGINZIP}" grails
fi

# done
echo
echo ...done.
echo
echo ==========================================================================
echo "when you're happy, tag the repository for the server software (and for"
echo "the plugin if that lives somewhere else):"
echo "  " svn cp ${SVNURL} ${SVNTAG}
echo
echo to deploy rsync to the deployment server and \"ant run-prod\" in $SITEDIR
echo =========================================================================
