#!/bin/sh
# svnSlim: checkout or update gallery2 svn skipping unit tests and language files
#          can also remove text-base copies and restore them for updates
# svnSlim co [-r {rev}] [-L {locale}] [dir] -- new checkout in [dir] (default=gallery2)
#                                              still has text-base; optionally include one locale
# svnSlim slim [dir]          -- remove all text-base files (can't do svn diff, etc now)
# svnSlim fat [dir]           -- restore all text-base files
# svnSlim up [-r {rev}] [dir] -- perform svn update (restores current slim/fat state when done)
# svnSlim export [-r {rev}] [-L {locale}] [dir] -- svn export into [dir] (default=gallery2)
#   [dir] parameter for up/slim/fat is path to working copy (default=.); subdir not supported
# Set BRANCH below to branches/BRANCH_2_1 (or any branch name) to use a release branch:
BRANCH=trunk
CMD=$1
if [ "$CMD" = co -o "$CMD" = checkout ]; then    CMD=co
elif [ "$CMD" = up -o "$CMD" = update ]; then    CMD=up
elif [ "$CMD" != slim -a "$CMD" != fat -a "$CMD" != export ]; then
  echo "Usage: $0 {checkout|co|update|up|export|slim|fat} [-r {rev}] [-L {locale}] [dirname]"
  exit 1
fi
shift
while [ "$1" = "-r" -o "$1" = "-L" ]; do
  if [ "$1" = "-r" ]; then
    REV="-r $2"; shift 2
  else
    LOCALE="$2"; shift 2
  fi
done
DIR=$1
if [ -z "$DIR" ]; then
  if [ $CMD = co -o $CMD = export ]; then DIR=gallery2; else DIR=.; fi
fi
if [ $CMD = export ]; then
  svn export $REV https://svn.sourceforge.net/svnroot/gallery/$BRANCH/gallery2 $DIR
  cd $DIR; echo "Remove unneeded files.."
  rm -rf install/po upgrade/po themes/*/po modules/*/po modules/*/test
  if [ -z "$LOCALE" ]; then
    rm -rf install/locale upgrade/locale themes/*/locale modules/*/locale
  else
    for X in install upgrade themes/[a-z]* modules/[a-z]*; do
      cd $X; Y=`ls -d locale/$LOCALE 2> /dev/null`
      if [ -z "$Y" ]; then rm -rf locale
      else mv locale/$LOCALE .locDir; rm -rf locale; mkdir locale; mv .locDir locale/$LOCALE; fi
      cd ..; if [ $X != install -a $X != upgrade ]; then cd ..; fi
    done
  fi
  echo DONE.
  exit
fi
if [ $CMD = co ]; then
  svn co -N $REV https://svn.sourceforge.net/svnroot/gallery/$BRANCH/gallery2 $DIR
fi
cd $DIR
if [ $CMD = up -o $CMD = fat ]; then
  if [ ! -f .svn/text-base/main.php.svn-base ]; then
    # restore text-base files
    if [ ! -f .svnSlim ]; then echo ".svmSlim not found"; exit 2; fi
    LASTREV=`cat .svnSlim`
    echo -n "Restore text-base files.."; I=
    for X in `find * -name .svn -prune -o -type f -print`; do
      grep -qs 'name="'`basename $X`'"' `dirname $X`/.svn/entries
      if [ $? -ne 0 ]; then continue; fi
      wget -q -O `dirname $X`/.svn/text-base/`basename $X`.svn-base \
 "http://svn.sourceforge.net/viewcvs.cgi/gallery/$BRANCH/gallery2/$X?rev=$LASTREV&pathrev=$LASTREV"
      if [ $? -ne 0 ]; then echo "Error restoring text-base for $X"; exit 3; fi
      I=".$I"; if [ $I = "..........................." ]; then I=; echo -n .; fi
    done
    echo .; if [ $CMD = up ]; then WASSLIM=1; fi
  fi
fi
if [ $CMD = co -o $CMD = up ]; then
  echo "Update from repository.."
  svn up $REV lib images
  svn up -N $REV . install modules themes upgrade
  cd modules; svn up -N $REV `svn ls $REV`; cd ..
  cd themes; svn up -N $REV `svn ls $REV`; cd ..
  for X in install upgrade modules/[a-z]* themes/[a-z]*; do
    cd $X
    svn up $REV `svn ls $REV | perl -ne 'print if /^(?!po|locale|test).*\/$/'`
    if [ -d locale ]; then svn up $REV locale; fi
    if [ -n "$LOCALE" ]; then
      svn up -N $REV locale; svn up $REV locale/$LOCALE
    fi
    cd ..; if [ $X != install -a $X != upgrade ]; then cd ..; fi
  done
fi
if [ $CMD = slim -o -n "$WASSLIM" ]; then
  # record rev# and remove text-base files
  rm -f .svnSlim
  svn info . | sed -ne 's/^Revision: //p' > .svnSlim
  echo "Remove text-base files.."
  for X in `find . -path "*/.svn/text-base" -type d`; do
    rm -f $X/* $X/.[a-zA-Z]*
  done
fi
echo DONE.
