scripts

Various scripts for UNIX-like systems
git clone https://git.tronto.net/scripts
Download | Log | Files | Refs | README

event (3422B)


      1 #!/bin/sh
      2 
      3 # Manage events (replacement for https://git.tronto.net/sdep)
      4 
      5 # Usage: event [command [argument]]
      6 # See usage() for details
      7 
      8 file="$HOME/box/events" # Change to $XDG_DATA_HOME/events or whatever
      9 editor=${EDITOR:-vi}
     10 
     11 usage() {
     12 	echo "Usage: event [command]"
     13 	echo "Commands:"
     14 	echo "  add [day]    Add a new event (default date: none)"
     15 	echo "               day: today, t; tomorrow, tm"
     16 	echo "  clear        Sort and cleanup event file"
     17 	echo "  edit         Open event file in $editor"
     18 	echo "  list [date]  List all events on date (default: all events)"
     19 	echo "               date: now, n; today, t; tomorrow, tm;"
     20 	echo "                     week, w; nextweek, nw;"
     21 	echo "                     future, f; past, p"
     22 }
     23 
     24 secondstoday() {
     25 	if [ "$(uname)" = "Linux" ]; then
     26 		date -d @$1 +'%Y-%m-%d'
     27 	else # Assume BSD date
     28 		date -r $1 +'%Y-%m-%d'
     29 	fi
     30 }
     31 
     32 secondstomin() {
     33 	if [ "$(uname)" = "Linux" ]; then
     34 		date -d @$1 +'%Y-%m-%d %H:%M'
     35 	else # Assume BSD date
     36 		date -r $1 +'%Y-%m-%d %H:%M'
     37 	fi
     38 }
     39 
     40 datetoseconds() {
     41 	# Read date in YYYY-MM-DD HH:MM format
     42 	if [ "$(uname)" = "Linux" ]; then
     43 		date -d "$1 $2" +'%s'
     44 	else # Assume BSD date
     45 		date -f '%Y-%m-%d %H:%M' -j "$1 $2" +'%s'
     46 	fi
     47 }
     48 
     49 today() { echo "$(date +'%Y-%m-%d') 00:00"; }
     50 dayplus() {
     51 	d="$1"
     52 	n="$(datetoseconds $(today))"
     53 	echo "$((n+d*60*60*24))"
     54 }
     55 monday() { d="$(date +%u)"; dayplus "$((1-d))"; }
     56 monday1() { d="$(date +%u)"; dayplus "$((8-d))"; }
     57 monday2() { d="$(date +%u)"; dayplus "$((15-d))"; }
     58 daydiff() {
     59 	ar="$1"
     60 	d="$(date +%u)"
     61 	case "$ar" in
     62 		mon|monday)
     63 			w=1
     64 			;;
     65 		tue|tuesday)
     66 			w=2
     67 			;;
     68 		wed|wednesday)
     69 			w=3
     70 			;;
     71 		thu|thursday)
     72 			w=4
     73 			;;
     74 		fri|friday)
     75 			w=5
     76 			;;
     77 		sat|saturday)
     78 			w=6
     79 			;;
     80 		sun|sunday)
     81 			w=7
     82 			;;
     83 		*)
     84 			w=0
     85 			;;
     86 	esac
     87 	[ "$w" = "0" ] && echo "" || echo "$(((w-d+7) % 7))"
     88 }
     89 
     90 secondsline() {
     91 	# TODO: this function is not needed, need similar one with weekday
     92 	line="$(cat)"
     93 	s="$(datetoseconds $line)"
     94 	t="$(echo "$line" | sed -E 's/....-..-.. .{1,2}:.. //g')"
     95 	echo "$s $t"
     96 }
     97 
     98 filterlines() {
     99 	while read -r line; do
    100 		cleanline="$(echo "$line" | awk '{print $1, $2}')"
    101 		s="$(datetoseconds $cleanline)"
    102 		[ "$s" -ge "$1" ] && [ "$s" -lt "$2" ] && echo "$line"
    103 	done
    104 }
    105 
    106 list() {
    107 	if [ -z "$1" ]; then
    108 		cat
    109 	elif [ "$1" = "now" ] || [ "$1" = "n" ]; then
    110 		grep "$(date +'%Y-%m-%d %H:%M')"
    111 	elif [ "$1" = "today" ] || [ "$1" = "t" ]; then
    112 		grep "$(date +'%Y-%m-%d')"
    113 	elif [ "$1" = "tomorrow" ] || [ "$1" = "tm" ]; then
    114 		grep "$(secondstoday $(dayplus 1))"
    115 	elif [ "$1" = "week" ] || [ "$1" = "w" ]; then
    116 		filterlines "$(monday)" "$(monday1)"
    117 	elif [ "$1" = "nextweek" ] || [ "$1" = "nw" ]; then
    118 		filterlines "$(monday1)" "$(monday2)"
    119 	elif [ "$1" = "future" ] || [ "$1" = "f" ]; then
    120 		filterlines "$(date +%s)" "9999999999"
    121 	elif [ "$1" = "past" ] || [ "$1" = "p" ]; then
    122 		filterlines "0" "$(date +%s)"
    123 	fi
    124 }
    125 
    126 case "$1" in
    127 	add)
    128 		if [ "$2" = "today" ] || [ "$2" = "t" ]; then
    129 			d="$(date +'%Y-%m-%d')"
    130 		elif [ "$2" = "tomorrow" ] || [ "$2" = "tm" ]; then
    131 			d="$(secondstoday $(dayplus 1))"
    132 		elif [ -n "$2" ]; then
    133 			d="$(secondstoday $(dayplus $(daydiff $2)))"
    134 		fi
    135 		[ -n "$d" ] && printf '%s ' "$d"
    136 		read -r line
    137 		[ -n "$d" ] && line="$d $line"
    138 		echo "$line" >> "$file"
    139 		;;
    140 	clear)
    141 		tmp=$(mktemp)
    142 		cat "$file" | list future | sort > "$tmp"
    143 		mv "$tmp" "$file"
    144 		;;
    145 	edit)
    146 		"$editor" "$file"
    147 		;;
    148 	list)
    149 		cat "$file" | list $2 | sort
    150 		;;
    151 	*)
    152 		usage
    153 		;;
    154 esac