]> pd.if.org Git - zpackage/blob - zpm-note
output improvements to elftype
[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                         *) find $ZPMSPOOL/open -type f -printf '%P\n' | sort -n
66                                 ;;
67                 esac
68                 ;;
69         detail)
70                 len=$(find $ZPMSPOOL/open -type f -printf "%P\n" | awk ' { if ( length > x ) { x = length } }END{ print x }')
71                 find $ZPMSPOOL/open -type f -printf "%P\n" | sort -n | while read note ; do
72                         subject=$(head -1 $ZPMSPOOL/open/$note) 
73                         date=$(stat -c '%y' $ZPMSPOOL/open/$note | cut -f1 -d' ')
74                         owner=$(stat -c '%U' $ZPMSPOOL/open/$note)
75                         printf '%*s %8s %s %s\n' $len $note $owner "$date" "$subject"
76                 done
77                 ;;
78         ack)
79                 file=$(findopen $1)
80                 subfile=$(echo $file | sed -e "s|$ZPMSPOOL/open/||")
81                 filedir=$(dirname $subfile)
82                 mkdir -p $ZPMSPOOL/ack/$filedir
83                 mv $file $ZPMSPOOL/ack/$filedir
84                 ;;
85         new)
86                 # new -- edit a new file for package general (or ZPMNOTEPACKAGE)
87                 # new package -- edit a new note for package
88                 # new package file -- use file for new package note
89                 if [ $# -eq 0 ]; then set ${ZPMNOTEPACKAGE:-general}; fi
90
91                 # edit a new file for the note
92                 if [ $# -eq 1 ]; then
93                         tmp=$(mktemp -p $ZPMSPOOL/open)
94                         pkg=$1
95                         [ -f $ZPMSPOOL/.template ] && cp $ZPMSPOOL/.template $tmp
96                         ${EDITOR:-vi} $tmp
97                         file=$(zpm sequence notes)
98                         mv $tmp $ZPMSPOOL/open/$pkg/$file || { rm -f $tmp; exit 1; }
99                         echo "$pkg/$file"
100                         exit 0
101                 fi
102
103                 # take note from file or stdin
104                 pkg=$1 ; shift
105                 mkdir -p $ZPMSPOOL/open/$pkg
106                 case $1 in
107                         '-')
108                                 tmp=$(mktemp -p $ZPMSPOOL/open)
109                                 cat - > $tmp
110                                 file=$(zpm sequence notes)
111                                 mv $tmp $ZPMSPOOL/open/$pkg/$file || { rm -f $tmp ; exit 1; }
112                                 echo $file
113                                 ;;
114                         *)
115                                 tmp=$(mktemp -p $ZPMSPOOL/open)
116                                 cp $1 $tmp
117                                 #flock -n -E3 $tmp vim $tmp
118                                 #if [ $? -eq 3 ]; then echo already editing $1; exit 1; fi
119                                 file=$(zpm sequence notes)
120                                 mv $tmp $ZPMSPOOL/open/$pkg/$file || { rm -f $tmp; exit 1; }
121                                 echo $file
122                                 ;;
123                 esac
124                 ;;
125         edit)
126                 file=$(findnote $1)
127                 flock -n -E3 $file vim $file
128                 if [ $? -eq 3 ]; then echo already editing $1; exit 1; fi
129                 ;;
130         resolve)
131                 file=$(findnote $1)
132                 echo $file
133                 ;;
134         remove)
135                 file=$(findnote $1)
136                 rm $file
137                 ;;
138         show)
139                 file=$(findnote $1)
140                 ${PAGER:-less} $file
141                 ;;
142         *)
143                 echo '$0: unknown command ' $cmd 1>&2
144                 exit 1
145 esac