added script. Initial commit.
[bash-notes.git] / notes.sh
1 #! /bin/bash
2
3 # set -ex
4
5 PID=$$
6 VERSION="0.1"
7 EDITOR=${EDITOR:-/usr/bin/vim}
8 BASEDIR=${BASEDIR:-~/.bash-notes}
9 DB=${BASEDIR}/db.json
10 TMPDB=/tmp/db.json
11 NOTESDIR=${BASEDIR}/notes
12 BASENAME=$( basename $0 )
13 TERMINAL=${TERMINAL:-/usr/bin/alacritty}
14 JQ=$(which jq)
15
16 if [ ! -x $JQ ]; then
17 echo "jq not found in your PATH"
18 echo "install jq to continue"
19 exit 1
20 fi
21
22 # We prevent the program from running more than one instance:
23 PIDFILE=/var/tmp/$(basename $0 .sh).pid
24
25 # Make sure the PID file is removed when we kill the process
26 trap 'rm -f $PIDFILE; exit 1' TERM INT
27
28 if [[ -r $PIDFILE ]]; then
29 # PIDFILE exists, so I guess there's already an instance running
30 # let's kill it and run again
31 kill -s 15 $(cat $PIDFILE) > /dev/null 2>&1
32 # should already be deleted by trap, but just to be sure
33 rm $PIDFILE
34 fi
35
36 # create PIDFILE
37 echo $PID > $PIDFILE
38
39 function helptext() {
40 echo "Parameters are:"
41 echo " -h : This help text"
42 echo " -s <directory> : specify directory where to store all notes."
43 echo " -e <editor> : specify EDITOR for this session only."
44 echo " -l : List existing notes"
45 echo " -a : Add new note"
46 echo " -m <note> : Modify note"
47 echo " -d <note> : Modify date for note"
48 echo " -r <note> : Remove note"
49 echo " -v : Print version"
50 }
51
52 function addnote() {
53 NOTETITLE=$1
54 echo "add new note"
55 NOW=$(date +%s)
56 FILEDATE=$(date -d @$NOW +%d/%m/%Y_%T)
57 LASTID=$($JQ '.notes[-1].id' $DB)
58 [ null == $LASTID ] && LASTID=0
59 NOTEID=$(( $LASTID + 1 ))
60 touch ${NOTESDIR}/${NOW}
61 $JQ --arg i "$NOTEID" --arg t "$NOTETITLE" --arg f "$NOW" '.notes += [{"id": $i, "title": $t, "file": $f}]' "$DB" > $TMPDB
62 mv $TMPDB $DB
63 NEWNOTE=$(${TERMINAL} --class notes --title notes -e ${EDITOR} ${NOTESDIR}/${NOW})
64 if [[ $NEWNOTE ]]; then
65 echo "New note saved!"
66 fi
67 }
68
69 function listnotes() {
70 echo "list all notes"
71 }
72
73 function editnote() {
74 echo "edit note \"${1}\""
75 }
76
77 function datenote() {
78 echo "edit date for note \"${1}\""
79 }
80
81 function rmnote() {
82 echo "remove note"
83 }
84
85 function firstrun() {
86 mkdir -p $NOTESDIR
87 cat << "__EOL__" > $DB
88 {
89 "notes": []
90 }
91 __EOL__
92 }
93
94 # check for notes dir existance and create it in case it doesn't exists
95 if [[ ! -d $NOTESDIR ]]; then
96 # we don't have a directory. FIRST RUN?
97 firstrun
98 fi
99
100 # Command line parameter processing:
101 while getopts ":a:hlvm:s:n:e:r:d:" Option
102 do
103 case $Option in
104 h ) helptext
105 exit
106 ;;
107 a ) TITLE=${OPTARG}
108 addnote $TITLE
109 ;;
110 l ) listnotes
111 ;;
112 m ) NOTE=${OPTARG}
113 editnote "${NOTE}"
114 ;;
115 d ) NOTE=${OPTARG}
116 datenote "${NOTE}"
117 ;;
118 r ) NOTE=${OPTARG}
119 rmnote "${NOTE}"
120 ;;
121 e ) EDITOR=${OPTARG}
122 ;;
123 s ) NOTESDIR=${OPTARG}
124 ;;
125 v ) echo $BASENAME v${VERSION}
126 ;;
127 * ) echo "You passed an illegal switch to the program!"
128 echo "Run '$0 -h' for more help."
129 exit
130 ;; # DEFAULT
131 esac
132 done
133
134 # End of option parsing.
135 shift $(($OPTIND - 1))
136 # $1 now references the first non option item supplied on the command line
137 # if one exists.
138 # ---------------------------------------------------------------------------
139