summarylogtreecommitdiffstats
path: root/locatecc
blob: ad1f5873fd1bee31888a194d16f19253b0d68d9b (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
#!/bin/bash

#launch locatecc instead this

search_str="$@"

  #simona 30/10/2020
  #updatedb /var/lib/mlocate/
  #plocate-build /var/lib/mlocate/mlocate.db /var/lib/mlocate/plocate.db
  locate-current() {
    [[ -r /bin/plocate ]] && exe="/bin/plocate" || exe="/bin/locate"
    [[ $1 == "-h" || $1 == "--help" ]] && { echo "Remember 'updatedb /var/lib/mlocate/' and 'plocate-build /var/lib/mlocate/mlocate.db /var/lib/mlocate/plocate.db'" ; return 0; }

    [[ ${#search_str} -le 3 ]] && { echo "Search string too short or missing"; return 1; }

    rm -f /tmp/locate-temp-file-1
    touch /tmp/locate-temp-file-1
    $exe --ignore-case "$search_str" > /tmp/locate-temp-file-1

    touch /tmp/locate-temp-file-2
    cat /tmp/locate-temp-file-1 | grep "^$(pwd)" --ignore-case --color=never >/tmp/locate-temp-file-2

    touch /tmp/locate-temp-file-3
    cat /tmp/locate-temp-file-2 | grep --ignore-case --color=never "/$search_str" >/tmp/locate-temp-file-3
    cat /tmp/locate-temp-file-2 | grep --ignore-case --color=never "/$search_str\.[A-Za-z0-9]*$" >>/tmp/locate-temp-file-3

    #cancellazione duplicati
    touch /tmp/locate-temp-file-4
    [[ -r /bin/perl ]] && perl -ne 'print unless $dup{$_}++;' /tmp/locate-temp-file-3 > /tmp/locate-temp-file-4 || sort -u /tmp/locate-temp-file-3 > /tmp/locate-temp-file-4

    #se ho una /dir1/dir1/file
    #seguito da /dir1/dir1/file/qualcosa1
    #/dir1/dir1/file/qualcosa2
    #la seconsa e terza ricorrenza le devo cancellare
    rm -f /tmp/locate-temp-file-5
    touch /tmp/locate-temp-file-5
    pr_line=""
    pr_line_len=0
    st_line="off"
    st_line_len=0
    while read -r line ; do
      line_len=${#line}
      if [[ $pr_line != "" ]] ; then
        if [[ $st_line == "off" ]] ; then
          if [[ ${line:0:$pr_line_len} == $pr_line ]] ; then
            st_line=$pr_line
            st_line_len=${#st_line}
          else
            echo $line >>/tmp/locate-temp-file-5
          fi
        else
          if [[ ${line:0:$st_line_len} != $st_line ]] ; then
            st_line="off"
            echo $line >>/tmp/locate-temp-file-5
          fi
        fi
      else
        echo $line >>/tmp/locate-temp-file-5
      fi

      #next loop
      pr_line=$line
      pr_line_len=${#pr_line}
    done < /tmp/locate-temp-file-4

    #per colorazione
    grep --color --no-messages --extended-regexp --ignore-case "$search_str" /tmp/locate-temp-file-5

    count=$(wc -l /tmp/locate-temp-file-5)
    count=$(echo $count | cut -f1 -d" ")
    echo "$count items found"

    rm -f /tmp/locate-temp-file-1
    rm -f /tmp/locate-temp-file-2
    rm -f /tmp/locate-temp-file-3
    rm -f /tmp/locate-temp-file-4
    rm -f /tmp/locate-temp-file-5

    [[ $count -gt 0 ]] && return 0 || return 1
  }


locate-current