### A simple script that opens up a new iTerm terminal window and then activates four sessions
### for use with Rails development.
## 1: COMMANDS Session = for basic Rails commands (script/generate...)
## 2: SERVER Session = for WeBrick/Lighttpd script/server sessions
## 3: AUTOTEST Session = for Zentest autotest functionality (autotest -rails) [http://nubyonrails.com/articles/2006/04/19/autotest-rails]
## 4: CONSOLE Session = for working with the console
##
###	The syntax below is fairly simple so you can change this easily to fit your requirements.
##	Created 2006-08-25 by Mats Persson  (mats-at-imediatec.co.uk)
##	With help from Caius Durling (#textmate IRC) to import the TM variable into AppleScript

osascript <<-APPLESCRIPT

-- get the path to the rails projects root dir
set rails_root to (do shell script "echo $TM_PROJECT_DIRECTORY")

tell application "iTerm"
	activate
	-- make a new terminal 
	set myterm to (make new terminal)
	-- talk to the new terminal 
	tell myterm
		-- open up the Commands session
		-- launch a default shell in a new tab in the same terminal 
		launch session "Default Session"
		-- talk to the session 
		tell the last session
			-- set some attributes 
			set name to "Commands"
			-- execute a command 
			write text "cd " & rails_root
		end tell -- we are done talking to the session 
		-- we are back to talking to the terminal 
		
		-- now open up the SERVER session
		-- launch a default shell in a new tab in the same terminal 
		launch session "Default Session"
		-- talk to the session 
		tell the last session
			-- set some attributes 
			set name to "Server"
			-- execute a command 
			write text "cd " & rails_root
			-- uncomment the next line to automatically start the rails server
			-- write text "script/server webrick -p 300"
		end tell -- we are done talking to the session 
		
		-- now open up the AutoTest session
		-- launch a default shell in a new tab in the same terminal 
		launch session "Default Session"
		-- talk to the session 
		tell the last session
			-- set some attributes 
			set name to "AutoTest"
			-- execute a command 
			write text "cd " & rails_root
			-- uncomment the next line to automatically start the autotest rails service
			-- write text "autotest -rails"
		end tell -- we are done talking to the session 
		
		-- now open up the Console session
		-- launch a default shell in a new tab in the same terminal 
		launch session "Default Session"
		-- talk to the session 
		tell the last session
			-- set some attributes 
			set name to "Console"
			-- execute a command 
			write text "cd " & rails_root
		end tell -- we are done talking to the session 
		
	end tell
	
end tell
APPLESCRIPT
## end script