]> pd.if.org Git - zpackage/blob - zpm-note
add zpm-soneed
[zpackage] / zpm-note
1 #!/bin/sh
2
3 # Admin notes:
4
5 # Each note is just a file.  Stored under /var/spool/admin/notes/[open|ack]/<package>/<note>
6
7 # note: list notes
8 # note ack <note> - moves note to "acknowledged" spool
9 # note list [package] show notes for a given package, or all notes
10 # note delete <note> - delete a note from the system
11 # note edit <package> [file] - edit or create a note
12
13 # TODO
14 # discard unchanged new note file
15 # quiet options to suppress new note name echo
16 # semi-quiet to just output the number
17 # renumber? to renumber all notes/reset sequence
18 # lock the temp files
19 # clean up unlocked temp files
20
21 : ${ZPMSPOOL:=/var/lib/admin/notes}
22
23 OPEN=$ZPMSPOOL/open
24
25 set -e
26
27 umask 007
28
29 #cd $SPOOL || { echo "can't chdir to $SPOOL" ; exit 1; }
30
31 [ -z "$1" ] && set list
32
33 quiet=
34
35 cmd=$1
36 shift
37
38 findnote() {
39         file=$(find $ZPMSPOOL -type f -name "$1")
40         if [ -z "$file" ] ; then
41                 [ -z "$quiet" ] || printf "no such note $1\n" 1>&2
42                 exit 1
43         fi
44         echo $file
45 }
46
47 findopen() {
48         file=$(find $ZPMSPOOL/open -type f -name "$1")
49         if [ -z "$file" ] ; then
50                 [ -z "$quiet" ] || printf "no such open note $1\n" 1>&2
51                 exit 1
52         fi
53         echo $file
54 }
55
56 case $cmd in
57         category)
58                 notefile=$(findnote $1)
59                 printf "%s\n" $(basename $(dirname $notefile))
60                 ;;
61         list)
62                 case $1 in
63                         -a) find $ZPMSPOOL/ack -type f -printf '%P\n' | sort -n
64                                 ;;
65                         ?*) if [ -d $ZPMSPOOL/open/$1 ] ; then
66                                 find $ZPMSPOOL/open/$1 -type f -printf '%P\n' | sort -n
67                         else
68                                 exit 1
69                         fi
70                                 ;;
71                         *) find $ZPMSPOOL/open -type f -printf '%P\n' | sort -n
72                                 ;;
73                 esac
74                 ;;
75         detail)
76                 len=$(find $ZPMSPOOL/open -type f -printf "%P\n" | awk ' { if ( length > x ) { x = length } }END{ print x }')
77                 find $ZPMSPOOL/open -type f -printf "%P\n" | sort -n | while read note ; do
78                         subject=$(head -1 $ZPMSPOOL/open/$note) 
79                         date=$(stat -c '%y' $ZPMSPOOL/open/$note | cut -f1 -d' ')
80                         owner=$(stat -c '%U' $ZPMSPOOL/open/$note)
81                         printf '%*s %8s %s %s\n' $len $note $owner "$date" "$subject"
82                 done
83                 ;;
84         ack)
85                 file=$(findopen $1)
86                 subfile=$(echo $file | sed -e "s|$ZPMSPOOL/open/||")
87                 filedir=$(dirname $subfile)
88                 mkdir -p $ZPMSPOOL/ack/$filedir
89                 mv $file $ZPMSPOOL/ack/$filedir
90                 ;;
91         new)
92                 # new -- edit a new file for package general (or ZPMNOTEPACKAGE)
93                 # new package -- edit a new note for package
94                 # new package file -- use file for new package note
95                 if [ $# -eq 0 ]; then set ${ZPMNOTEPACKAGE:-general}; fi
96
97                 # edit a new file for the note
98                 if [ $# -eq 1 ]; then
99                         tmp=$(mktemp -p $ZPMSPOOL/open)
100                         pkg=$1
101                         [ -f $ZPMSPOOL/.template ] && cp $ZPMSPOOL/.template $tmp
102                         #flock -n -E3 $tmp vim $tmp
103                         #if [ $? -eq 3 ]; then echo already editing $1; exit 1; fi
104                         ${EDITOR:-vi} $tmp
105                 else
106                         # take note from file or stdin
107                         pkg=$1 ; shift
108                         mkdir -p $ZPMSPOOL/open/$pkg
109                         case $1 in
110                                 '-')
111                                         tmp=$(mktemp -p $ZPMSPOOL/open)
112                                         cat - > $tmp
113                                         ;;
114                                 *)
115                                         tmp=$(mktemp -p $ZPMSPOOL/open)
116                                         cp $1 $tmp
117                                         ;;
118                         esac
119                 fi
120                 file=$(zpm sequence notes)
121                 mkdir -p $ZPMSPOOL/open/$pkg
122                 mv $tmp $ZPMSPOOL/open/$pkg/$file || { rm -f $tmp; exit 1; }
123                 # TODO run newhooks in $ZPMSPOOL/.hook/newnote
124                 echo "$pkg/$file"
125                 exit 0
126                 ;;
127         edit)
128                 file=$(findnote $1)
129                 flock -n -E3 $file vim $file
130                 if [ $? -eq 3 ]; then echo already editing $1; exit 1; fi
131                 ;;
132         resolve)
133                 file=$(findnote $1)
134                 echo $file
135                 ;;
136         remove)
137                 file=$(findnote $1)
138                 rm $file
139                 ;;
140         show)
141                 file=$(findnote $1)
142                 ${PAGER:-less} $file
143                 ;;
144         *)
145                 echo '$0: unknown command ' $cmd 1>&2
146                 exit 1
147 esac