File: pinot-cd.sh

package info (click to toggle)
pinot 1.05-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 5,848 kB
  • ctags: 3,572
  • sloc: cpp: 39,255; sh: 10,481; ansic: 3,049; makefile: 620; xml: 379
file content (131 lines) | stat: -rwxr-xr-x 2,992 bytes parent folder | download | duplicates (5)
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
#!/bin/bash
# A "tagged cd" implementation
# Based on an idea by C. Scott Ananian (http://wiki.laptop.org/go/Experiments_with_unordered_paths)
# Run this script with ". /path/to/pinot-cd.sh"

MISSING_PROGRAM=1
MISSING_INDEX=1
MATCHES_COUNT=0

# Check programs we need are available
checkprograms()
{
  WHICH_BN=`which basename`
  if [ $? != 0 ]; then
    echo "Couldn't find basename. Is the coreutils package installed ?"
    return
  fi
  WHICH_CAT=`which cat`
  if [ $? != 0 ]; then
    echo "Couldn't find cat. Is the coreutils package installed ?"
    return
  fi
  WHICH_MKTEMP=`which mktemp`
  if [ $? != 0 ]; then
    echo "Couldn't find mktemp. Is the coreutils package installed ?"
    return
  fi
  WHICH_GREP=`which grep`
  if [ $? != 0 ]; then
    echo "Couldn't find grep. Is the grep package installed ?"
    return
  fi
  WHICH_SED=`which sed`
  if [ $? != 0 ]; then
    echo "Couldn't find sed. Is the sed package installed ?"
    return
  fi
  WHICH_PS=`which pinot-search`
  if [ $? != 0 ]; then
    echo "Couldn't find pinot-search. Is the pinot package installed ?"
    return
  fi

  MISSING_PROGRAM=0
}

# Check the daemon's index exists
checkindex()
{
  if [ ! -d "$HOME/.pinot/daemon" ]; then
    echo "Configure Pinot to crawl and index directories first."
    return
  fi

  MISSING_INDEX=0
}

# Run the actual search and get matches
runsearch()
{
  local TEMP_FILE=`mktemp`

  pinot-search -l xapian "$HOME/.pinot/daemon" "type:x-directory/normal $1" 2>/dev/null | grep "file://" | sed -e "s/file:\/\/\(.*\)/\1/g" >$TEMP_FILE 2>/dev/null

  local RESULTS=`cat $TEMP_FILE`
  MATCHES_COUNT=`cat $TEMP_FILE|wc -l`

  if [ $MATCHES_COUNT -eq 1 ]; then
    echo $RESULTS
    cd "$RESULTS"
  elif [ $MATCHES_COUNT -gt 1 ] && [ $2 -eq 1 ]; then
    echo "Matches for \"$1\":"
    cat $TEMP_FILE
  elif [ $MATCHES_COUNT -eq 0 ] && [ $2 -eq 1 ]; then
    echo "No match"
  fi
  \rm -f $TEMP_FILE >/dev/null 2>&1
}

# Prepares the query string
preparequery()
{
  local ARGS=""
  local LAST_ARG=""

  for ARG in `echo $@` ;
  do
    ARGS="$ARGS path:$ARG"
    LAST_ARG="$ARG"
  done

  if [ -z "$LAST_ARG" ]; then
    return
  fi

  PATHS_ONLY_STRING=$ARGS
  # Assume the last path is the name of the final directory
  PATHS_AND_FILE_STRING=`echo $ARGS | sed -e "s/path:$LAST_ARG/file:$LAST_ARG/g"`

  runsearch "$PATHS_AND_FILE_STRING" 1
  if [ $MATCHES_COUNT -eq 0 ]; then
    # That assumption doesn't seem correct
    runsearch "$PATHS_ONLY_STRING" 1
  fi
}

# Warn if not sourced
BASE_NAME=`basename $0`
if [ "$BASE_NAME" == "pinot-cd.sh" ]; then
  echo "Run this script with . $0"
  exit 1
fi

checkprograms
checkindex

if [ $# == 0 ]; then
  echo "Usage: $0 PATH1 PATH2..."
elif [ "$MISSING_PROGRAM" == 0 ] || [ "$MISSING_INDEX" == 0 ]; then
  if [ $# -eq 1 ] && [ "$1" == "-" ] && [ ! -z "$OLDPWD" ]; then
    cd "$OLDPWD"
  else
    IS_FULL_PATH=`echo "$@" | grep "/"`
    if [ ! -z "$IS_FULL_PATH" ]; then
      cd $@
    else
      preparequery $@
    fi
  fi
fi