# Script for installing Festival 1.95 on Mac OS X 10.5 (Intel)
# The workarounds are from festival-talk mailing list.
# By Patty and Nathan Sakunkoo

import os,urllib,urllib2,re
def download(url,fname):
	print "Downloading %s" % url
	webFile = urllib.urlopen(url)
	localFile = open(fname, 'w')
	localFile.write(webFile.read())
	webFile.close()
	localFile.close()
# Get the tar balls
url = "http://festvox.org/packed/festival/1.96/"
html = urllib2.urlopen(url).read()
files = re.findall("href=\"(.*?\.gz)",html)
for f in files:
	# skip multisyn sound files because they are big.
	if f.find("multisyn")>0:
		continue
	download(url+f,f)
	os.system("tar -xzf "+f)
	os.system("rm "+f)

# Patch macosxaudio
# Leopard uses g++ 4.0 which deprecates some function in this file.
download("http://www.stanford.edu/class/cs224s/festival_mac/macosxaudio.cc","macosxaudio.cc")
os.system("mv macosxaudio.cc speech_tools/audio/macosxaudio.cc")
# Patch on Math macro
# something about NaN
download("http://www.stanford.edu/class/cs224s/festival_mac/EST_math.h","EST_math.h")
os.system("mv EST_math.h speech_tools/include/EST_math.h")
os.chdir("speech_tools")
os.system("make")
os.chdir("..")

# Make Festival use afplay as the audio player
download("http://www.stanford.edu/class/cs224s/festival_mac/siteinit.scm","siteinit.scm")
os.system("mv siteinit.scm festival/lib/siteinit.scm")
os.chdir("festival")
os.system("make")
os.chdir("bin")
os.system('echo "Hello World" | ./festival --tts')

