1#!/usr/bin/env python
2# encoding: utf-8
3import sys
4import re
5import os
6import zipfile
7
8THEME_FILE_EXTENSIONS = ('.stx', '.bmp', '.fcc', '.ttf', '.png')
9
10def buildTheme(themeName):
11	if not os.path.isdir(themeName) or not os.path.isfile(os.path.join(themeName, "THEMERC")):
12		print ("Invalid theme name: " + themeName)
13		return
14
15	zf = zipfile.ZipFile(themeName + ".zip", 'w')
16
17	print ("Building '" + themeName + "' theme:")
18	os.chdir(themeName)
19
20	zf.write('THEMERC', './THEMERC')
21
22	filenames = os.listdir('.')
23	filenames.sort()
24	for filename in filenames:
25		if os.path.isfile(filename) and not filename[0] == '.' and filename.endswith(THEME_FILE_EXTENSIONS):
26			zf.write(filename, './' + filename)
27			print ("    Adding file: " + filename)
28
29	os.chdir('../')
30
31	zf.close()
32
33def buildAllThemes():
34	for f in os.listdir('.'):
35		if os.path.isdir(os.path.join('.', f)) and not f[0] == '.':
36			buildTheme(f)
37
38def parseSTX(theme_file, def_file, subcount):
39	comm = re.compile("<!--(.*?)-->", re.DOTALL)
40	head = re.compile("<\?(.*?)\?>")
41
42	strlitcount = 0
43	subcount += 1
44
45	def_file.write(";\n const char *defaultXML" + str(subcount) + " = ")
46
47	output = ""
48	for line in theme_file:
49		output +=  line.rstrip("\r\n\t ").lstrip()
50		if not output.endswith('>'):
51			output += ' '
52		output += "\n"
53
54	output = re.sub(comm, "", output)
55	output = re.sub(head, "", output)
56	output = output.replace("\t", " ").replace("  ", " ").replace("\"", "'")
57	output = output.replace(" = ", "=").replace(", ", ",")
58
59	for line in output.splitlines():
60		if line and not line.isspace():
61			strlitcount += len(line)
62			if strlitcount > 65535:
63				subcount += 1
64				def_file.write(";\n const char *defaultXML" + str(subcount) + " = ")
65				strlitcount = len(line)
66			def_file.write("\"" + line + "\"\n")
67	return subcount
68
69def buildDefTheme(themeName):
70	def_file = open("default.inc", "w")
71
72	if not os.path.isdir(themeName):
73		print ("Cannot open default theme dir.")
74
75	def_file.write("""const char *defaultXML1 = "<?xml version = '1.0'?>"\n""")
76	subcount = 1
77
78	filenames = os.listdir(themeName)
79	filenames.sort()
80	for filename in filenames:
81		filename = os.path.join(themeName, filename)
82		if os.path.isfile(filename) and filename.endswith(".stx"):
83			theme_file = open(filename, "r")
84			subcount = parseSTX(theme_file, def_file, subcount)
85			theme_file.close()
86
87	def_file.write(";\nconst char *defaultXML[] = { defaultXML1")
88	for sub in range(2, subcount + 1):
89		def_file.write(", defaultXML" + str(sub))
90
91	def_file.write(" };\n")
92
93	def_file.close()
94
95def printUsage():
96	print ("===============================")
97	print ("ScummVM Theme Generation Script")
98	print ("===============================")
99	print ("Usage:")
100	print ("scummtheme.py makeall")
101	print ("    Builds all the available themes.\n")
102	print ("scummtheme.py make [themename]")
103	print ("    Builds the theme called 'themename'.\n")
104	print ("scummtheme.py default [themename]")
105	print ("    Creates a 'default.inc' file to embed the given theme in the source code.\n")
106
107def main():
108
109	if len(sys.argv) == 2 and sys.argv[1] == "makeall":
110		buildAllThemes()
111
112	elif len(sys.argv) == 3 and sys.argv[1] == "make":
113		buildTheme(sys.argv[2])
114
115	elif len(sys.argv) == 3 and sys.argv[1] == "default":
116		buildDefTheme(sys.argv[2])
117
118	else:
119		printUsage()
120
121if __name__ == "__main__":
122	sys.exit(main())
123