#!/usr/bin/env python

import getopt
import os
import sys

def usage():
	print "renderAll.py [--shrouded] [--thumbnails | --big ] [--terrain] [--captions] [--structures] [--trees]"
	sys.exit()


workingDir = '/home/ceej/DaoC/mapper-20021126'
gameDir = '/home/ceej/DaoC'
destinationDir = '/home/ceej/html/Maps'
scale = 1024
overviewScale = 150
imageType = '.png'

originalZones = {
	'000' : 'alb_camelot_hills',
	'001' : 'alb_salisbury_plains',
	'002' : 'alb_black_mtns_south',
	'003' : 'alb_black_mtns_north',
	'004' : 'alb_dartmoor',
	'006' : 'alb_cornwall',
	'007' : 'alb_llyn_barfog',
	'008' : 'alb_campacorentin_forest',
	'009' : 'alb_avalon_marsh',
	'010' : 'alb_lyonesse',
	'011' : 'alb_forest_sauvage',
	'012' : 'alb_snowdonia',
	'014' : 'alb_pennine_mountains',
	'015' : 'alb_hadrians_wall',
	'100' : 'mid_vale_of_mularn',
	'101' : 'mid_svealand_east',
	'102' : 'mid_svealand_west',
	'103' : 'mid_gotar',
	'104' : 'mid_muspelheim',
	'105' : 'mid_myrkwood_forest',
	'106' : 'mid_skona_ravine',
	'107' : 'mid_vanern_swamp',
	'108' : 'mid_raumarik',
	'111' : 'mid_uppland',
	'112' : 'mid_yggdra_forest',
	'113' : 'mid_jamtland_mountains',
	'115' : 'mid_odins_gate',
	'116' : 'mid_malmohus',
	'200' : 'hib_lough_derg',
	'201' : 'hib_silvermine_mts',
	'202' : 'hib_shannon_estuary',
	'203' : 'hib_cliffs_of_moher',
	'204' : 'hib_lough_gur',
	'205' : 'hib_bog_of_cullen',
	'206' : 'hib_valley_of_bri_leith',
	'207' : 'hib_connacht',
	'208' : 'hib_cursed_forest',
	'210' : 'hib_mount_collory',
	'211' : 'hib_cruachan_gorge',
	'212' : 'hib_breifine',
	'214' : 'hib_emain_macha',
	'216' : 'hib_sheeroe_hills',
	'250' : 'rvr_caledon',
	'251' : 'rvr_murdaigean',
	'252' : 'rvr_thidranki',
	}
	
siZones = {
	'051' : 'alb_isle_of_glass',
	'052' : 'alb_avalon_isle',
	'053' : 'alb_dales_of_devwy',
	'054' : 'alb_caldey',
	'055' : 'alb_plain_of_gwyddneau',
	'056' : 'alb_aldland',
	'151' : 'mid_aegirs_landing',
	'152' : 'mid_gripklosa_mountains',
	'153' : 'mid_delling_crater',
	'154' : 'mid_faraheim',
	'155' : 'mid_munin_sound',
	'156' : 'mid_iarnwood',
	'181' : 'hib_domnan',
	'182' : 'hib_caillte_garran',
	'183' : 'hib_vale_of_balor',
	'184' : 'hib_cothrom_gorge',
	'185' : 'hib_worlds_end',
	'186' : 'hib_vigilant_rock',
	}	

try:
	optlist, pargs = getopt.getopt(sys.argv[1:], 'tb', ["thumbnails", "big", "captions", "shrouded", "terrain", "structures", "trees"])
except:
	usage()

# common setup
os.chdir(workingDir)
commandBase = 'python mapper.py'
logfile = "renderAll.%d.log" % (os.getpid())

# defaults
doCaptions = 0
doTerrain = 0
doStructures = 0
doTrees = 0
doThumbnails = 0
doBig = 0

options = {}
for pair in optlist:
	options[pair[0]] = pair[1]
	
if options.has_key('--shrouded'):
	zoneList = siZones
else:
	zoneList = originalZones

idlist = zoneList.keys()
idlist.sort()

if  options.has_key('--captions'):
	doCaptions = 1
if  options.has_key('--terrain'):
	doTerrain = 1
if  options.has_key('--structures'):
	doStructures = 1
if  options.has_key('--trees'):
	doTrees = 1

if options.has_key('-t') or options.has_key('--thumbnails'):
	doThumbnails = 1
elif  options.has_key('-b') or options.has_key('--big'):
	doBig = 1

if doThumbnails:
	unchangingPart = '-gamepath %s -scale %d' % (gameDir, overviewScale)
	for id in idlist:
		print "Building thumbnail for %s..." % (zoneList[id])
		os.system('%s -settings ceej_overview.ini %s -zone %s -out %s >> %s 2>&1' % (commandBase, unchangingPart, id, os.path.join(destinationDir, "zone_%s.thumb%s"%(id, imageType)), logfile ))

elif doBig:
	unchangingPart = '-gamepath %s -scale %d' % (gameDir, scale)
	for id in idlist:
		print "Building layers for %s..." % (zoneList[id])
		if doTerrain:
			print "    building terrain"
			os.system('%s -settings backdrop.ini %s -zone %s -out %s >> %s 2>&1' % (commandBase, unchangingPart, id, os.path.join(destinationDir, "%s.backdrop%s"%(zoneList[id], imageType)), logfile ))
		if doStructures:
			print "    building structures"
			os.system('%s -settings structures.ini %s -zone %s -out %s >> %s 2>&1' % (commandBase, unchangingPart, id, os.path.join(destinationDir, "%s.structs%s"%(zoneList[id], imageType)), logfile ))
		if doTrees:
			print "    building trees"
			os.system('%s -settings trees.ini %s -zone %s -out %s >> %s 2>&1' % (commandBase, unchangingPart, id, os.path.join(destinationDir, "%s.trees%s"%(zoneList[id], imageType)), logfile ))
		if doCaptions:
			print "    building captions"
			os.system('%s -settings captions_only.ini %s -zone %s -out %s >> %s 2>&1' % (commandBase, unchangingPart, id, os.path.join(destinationDir, "%s.captions%s"%(zoneList[id], imageType)), logfile ))

else:
	print "You forgot to ask me to do something."
	usage()


