
subdomain="yourDomaiNname.com"  
## <--CHANGE THIS
user="yourUserID"	
## <-- CHANGE THIs
# should be something like web2py.mydomain.com 
#where you have set up the web2py subdomain from 
#the dreamhost control pannel allowing rails 
# (note: have to add public to the directory to
# make this happen).
#
# note that it takes a few minutes for 
# dreamhost to create the domain..
#

import os

# if the file 
os.system("cd ~/")
os.system("mkdir bin")  
#if bin already exists, it won't over write
found=0
bash=[]
try:
	bash=open(".bash").read().split("\n")
	for line in bash:
		if line=="PATH=$HOME/bin:$PATH":
			found=1
except:
	print "No .bash file found, creating one..."
	found=0
if found==0: #add it
	print "Modifying .bash profile.." 
	f=open(".bash","w")
	bash.append("PATH=$HOME/bin:$PATH")
	f.write("\n".join(bash))
	f.close()
else:
	print ".bash profile already modified..."

os.system("source .bash_profile")
url="http://pypi.python.org/packages/"
url+="source/v/virtualenv/virtualenv-1.3.3.tar.gz"
os.system("curl "+url+"  > ve.tar.gz")

os.system("tar xzf ve.tar.gz")



os.system("python2.5 virtualenv-1.3.3/virtualenv.py $HOME") 
# installs a local version of python2.5
# now you should have a folder bin/python2.5 that runs.
os.system("rm -fr virtualenv-1.3.3") #cleanup
os.system("rm ve.tar.gz") # cleanup

# get a fresh copy of web2py
url="http://www.web2py.com/examples/static/web2py_src.zip"
os.system("curl "+url+"  > w2p.zip")
os.system("unzip w2p.zip")

os.system("mv web2py/* "+subdomain+"/")


os.system("rm w2p.zip")
os.system("rm -r web2py")

command="ln -s ~/" + subdomain + \
"/wsgihandler.py ~/" + subdomain + "/passenger_wsgi.py"

os.system(command)

ws=open(subdomain+"/wsgihandler.py").read().split("\n")
found=0
for line in ws:
	if line=='INTERP = "/home/'+user+'/bin/python2.5"':
		found=1
if found==1:
	print "wsgihandler.py already modified"
else:
	for l in range(len(ws)):
		if ws[l]=="#!/usr/bin/env python":
			ws[l]=="#!/usr/bin/env python2.5"
		if ws[l]=='import os':
			ws[l]='import os\nINTERP = "/home/'+ \
user+'/bin/python2.5"\nif sys.executable !'+ \
'= INTERP: os.execl(INTERP, INTERP, *sys.argv)'
	f=open(subdomain+"/wsgihandler.py","w")
	f.write("\n".join(ws))
	f.close()

os.system("python2.5 "+subdomain+"/web2py.py")