calendar-export (1422B)
1 #!/bin/sh 2 # parse calendar(1) output to iCalendar (.ics) format 3 # NOTE: assumes all events occur in current year 4 5 calfile="$HOME/.calendar/calendar" 6 year="$(date '+%Y')" 7 8 die() 9 { 10 printf '%s: error: %s\n' "${0##*/}" "$1" >&2 11 exit 1 12 } 13 14 if test ! -r "$calfile"; then 15 die "calendar file $calfile does not exist" 16 fi 17 18 ical_header() 19 { 20 printf 'BEGIN:VCALENDAR\r\n' 21 printf 'VERSION:2.0\r\n' 22 printf 'PRODID:-//%s//calendar-export:2.0\r\n' "$(whoami)" 23 printf 'X-WR-TIMEZONE:Etc/GMT\r\n' 24 } 25 26 ical_footer() 27 { 28 printf 'END:VCALENDAR\r\n' 29 } 30 31 ical_event() 32 { 33 datestr="$(printf '%s' "$1" | cut -d' ' -f1)" 34 case "$datestr" in 35 Jan\ *) month=01;; 36 Feb\ *) month=02;; 37 Mar\ *) month=03;; 38 Apr\ *) month=04;; 39 May\ *) month=05;; 40 Jun\ *) month=06;; 41 Jul\ *) month=07;; 42 Aug\ *) month=08;; 43 Sep\ *) month=09;; 44 Oct\ *) month=10;; 45 Nov\ *) month=11;; 46 Dec\ *) month=12;; 47 *) die "could not extract month from $1";; 48 esac 49 date="$(printf '%s' "$datestr" | sed 's/.*\([0-3][0-9]\).*/\1/')" 50 desc="$(printf '%s' "$1" | cut -d' ' -f2-)" 51 52 printf 'BEGIN:VEVENT\r\n' 53 printf 'UID:none@none.com\r\n' 54 printf 'DTSTAMP:19970610T172345Z\r\n' 55 printf 'DTSTART:%s%s%s\r\n' "$year" "$month" "$date" 56 printf 'DTEND:%s%s%s\r\n' "$year" "$month" "$date" 57 printf 'SUMMARY:%s\r\n' "$desc" 58 printf 'END:VEVENT\r\n' 59 } 60 61 ical_header 62 63 calendar -f "$calfile" -B 365 -A 365 | awk '!x[$0]++' | while read -r; do 64 ical_event "$REPLY" 65 done 66 67 ical_footer