#!/usr/bin/env python

#####
# Name: Simple iCal file (ics) merger
# Version: 0.2
# Status: stable, but not much tested
# Danger: very small
# Creator: Oliver Kuka http://justanotherhack.net/
# Year: 2009
# Attention: I give no warranty that it works and I take no
#   responsibility if anything goes wrong or is damaged.
# License: Creative Commons cc (http://creativecommons.org/)
# Documentation: Nada, just the comments.
#####

# Configuration
#
# Files to merge, seperated by commas, as many or as few as one likes.
INPUTFILES = ["A:\path\to\Local.ics","A:\path\to\Work.ics","A:\path\to\Private.ics"]
# Result file for iCal-merge
OUTPUTFILE = "A:\path\to\iCalMerge4PDA.ics"

# DO NOT EDIT BELOW THIS LINE. (Of course, unless you know what you are doing ;-) .)

import fileinput
# + Start iCal Header Creation
OUTPUTCNT = "BEGIN:VCALENDAR\nPRODID:Python_iCal_Merger\nVERSION:2.0\n"
# + + Start Timezone specific lines (CET/CEST)
OUTPUTCNT = OUTPUTCNT + "BEGIN:VTIMEZONE\nTZID:Europe/Berlin\nX-LIC-LOCATION:Europe/Berlin\n"
# + + - Daylight Saving Time
OUTPUTCNT = OUTPUTCNT + "BEGIN:DAYLIGHT\nTZOFFSETFROM:+0100\nTZOFFSETTO:+0200\nTZNAME:CEST\nDTSTART:19700329T020000\nRRULE:FREQ=YEARLY;INTERVAL=1;BYDAY=-1SU;BYMONTH=3\nEND:DAYLIGHT\n"
# + + - Standard (Winter) Time
OUTPUTCNT = OUTPUTCNT + "BEGIN:STANDARD\nTZOFFSETFROM:+0200\nTZOFFSETTO:+0100\nTZNAME:CET\nDTSTART:19701025T030000\nRRULE:FREQ=YEARLY;INTERVAL=1;BYDAY=-1SU;BYMONTH=10\nEND:STANDARD\n"
# + + End Timezone specific lines 
OUTPUTCNT = OUTPUTCNT + "END:VTIMEZONE\n"
# + End iCal Header Creation
# + Start merge process
for INPUTFILE in INPUTFILES:
	PRINTIT = False
	for LINE in fileinput.input(files=INPUTFILE):
		LINE = LINE.rstrip()
		if LINE == 'BEGIN:VEVENT':
			PRINTIT = True
		if PRINTIT == True:
			OUTPUTCNT = OUTPUTCNT + LINE + "\n"
		if LINE == 'END:VEVENT':
			PRINTIT = False
OUTPUTCNT = OUTPUTCNT + "END:VCALENDAR"
# + End merge process
# - Write out
OUTPUT = open(OUTPUTFILE, mode='w')
print (OUTPUTCNT, file=OUTPUT)
OUTPUT.close();
# fin !