]> us-delaporte.pagekite.me Git - minion.git/blob - bin/mg
c78f32ad9461b6b6a99f547a2a89ec70e9c6474b
[minion.git] / bin / mg
1 #!/bin/bash
2 # mg - minion gather
3 # Creates a new file $HOME/Journal/YYYY/MM-topic.md with all contents from all matches for all terms
4 # Optionally include a second search term
5
6 year=`date +%Y`
7 # File to gather all content into
8 topic=$1
9 if [ ! -n "$1" ]; then
10 echo "A topic to gather is required."
11 exit 1
12 fi
13 fname=$(printf "$HOME/Journal/$year/%02d-$topic.md" $(date +%m))
14 archive="$HOME/Journal/archive-$year/"
15
16 # Files to gather
17 files=$(find $HOME/Journal/$year -type f -iname "*$1*\.md")
18 if [ -n "$files" ]; then
19 echo "Will merge $files into $fname"
20 else
21 echo "Nothing to gather for $topic"
22 fi
23
24 # optional - more files to gather
25 if [ -n "$2" ]; then
26 more_files=$(find $HOME/Journal/$year -type f -iname "*$2*\.md")
27 fi
28 if [ -n "$more_files" ]; then
29 echo "Will merge $more_files into $fname"
30 else
31 echo "No second keyword."
32 fi
33
34 echo ""
35 read -n1 -p "Continue? [y,N]" input
36 echo ""
37 if [[ ! $input == "Y" && ! $input == "y" ]]; then
38 echo "Aborted by user"
39 exit 1
40 fi
41
42 for f in $files; do
43 echo "Gathering $f into $fname"
44 cat "$f" >> "$fname.gathered"
45 done
46
47 mkdir -p $archive
48 mv $files $archive
49
50 if [ -n "$more_files" ]; then
51
52 for f in $more_files; do
53 echo "Gathering $f into $fname"
54 cat "$f" >> "$fname.gathered"
55 done
56
57 mv $more_files $archive
58 fi
59
60 mv $fname.gathered $fname
61
62 if [ -z ${EDITOR} ]; then
63 echo "EDITOR unset, defaulting to Nano"
64 export EDITOR=nano
65 fi
66 $EDITOR $fname
67