#!/usr/bin/python
#
# Copyright (C) 2011 Reece H. Dunn
# Licence: GPLv3
#
# A script for generating the dictionary Makefile rules from the files in dictsource.

import sys
import os

voices = set()
dictionaries = {}
phoneme_data = set()
mbrola = set()

# Map voice names to dictionaries when these do not match.
special_voices = {
	'bs': 'hbs',
	'hr': 'hbs',
	'zh-yue': 'zhy',
}

# Support for extended dictionaries.
extended_dictionaries = {
	'ru':  'ru_listx',
	'zh':  'zh_listx',
	'zhy': 'zhy_list',
}

exclude_voices = []

def find_voices(path):
	for filename in os.listdir(path):
		voice_path = os.path.join(path, filename)
		if os.path.isdir(voice_path):
			if not filename in ['!v', 'mb']:
				find_voices(voice_path)
		else:
			if filename in special_voices.keys():
				voices.add(special_voices[filename])
			elif filename not in exclude_voices:
				voices.add(filename)

def find_phoneme_data(path):
	for filename in os.listdir(path):
		phondata_path = os.path.join(path, filename)
		if filename.startswith('ph_'):
			phoneme_data.add(phondata_path)

def find_mbrola_voices(path):
	for filename in os.listdir(path):
		mbrola.add(filename)

find_voices('espeak-data/voices')
find_phoneme_data('phsource')
find_mbrola_voices('phsource/mbrola')

for filename in os.listdir('dictsource'):
	if filename.endswith('_rules') or filename.endswith('_list') or filename in ['bg_listx', 'it_listx']:
		dic, cat = filename.split('_')
		if dic in voices:
			if not dic in dictionaries.keys():
				dictionaries[dic] = []
			dictionaries[dic].append('dictsource/%s' % filename)

def write_phoneme_data_rules(f):
	f.write('##### phoneme data:\n')
	f.write('\n')
	f.write('espeak-data/phondata: phsource/phonemes.stamp\n')
	f.write('espeak-data/phondata-manifest: phsource/phonemes.stamp\n')
	f.write('espeak-data/phonindex: phsource/phonemes.stamp\n')
	f.write('espeak-data/phontab: phsource/phonemes.stamp\n')
	f.write('espeak-data/intonations: phsource/phonemes.stamp\n')
	f.write('\n')
	f.write('phsource/phonemes.stamp: \\\n')
	for phonfile in sorted(phoneme_data):
		f.write('\t%s \\\n' % phonfile)
	f.write('\tphsource/phonemes \\\n')
	f.write('\tsrc/espeakedit\n')
	f.write('\tESPEAK_DATA_PATH=$(PWD) src/espeakedit --compile && touch $@\n')
	f.write('\n')

def write_dictionary_make_rules(f):
	f.write('##### dictionaries:\n')
	f.write('\n')
	f.write('dictionaries: \\\n')
	for n, name in enumerate(sorted(dictionaries.keys())):
		if not name in ['bo']: # espeak fails to read these voices
			if n == len(dictionaries.keys()) - 1:
				f.write('\tespeak-data/%s_dict\n' % name)
			else:
				f.write('\tespeak-data/%s_dict \\\n' % name)
	for name, files in sorted(dictionaries.items()):
		f.write('\n')
		f.write('%s: espeak-data/%s_dict\n' % (name, name))
		f.write('dictsource/%s_extra:\n' % name)
		f.write('\ttouch dictsource/%s_extra\n' % name)
		if name in extended_dictionaries.keys():
			ext = extended_dictionaries[name]
			f.write('dictsource/%s:\n' % ext)
			f.write('\tln -svf extra/%s dictsource/\n' % ext)
			f.write('if HAVE_%s_EXTENDED_DICTIONARY\n' % name.upper())
			f.write('espeak-data/%s_dict: src/espeak phsource/phonemes.stamp %s dictsource/%s_extra dictsource/%s\n' % (name, ' '.join(sorted(files)), name, ext))
			f.write('else\n')
		f.write('espeak-data/%s_dict: src/espeak phsource/phonemes.stamp %s dictsource/%s_extra\n' % (name, ' '.join(sorted(files)), name))
		if name in extended_dictionaries.keys():
			f.write('endif\n')
		f.write('\tcd dictsource && ESPEAK_DATA_PATH=$(PWD) LD_LIBRARY_PATH=../src:${LD_LIBRARY_PATH} ../src/espeak --compile=%s && cd ..\n' % name)

def write_mbrola_make_rules(f):
	f.write('\n##### mbrola:\n')
	f.write('\n')
	f.write('mbrola: \\\n')
	for n, name in enumerate(sorted(mbrola)):
		if n == len(mbrola) - 1:
			f.write('\tespeak-data/mbrola_ph/%s_phtrans\n' % name)
		else:
			f.write('\tespeak-data/mbrola_ph/%s_phtrans \\\n' % name)
	for name in sorted(mbrola):
		f.write('\n')
		f.write('espeak-data/mbrola_ph/%s_phtrans: phsource/mbrola/%s src/espeakedit\n' % (name, name))
		f.write('\tmkdir -p espeak-data/mbrola_ph\n')
		f.write('\tESPEAK_DATA_PATH=$(PWD) src/espeakedit --compile-mbrola ${PWD}/$<\n')

try:
	filename = sys.argv[1]
except:
	filename = None

if filename:
	with open(filename, 'r') as f:
		prelude = f.read().split('##### phoneme data:\n')[0]
	with open(filename, 'w') as f:
		f.write(prelude)
		write_phoneme_data_rules(f)
		write_dictionary_make_rules(f)
		write_mbrola_make_rules(f)
else:
	write_phoneme_data_rules(sys.stdout)
	write_dictionary_make_rules(sys.stdout)
	write_mbrola_make_rules(sys.stdout)
