1
#!/bin/sh
2
3
usage="\
4
rcnotify.sh <<update|up>|<show|sh>|<remove|rm> <msg name>|add <msg name> <msg content>|<edit|ed> <msg name>>
5
"
6
7
dot_dir=.rcnotify
8
scripts_dir=$HOME/$dot_dir/scripts
9
mbox_dir=$HOME/$dot_dir/mbox
10
11
check() {
12
	[ -d $scripts_dir ] && mkdir -p $scripts_dir
13
	[ -d $mbox_dir ] && mkdir -p $mbox_dir
14
}
15
16
update_msg() {
17
	# we will working at temp dir and remove it at exiting in case the msg scripts
18
	# leave rubbish...
19
	TMP=/tmp
20
	work_dir=$(mktemp -d $TMP/rcnotify.XXXXXX) || exit 1
21
	cd $work_dir &>/dev/null
22
23
	# execute the msg scripts
24
	for i in $scripts_dir/*.sh; do
25
		msg=$(sh $i)
26
		if [ -n "$msg" ]; then
27
			#FIXME: a up coming msg will over write the old one...
28
			echo "$msg" > $mbox_dir/$(basename $i .sh)
29
		fi
30
		# cleanup the work folder or the following script may have trouble
31
		rm -rf *
32
	done
33
34
	# do cleanups
35
	cd - &>/dev/null
36
	rm -rf $work_dir
37
}
38
39
add_msg() {
40
	name=$1
41
	shift
42
	echo "$@" > $mbox_dir/$name
43
}
44
45
show_msg() {
46
	cd $mbox_dir &>/dev/null
47
	for i in *[^~]; do
48
		sed -e "s%^%$i: %" $i 2>/dev/null
49
	done
50
}
51
52
remove_msg() {
53
	while [ "$1" != "" ]; do
54
		rm $mbox_dir/$1 &>/dev/null
55
		shift
56
	done
57
}
58
59
edit_msg() {
60
	if [ -n "$EDITOR" ]; then
61
		editor=$EDITOR
62
	elif [ -n "$VISUAL" ]; then
63
		editor=$VISUAL
64
	elif [ -x /usr/bin/vim ]; then
65
		editor=/usr/bin/vim
66
	elif [ -x /usr/bin/emacs ]; then
67
		editor=/usr/bin/emacs
68
	else
69
		echo "Please specify your editor in VISUAL or EDITOR env."
70
		exit 1
71
	fi
72
	[ $# -eq 0 ] && echo "rcnotify.sh ed <msg_file_names...>"
73
	while [ $# -gt 0 ]; do
74
		eval $editor $mbox_dir/$1
75
		shift
76
	done
77
}
78
79
help() {
80
	echo $usage
81
}
82
83
main() {
84
	check
85
	[ $# -eq 0 ] && { show_msg; exit 0;}
86
	while [ "$1" != "" ]; do
87
		case $1 in
88
			"update" | "up")
89
			update_msg
90
			;;
91
			"show" | "sh")
92
			show_msg
93
			;;
94
			"remove" | "rm")
95
			shift
96
			remove_msg $@
97
			;;
98
			"add")
99
			shift
100
			add_msg $@
101
			;;
102
			"edit" | "ed")
103
			shift
104
			edit_msg $@
105
			;;
106
			*)
107
			help
108
			;;
109
		esac
110
		shift
111
	done
112
}
113
114
main $@