#!/bin/sh

cleanup_adttmp=0
if [ -z "$ADTTMP" ]; then
  ADTTMP=$(mktemp -d)
  cleanup_adttmp=1
fi

cleanup() {
  set +e
  spring stop
  if [ $cleanup_adttmp -gt 0 ]; then
    rm -rf "$ADTTMP"
  fi
}
trap cleanup INT TERM EXIT

cd $ADTTMP

exec 2>&1
set -ex

rails new myapp
cd myapp

echo 'gem "public_suffix"' >> Gemfile

rails generate cucumber:install

tee features/test.feature <<FEATURE
Feature: test
  Scenario: test
    When I go to /
    Then I should see "Hello, world"
FEATURE

tee features/step_definitions/test_steps.rb <<STEPS
When(/^I go to \/$/) do
  visit '/'
end

Then(/^I should see "([^"]*)"$/) do |arg1|
  expect(page.body).to match(arg1)
end
STEPS

rails generate controller home

tee config/routes.rb <<ROUTES
Rails.application.routes.draw do
  root 'home#index'
end
ROUTES

tee app/views/home/index.html.erb <<HOMEPAGE
Hello, world
HOMEPAGE

rake
