aboutsummaryrefslogtreecommitdiff
path: root/show
blob: 219570252456aeeb6294b3ec523069fc1758f6f1 (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
#!/bin/bash
#
# Fast way to browse documents stored in an archive.
#

# Parameters
PROGRAM="$0"
BASENAME="`basename $0`"
DOCS="$HOME/data/doc"
FILELIST=".filelist"
LIST="$DOCS/$FILELIST"
DOC_PATTERN=".*\.(txt|doc|docx|rtf|pdf|sxc|csv|odt|odf|ods|xls|xlsx|ppt|epub|mobi|djvu|lit)"
FIND_OPTS="-not -path '*.git*' -regextype posix-egrep -iregex"
ITEM="$1"
DATE="`date +%s`"
MAX_AGE="86400"

# Update the filelist
function __update_filelist {
  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 . $FIND_OPTS "$DOC_PATTERN" > $LIST

  # Stage
  git add $LIST
}

# Check
if [ -z "$ITEM" ]; then
  echo "usage: $BASENAME <item-name>"
  exit 1
elif [ "$ITEM" == "--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 "*$ITEM*" | head -1 | while read entry; do xdg-open "$entry"; done
if [ "$ITEM" == "--search" ]; then
  shift
  grep -i -- "$*" $LIST
elif [ "$ITEM" == "--watch" ]; then
  (
    cd $DOCS

    while inotifywait -e modify -e create -e move -e delete .; do
      $PROGRAM --refresh
    done
  )
else
  grep -i -- "$ITEM" $LIST | while read entry; do
    echo "Opening $entry..."
    cd $DOCS && xdg-open "$entry"
  done
fi