#!/usr/local/bin/bash

#  Rails and subversion all-in- one
#  Creates all files, reposiotries and makes additional changes to Rakefile
#  (c) 2006 Witold Rugowski http://nhw.pl

SVN="/usr/local/bin/svn"
SVNA="/usr/local/bin/svnadmin"
RAILS="/usr/local/bin/rails"
FETCH="/usr/bin/fetch -o rake.patch"

ERRORS=0

################################################################


function wasWrong () {
# takes 3 args 
# first is result of action
# second - should it be critical?
# third - action description

 if [ $1 -ne 0 ]; then
    ERRORS=1
    if [ $2 -eq 1 ]; then
    	echo "******** Error with $3. Exiting."
    	exit 1
    else
    	echo "******** Some errors with $3. I try to go further anyway"
    fi
 fi
}


############################################################

echo -n "Rails project name: "
read PROJECT

echo -n "Subversion repository root: "
read PATH

DIR=`pwd`

echo -n "
Now I will create ${PATH}/${PROJECT} subversion repository,
next I checkout it to ${PWD}, and generate rails app files.

Next I setup svn according to suggestions in 
http://wiki.rubyonrails.org/rails/pages/HowtoUseRailsWithSubversion

Is that OK? [y/n] "
read ans

if [ ! $ans == "y" ]; then
 echo "Looks like You are unsure. Leaving it now without any work done"
 exit
fi


if [ -d ${PATH}/${PROJECT} ]; then
  echo "It looks like rpository dir exists. Maybe it was already created."
  echo -n " Should I proceed and use it anyway? [y/n] "
  read ans

  if [ ! $ans == "y" ]; then
   echo "Looks like You are unsure. Check directories/names and try again"
   exit
  fi
fi
  
$SVNA create ${PATH}/${PROJECT} 
wasWrong $? 1 "svnadmin create"


$SVN co "file://${PATH}/${PROJECT}"
wasWrong $? 1 "svn checkout"

$RAILS $PROJECT
wasWrong $? 1 "Rails app creation"

cd $PROJECT
wasWrong $? 1 "Rails app creation"


$SVN add *
wasWrong $? 1 "svn add"

$SVN commit -m 'adding all'
wasWrong $? 1 "svn add"

$SVN remove log/*
wasWrong $? 0 "svn around line $LINENO"

$SVN commit -m 'removing logs'
wasWrong $? 0 "svn around line $LINENO"

$SVN propset svn:ignore "*.log" log/
wasWrong $? 0 "svn around line $LINENO"
$SVN update log/
wasWrong $? 0 "svn around line $LINENO"
$SVN commit -m 'Ignoring all files in /log/ ending in .log'
wasWrong $? 0 "svn around line $LINENO"


$SVN move config/database.yml config/database.example
wasWrong $? 0 "svn around line $LINENO"
$SVN commit -m 'Moving database.yml to database.example to provide a template for anyone who checks out the code'
wasWrong $? 0 "svn around line $LINENO"

$SVN propset svn:ignore "database.yml" config/
wasWrong $? 0 "svn around line $LINENO"
$SVN update config/	
wasWrong $? 0 "svn around line $LINENO"
$SVN commit -m 'Ignoring database.yml'
wasWrong $? 0 "svn around line $LINENO"

$SVN remove tmp/*
wasWrong $? 0 "svn around line $LINENO"
$SVN propset svn:ignore "*" tmp/
wasWrong $? 0 "svn around line $LINENO"
$SVN update tmp/
wasWrong $? 0 "svn around line $LINENO"
$SVN commit -m "ignore tmp/ content from now" 
wasWrong $? 0 "svn around line $LINENO"

/bin/cp config/database.example config/database.yml 
wasWrong $? 0 "copying database.yml"

echo -n "Should I fetch rake svn task (makes all needed changes to svn repo
when run). Check more details on:
http://nhw.pl/wp/2006/08/05/yet-another-rake-and-subversion-task/

Fetch it? [y/n] "
read ans

if [  $ans == "y" ]; then
 $FETCH http://nhw.pl/download/svn.rake
 wasWrong $? 0 "fetching Rakefile update"
 /bin/cat rake.patch >> Rakefile
 /bin/rm rake.patch
 
 $SVN commit -m 'Rakefile update'
 wasWrong $? 0 "svn around line $LINENO"
 
fi

if [ $ERRORS -eq 1 ]; then
  echo "All done, however were some errors durnig process. Examine output to
find out what problem was."
else
  echo "All went OK. Now You can start coding"
fi

