#!/bin/sh

PROJECT="$1"
if [ -z "$1" ]; then
  echo -n "enter project name: "
  read
  PROJECT="$REPLY"
fi

echo "do you want to checkout:"
echo
echo "    T - trunk(T)"
echo "    b - branches(b)"
echo "    g - tags(g)"
echo "    a - all(a)"
echo
echo -n "    your choice (trunk is default): "
read

case $REPLY in
  "t"|"T"|"")
  exec svn co svn+ssh://svn.gnome.org/svn/$PROJECT/trunk $PROJECT
  ;;
  "b"|"B")
  echo
  echo "found the following branches:"
  BRANCHES=($(svn ls svn+ssh://svn.gnome.org/svn/$PROJECT/branches))
  echo ${BRANCHES[*]} | tr  " " "\n" | nl -
  COUNT=$(echo ${BRANCHES[*]} | tr  " " "\n" | wc -l)
  echo -n "    your choice: "
  read
  if [ ! -z "$(echo $REPLY | grep "[^0-9]")" ] || [ $REPLY -gt $COUNT ]; then
    echo "there is no branch with key $REPLY"
    exit
  fi
  BRANCH=${BRANCHES[$(($REPLY - 1))]}
  exec svn co svn+ssh://svn.gnome.org/svn/$PROJECT/branches/$BRANCH $BRANCH
  ;;
  "g"|"G")
  echo
  echo "found the following tags:"
  TAGS=($(svn ls svn+ssh://svn.gnome.org/svn/$PROJECT/tags))
  echo ${TAGS[*]} | tr  " " "\n" | nl -
  COUNT=$(echo ${TAGS[*]} | tr  " " "\n" | wc -l)
  echo -n "    your choice: "
  read
  if [ ! -z "$(echo $REPLY | grep "[^0-9]")" ] || [ $REPLY -gt $COUNT ]; then
    echo "there is no tag with key $REPLY"
    exit
  fi
  TAG=${TAGS[$(($REPLY - 1))]}
  exec svn co svn+ssh://svn.gnome.org/svn/$PROJECT/tags/$TAG $PROJECT-$TAG
  ;;
  "a"|"A")
  exec svn co svn+ssh://svn.gnome.org/svn/$PROJECT $PROJECT
  ;;
  *)
  echo "no valid option"
  ;;
esac

