aboutsummaryrefslogtreecommitdiff
path: root/docshower
blob: d7a06a5b49abdaee1249f4284cfe7e142cb831f2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#!/bin/bash
#
# Fast way to browse documents stored in an archive.
#

# Parameters
PROGRAM="$0"
BASENAME="`basename $0`"
DOCS="$HOME/data/doc"
BIBLIO="$DOCS/bibliographies"
FILELIST=".doclist"
LIST="$DOCS/$FILELIST"
DOC_PATTERN=".*\.(txt|doc|docx|rtf|pdf|sxc|csv|odt|odf|ods|xls|xlsx|ppt|epub|mobi|djvu|lit)"
FIND_OPTS="-regextype posix-egrep -iregex"
ARG="$1"
EXTRA_ARG="$2"
DATE="`date +%s`"
MAX_AGE="86400"
LOCK="$DOCS/.sync-media.lock"

# Fatal error
# Adapted from borger
function fatal {
  info [fatal] $*
  exit 1;
}

# Create lockfile
# Adapted from borger
function __set_lockfile {
  if [ ! -z "$LOCKFILE" ]; then
    mkdir -p `dirname $LOCKFILE`

    if ( set -o noclobber; echo "$$" > "$LOCKFILE" ) &> /dev/null; then
      trap '__unset_lockfile' INT TERM EXIT
    else
      fatal "Could not create lockfile $LOCKFILE, exiting"
    fi
  fi
}

# Remove lockfile
# Adapted from borger
function __unset_lockfile {
  if [ ! -z "$LOCKFILE" ]; then
    rm -f $LOCKFILE || echo "Could not remove lockfile $LOCKFILE"
  fi
}

# Check lockfile
# Adapted from borger
function __check_lockfile {
  local pid process

  if [ ! -z "$LOCKFILE" ] && [ -f "$LOCKFILE" ]; then
    pid="`cat $LOCKFILE`"
    process="`ps --no-headers -o comm $pid`"

    if [ "$?" == "0" ] && [ "`ps --no-headers -o comm $$`" == "$process" ]; then
      fatal "Another program is running for $LOCKFILE, skipping run"
    else
      echo "Found old lockfile $LOCKFILE, removing it"
      __unset_lockfile
    fi
  fi
}

# Update the filelist
function __update_filelist {
  # Lock file handling
  __check_lockfile
  __set_lockfile

  echo "Generating new filelist..."

  # Unnanex if it was erroneously annexed
  if [ -h "$LIST" ] && [ -d "$DOCS/.git/annex" ]; then
    ( cd $DOCS && git annex unlock $FILELIST )
  fi

  # Refresh
  cd $DOCS && find . -not -path '*.git*' $FIND_OPTS "$DOC_PATTERN" > $LIST

  # Stage
  git add $LIST

  # Unlock file handling
  __unlock_repo
}

# Check
if [ -z "$ARG" ]; then
  echo "usage: $BASENAME [option] [<object>] [extra-args]"
  echo "       $BASENAME --refresh"
  echo "       $BASENAME --<program-name> <item-name>"
  echo "       $BASENAME --open           <item-name>"
  echo "       $BASENAME --rifle          <item-name> -p 4"
  echo "       $BASENAME --koreader       <item-name>"
  echo "       $BASENAME --watch          <subfolder>"
  echo "       $BASENAME                  <item-name>"
  exit 1

elif [ "$ARG" == "--refresh" ]; then
  __update_filelist

elif [ ! -d "$DOCS" ]; then
  echo "missing $DOCS folder"
  exit 1
fi

# Check for filelist
if [ ! -e "$LIST" ]; then
  __update_filelist
  CHANGED="`date +%s`"
else
  CHANGED="`stat --printf='%Y\n' $LIST`"
fi

# Automatically refresh lists older than $MAX_AGE
#if ((($DATE - $CHANGED) >= $MAX_AGE)); then
#  __update_filelist
#fi

# Dispatch
#find $DOCS -iname "*$ARG*" | head -1 | while read entry; do xdg-open "$entry"; done
if [ "$ARG" == "--search" ]; then
  shift
  grep -i -- "$*" $LIST

elif [ "$ARG" == "--details" ]; then
  shift
  entry="`echo $* | sed -e 's/^@//'`"
  if grep -qiR -- "$entry" $BIBLIO; then
    grep -iR -- "$entry" $BIBLIO | cut -d : -f 1 | while read file; do
      echo "At $file:"
      echo ""
      # Thanks https://tex.stackexchange.com/questions/28506/how-to-use-a-command-line-tool-to-extract-a-bibtex-reference-that-contains-a-sea
      awk -v RS='\n@' "/${entry}/" $file
    done
  else
    find $DOCS -name ${1}.bib -exec cat {} \;
  fi

elif [ "$ARG" == "--watch" ]; then
  # Convert symlink to full path and start inotifywait loop
  folder="`readlink -f $DOCS`"
  subfolder="$EXTRA_ARG"
  echo "Watching changes at $folder/$subfolder..."

  #while inotifywait -e modify -e create -e move -e delete -r "$folder"; do
  #while inotifywait -e modify -e move -r "$folder"; do
  #while inotifywait -e modify -e move -r "$folder/$subfolder"; do
  #while inotifywait -e modify -e create -e move -e delete -r "$folder/$subfolder"; do
  #  $PROGRAM --refresh
  #done
  inotifier $PROGRAM --refresh

else
  shift 2
  EXTRA_ARGS="$*"

  # Allows for specifying a custom command to open the file
  if [ "${ARG:0:2}" == "--" ] && [ ! -z "$EXTRA_ARG" ]; then
    OPEN="${ARG:2}"
    ARG="$EXTRA_ARG"

    # Shortcuts for xdg-open
    if [ "$OPEN" == "open" ] || [ "$OPEN" == "read" ]; then
      OPEN="xdg-open"
    fi
  else
    OPEN="xdg-open"
    EXTRA_ARGS=""
  fi

  grep -i -- "$ARG" $LIST | while read entry; do
    echo "Opening $entry..."
    cd $DOCS && $OPEN $EXTRA_ARGS "$entry"
  done
fi