#!/bin/bash #=============================================================================== # # CalUpload shell script # # This script will search for iCal styled calendar definitions in the file # system, and collect the event ics files into a single calendar ics file # # Some specialized behaviour is defined and described below based on the needs # of the contractor. # # Copyright 2012 by David Paterson and C.T. Paterson # # This work is licensed under a Creative Commons Attribution-NonCommercial- # ShareAlike 3.0 Unported License. # Refer to http://creativecommons.org/licenses/by-nc-sa/3.0 for full text # #=============================================================================== # where to look for the calendar files BASE_CAL_DIRECTORY=${HOME}/Library/Calendars # where to put the resulting calendar file(s) TARGET_ICS_DIRECTORY=${HOME} # Set as empty string ("") to do all calendars CALENDAR_TO_PUBLISH="OrangeGroveSpa" #=============================================================================== # # collect_events_into_calendar_ics # # Recent version of iCal saves a calendar by creating an .ics file for each # event in the calendar. This function will, in a given directory, extract the # event details out of each event .ics file and concatenate them together, # wrapping the result in the necessary header and footer data to create an .ics # file for the full calendar # # Inputs: # $1 - the calendar directory # # Outputs: # The resuling calendar .ics file is written to stdout (standard output) # #=============================================================================== function collect_events_into_calendar_ics { EVENT_DIR=${1}/Events # write the head of the calendar ICS cat </dev/null | grep .ics); do get_events_from_file ${EVENT_DIR}/${E} done # write the tail of the calendar ICS cat <' -f2 | cut -d '<' -f1 } #=============================================================================== # # is_event_calendar # # Parse out the EventContainer value from the calendar's Info.plist file. # # Inputs: # $1 - the calendar directory # # Outputs: # The value associated with the EventContainer key, expected to be "true" or # "false" # #=============================================================================== function is_event_calendar { CAL_DIR=${1} # Only the gods know what Apple intends; but we think the difference between a # calendar built for events, and it's companion calendar for tasks, is the # setting for EventContainer. So, the setting for EventContainer in the Info.plist # file will be what determines whether this is an "event calendar." awk '/EventContainer/ { getline; print }' ${CAL_DIR}/Info.plist | cut -d '<' -f2 | cut -d '/' -f1 } ### MAINLINE ### # Loop through all the calendar directories found within the subdirectories below # the target directory (set as BASE_CAL_DIRECTORY) for C in $(find ${BASE_CAL_DIRECTORY}/ -name "*.calendar"); do # we're only interested in event calendars (as opposed to calendars with only tasks) if [[ true =~ $(is_event_calendar ${C}) ]]; then # Get the title of each calendar from the info file RETRIEVED_CAL_NAME=$(get_calendar_name ${C}) # If the user did no specify a calendar name, or if they did and we've found it... if [[ "" == "${CALENDAR_TO_PUBLISH}" ]] || [[ "${CALENDAR_TO_PUBLISH}" == "${RETRIEVED_CAL_NAME}" ]]; then # ... collect the data from the event files into a single calendar file, and put it # in the directory specified (the TARGET_ICS_DIRECTORY). collect_events_into_calendar_ics ${C} > "${TARGET_ICS_DIRECTORY}/${RETRIEVED_CAL_NAME}.full.ics" fi fi done # We're done. exit