# vim: set ft=sh:

rails_script ()
{
  local wd=$(__basedir)

  local lwd=`pwd`

  target=$1; shift
  if [ -f $wd/bin/rails ]; then
    cmd="$wd/bin/rails $target $@"
  elif [ -f $wd/script/rails ]; then
    cmd="$wd/script/rails $target $@"
  elif [ -f $wd/script/$target ]; then
    cmd="$lwd/script/$target $@"
  elif [ -f $lwd/script/rails ]; then
    cmd="$lwd/script/rails $target $@"
  elif [ -f $lwd/script/$target ]; then
    cmd="$lwd/script/$target $@"
  fi

  echo "${tgrn}Running${rst} $cmd"
  $cmd
}

# rails
alias rails1='rails _1.2.6_'
alias cap1='cap _1.4.0_'

alias unexample="find ./config/ -name '*.example'|xargs basename -s .example | xargs -I % cp -i config/%{.example,}"
alias Rdm='rake db:migrate'
alias Rdmr='rake db:migrate:redo'

.r() { touch "$(__basedir)/tmp/restart.txt" ; }

Rc () { rails_script "console"  $@ ; }
Rs () { rails_script "server"   $@ ; }
Rg () { rails_script "generate" $@ ; }
Rr () { rails_script "runner"   $@ ; }

Rgm() { rails_script "generate" "migration" $@ ; }
Rgs() { rails_script "generate" "scaffold"  $@ ; }
Rgr() { rails_script "generate" "resource"  $@ ; }

Rsb() { DB_ENV=build Rs      ; }
Rss() { DB_ENV=staging Rs    ; }
Rsp() { DB_ENV=production Rs ; }

Rcb() { DB_ENV=build Rc      ; }
Rcs() { DB_ENV=staging Rc    ; }
Rcp() { DB_ENV=production Rc ; }

Rrb() { DB_ENV=build Rr      ; }
Rrs() { DB_ENV=staging Rr    ; }
Rrp() { DB_ENV=production Rr ; }


