calendar

#!/bin/sh
# reads files and outputs lines beginning with today's and tomorrow's date
# like calendar(1) <http://plan9.bell-labs.com/magic/man2html/1/calendar>
# © 2014 Nils Dagsson Moskopp (erlehmann) – license: GPLv3+
date=$(date +%Y-%m-%d)
days_next=1
days_prev=0
file=$HOME/calendar

while [ $# != 0 ]; do
  case $1 in
    -A) shift; days_next=$1;;
    -B) shift; days_prev=$1;;
    -t) shift; date=$1;;
    -f) shift; file=$1;;
  esac
  shift
done

ts_now=$(date +%s -d "$date 00:00:00")
ts_start=$(expr $ts_now - '(' 86400 '*' $days_prev ')')
ts_end=$(expr $ts_now + '(' 86400 '*' $days_next ')' + '86399')

IFS='	'
cpp() {
  abspath=$(readlink -f $1)
  cd ${abspath%/*}
  sed -e "$(sed -n 's:#include.*"\(.*\)":/&/ { r \1\nd }:p' < $1)" $1
}
cpp $file | while read date note; do
  case "$date" in
    '#'*) continue;;
  esac
  case "$note" in
    '') continue;;
  esac
  if [ ${#date} -eq 10 ]; then
    ts=$(date +%s -d "$date 00:00:00")
  else
    ts=$(date +%s -d $date)
  fi
  if [ $ts -ge $ts_start ] && [ $ts -lt $ts_end ]; then
    printf '%s\t%s\n' "$date" "$note"
  fi
done | sort -n