#!/usr/bin/python
# -*- coding: UTF-8 -*-

import hebcalendar
import sys
import codecs
from convertdate import hebrew, gregorian, utils

import os
import win32com.client

def usage():
	print 'yahrtzeit.py Yahrtzeit Generation script'
	print 'Usage: yahrtzeit.py <Jewish year> <name of data file>'

def createDoc():
	wordapp = win32com.client.gencache.EnsureDispatch("Word.Application")
	wordapp.Visible = True # Word Application should be visible

	worddoc = wordapp.Documents.Open(os.getcwd() + "\emptyYahrtzeits.docx", False, False, False)

	worddoc.Content.Font.Size = 12

	return worddoc

def saveDoc(worddoc, year):
	worddoc.SaveAs(os.getcwd() + '\\yahrtzeitsFor' + repr(year) + '.docx')
	
def writeLine(worddoc, parsha, data):
	r = worddoc.Range().Paragraphs.Add().Range
	r.Font.SizeBi = 12
	r.Font.Name = "Arial"
	r.Font.BoldBi = True
	r.Font.Shading.BackgroundPatternColor = 50500
	r.Text = parsha
	r = worddoc.Range().Paragraphs.Add().Range
	r.Font.SizeBi = 12
	r.Font.Name = "Arial"
	r.Text = data
	r.ListFormat.ApplyBulletDefault()
	return r
	
dates = {}

if len(sys.argv) != 3:
	usage()
	sys.exit(1)

year = int(sys.argv[1])

inputFile = sys.argv[2]

# Read in file, build DB of dates
for line in codecs.open(inputFile, encoding='utf-8'):
	# Throw away anything after a comment character
	line = line.split('#')[0].strip()
	
	if len(line) == 0:
		continue
		
	name, date, info = [x.strip() for x in line.split(',', 3)]
	
	date = date.split('.', 3)
	
	if not hebrew.leap(year) and date[1] == '13':
		date[1] = '12'
	
	jd = hebrew.to_jd(year, int(date[1]), int(date[0]))
	
	if jd not in dates:
		dates[jd] = []
	
	dates[jd].append({'name': name, 'info': info, 'date': date})

# Get full date list from hebcalendar, filter for shabbat, chag
hebcalendar.setFilter(['shabbat', 'chag', 'RH', 'YK'])
holidays = hebcalendar.getYear(year, 'Israel')

# Okay, now I have a dict of shabbat/chagim, and a separate dict of yahrtzeits. Need to work through them both.

lastShabbat = 0
lastShabbatName = ''

yahrtzeits = sorted(dates)

doc = createDoc()

out = codecs.open('yahrtzeitsFor%d.txt' % year, 'w', encoding='utf-8')

for shabbat in sorted(holidays):
	day = holidays[shabbat]
	name = u', '.join(a['hebrew'] for a in day['fullnames'])
	greg = '%04d.%02d.%02d' % day['gregorian']
	heb = '%04d.%02d.%02d' % day['hebrew']
	name += u' - %s / %s:' % (greg, heb)
	
	yDates = [y for y in yahrtzeits if y > lastShabbat and y <= shabbat]

	if len(yDates) > 0:
		out.write(lastShabbatName + u'\n')
		parsha = lastShabbatName + u'\n'
		data = ''
		for date in sorted(yDates):
			# utils assumes the week starts on Monday, so adjust
			weekday = (utils.jwday(date) + 6) % 7
			for y in dates[date]:
				data += u"* %s: %s - %s\n" % (hebcalendar.numbers[weekday], y['name'], y['info'])
				out.write(data)
		writeLine(doc, parsha, data)
				

	lastShabbat = shabbat
	lastShabbatName = name
	
saveDoc(doc, year)
	
	
@@ -2631,16 +2631,43 @@
 ekday = 
+hebcalendar.hebrewDayOfWeek
 (utils.j
@@ -2680,17 +2680,9 @@
 ate)
- + 6) %25 7
+)
 %0A%09%09%09
@@ -2741,36 +2741,15 @@
  %25 (
-hebcalendar.numbers%5B
 weekday
-%5D
 , y%5B

Fix day of week wrong