This week only. Pastebin PRO Accounts Christmas Special! Don't miss out!Want more features on Pastebin? Sign Up, it's FREE!
Guest

CalUpload Bash Script

By: a guest on Jun 28th, 2012  |  syntax: Bash  |  size: 5.84 KB  |  views: 166  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #!/bin/bash
  2. #===============================================================================
  3. #
  4. # CalUpload shell script
  5. #
  6. # This script will search for iCal styled calendar definitions in the file
  7. # system, and collect the event ics files into a single calendar ics file
  8. #
  9. # Some specialized behaviour is defined and described below based on the needs
  10. # of the contractor.
  11. #
  12. # Copyright 2012 by David Paterson and C.T. Paterson
  13. #
  14. # This work is licensed under a Creative Commons Attribution-NonCommercial-
  15. # ShareAlike 3.0 Unported License.
  16. # Refer to http://creativecommons.org/licenses/by-nc-sa/3.0 for full text
  17. #
  18. #===============================================================================
  19.  
  20. # where to look for the calendar files
  21. BASE_CAL_DIRECTORY=${HOME}/Library/Calendars
  22.  
  23. # where to put the resulting calendar file(s)
  24. TARGET_ICS_DIRECTORY=${HOME}
  25.  
  26. # Set as empty string ("") to do all calendars
  27. CALENDAR_TO_PUBLISH="OrangeGroveSpa"
  28.  
  29. #===============================================================================
  30. #
  31. # collect_events_into_calendar_ics
  32. #
  33. # Recent version of iCal saves a calendar by creating an .ics file for each
  34. # event in the calendar.  This function will, in a given directory, extract the
  35. # event details out of each event .ics file and concatenate them together,
  36. # wrapping the result in the necessary header and footer data to create an .ics
  37. # file for the full calendar
  38. #
  39. # Inputs:
  40. # $1 - the calendar directory
  41. #
  42. # Outputs:
  43. # The resuling calendar .ics file is written to stdout (standard output)
  44. #
  45. #===============================================================================
  46.  
  47. function collect_events_into_calendar_ics {
  48.   EVENT_DIR=${1}/Events
  49.  
  50.   # write the head of the calendar ICS
  51.   cat <<ICS_HEAD
  52. BEGIN:VCALENDAR
  53. VERSION:2.0
  54. PRODID:-//Apple Inc.//iCal 5.0.2//EN
  55. CALSCALE:GREGORIAN
  56. ICS_HEAD
  57.  
  58.   # iterate through ics files in the Events directory, and write only
  59.   # the event definition in each (that is, not the whole file)
  60.   for E in $(ls ${EVENT_DIR}/ 2>/dev/null | grep .ics); do
  61.     get_events_from_file ${EVENT_DIR}/${E}
  62.   done
  63.  
  64.   # write the tail of the calendar ICS
  65.   cat <<ICS_TAIL
  66. END:VCALENDAR
  67. ICS_TAIL
  68. }
  69.  
  70.  
  71. #===============================================================================
  72. #
  73. # get_events_from_file
  74. #
  75. # Process an event ics file and extract the VEVENT definition for each event,
  76. # writing to stdout.
  77. #
  78. # Some data massaging is done at the request of the contractor to suppress
  79. # private details in the events that they do not wish published.
  80. #
  81. # Inputs:
  82. # $1 - the event file name
  83. #
  84. # Outputs:
  85. # The resuling VEVENT definitions, written to stdout (standard output)
  86. #
  87. #===============================================================================
  88. function get_events_from_file {
  89.   EVENT_FILE=${1}
  90.  
  91.   # This script was written to take a business calendar and publish it
  92.   # online to show available vs. booked times.
  93.   #
  94.   # For reasons of privacy, many event details are suppressed, to
  95.   # let all details display, delete everything from "sed" to the end
  96.   # of the command.
  97.   cat ${EVENT_FILE} | \
  98.   awk 'BEGIN { is_event=0 }
  99.       /^BEGIN:VEVENT/ { is_event=1 }
  100.       /^END:VEVENT/ { print $0; is_event=0 }
  101.       { if ( 1 == is_event ) { print $0 } }' | \
  102.   sed -e 's/SUMMARY:.*/SUMMARY:Booked/' \
  103.       -e 's/URI:.*/URI:/' \
  104.       -e 's/DESCRIPTION:.*/DESCRIPTION:/' \
  105.       -e 's/LOCATION:.*/LOCATION:/' \
  106.       -e '/^ /d'
  107. }
  108.  
  109.  
  110. #===============================================================================
  111. #
  112. # get_calendar_name
  113. #
  114. # From within a specified calendar directory, the function will parse out the
  115. # "Title" value from the Info.plist file, and return the value
  116. #
  117. # Inputs:
  118. # $1 - the calendar directory
  119. #
  120. # Outputs:
  121. # The value assigned to the "Title" in the Info.plist file
  122. #
  123. #===============================================================================
  124. function get_calendar_name {
  125.   CAL_DIR=${1}
  126.  
  127.   awk '/Title/ { getline; print }' ${CAL_DIR}/Info.plist | cut -d '>' -f2 | cut -d '<' -f1
  128. }
  129.  
  130.  
  131. #===============================================================================
  132. #
  133. # is_event_calendar
  134. #
  135. # Parse out the EventContainer value from the calendar's Info.plist file.
  136. #
  137. # Inputs:
  138. # $1 - the calendar directory
  139. #
  140. # Outputs:
  141. # The value associated with the EventContainer key, expected to be "true" or
  142. # "false"
  143. #
  144. #===============================================================================
  145. function is_event_calendar {
  146.   CAL_DIR=${1}
  147.  
  148.   # Only the gods know what Apple intends; but we think the difference between a
  149.   # calendar built for events, and it's companion calendar for tasks, is the
  150.   # setting for EventContainer.  So, the setting for EventContainer in the Info.plist
  151.   # file will be what determines whether this is an "event calendar."
  152.   awk '/EventContainer/ { getline; print }' ${CAL_DIR}/Info.plist | cut -d '<' -f2 | cut -d '/' -f1
  153. }
  154.  
  155.  
  156. ### MAINLINE ###
  157.  
  158. # Loop through all the calendar directories found within the subdirectories below
  159. # the target directory (set as BASE_CAL_DIRECTORY)
  160. for C in $(find ${BASE_CAL_DIRECTORY}/ -name "*.calendar"); do
  161.  
  162.   # we're only interested in event calendars (as opposed to calendars with only tasks)
  163.   if [[ true =~ $(is_event_calendar ${C}) ]]; then
  164.     # Get the title of each calendar from the info file
  165.     RETRIEVED_CAL_NAME=$(get_calendar_name ${C})
  166.  
  167.     # If the user did no specify a calendar name, or if they did and we've found it...
  168.     if [[ "" == "${CALENDAR_TO_PUBLISH}" ]] || [[ "${CALENDAR_TO_PUBLISH}" == "${RETRIEVED_CAL_NAME}" ]]; then
  169.  
  170.       # ... collect the data from the event files into a single calendar file, and put it
  171.       # in the directory specified (the TARGET_ICS_DIRECTORY).
  172.       collect_events_into_calendar_ics ${C} > "${TARGET_ICS_DIRECTORY}/${RETRIEVED_CAL_NAME}.full.ics"
  173.     fi
  174.   fi
  175. done
  176.  
  177. # We're done.
  178. exit
clone this paste RAW Paste Data