summaryrefslogtreecommitdiff
path: root/checkAllRepos
blob: c27d63bfb3ddc52ae9fc0ad2d1b5fe40891e4c82 (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
181
182
183
184
185
186
187
188
189
190
191
192
193
#!/bin/bash

usage() {
  >&2 echo 'Verwendung:'
  >&2 echo "${ich}"' [-d|--download URL] [-a|--alle] [-s|--suche wonach] [-i|--intensiv] [-x|--exakt] [-l|--leise]'
  exit 1
}

verpackung() {
  typ="$(file -bizL "$1")"
  case "${typ}" in
    "application/x-tar; charset=binary")
    ;;
    "application/x-tar; charset=binary compressed-encoding=application/x-xz; charset=binary")
      echo "-J"
    ;;
    "application/x-tar; charset=binary compressed-encoding=application/x-bzip2; charset=binary")
      echo "-j"
    ;;
    "application/x-tar; charset=binary compressed-encoding=application/x-gzip; charset=binary" | \
    "application/octet-stream; charset=binary compressed-encoding=application/x-gzip; charset=binary")
      echo "-z"
    ;;
    "ERROR: (null)" | \
    "inode/x-empty; charset=binary")
      return 1
    ;;
    *)
      ${leise} || >&2 echo "FEHLER: Unbekannter Verpackungstyp '${typ}'"
      return 1
    ;;
  esac
  return 0
}

ich="$(readlink -f "$0")"
pfad="$(dirname "${ich}")/.checkAllRepos"
[ ! -d "${pfad}" ] && mkdir -p "${pfad}"

eval set -- "$(
  getopt -o ad:ils:x \
    --long alle \
    --long download: \
    --long exakt \
    --long intensiv \
    --long leise \
    --long suche: \
    -n "$(basename "$0")" \
    -- "$@" \
    || echo "usage"
)"
args=("$@")

unset downloads suchen
alle=false
exakt=false
intensiv=false
leise=false
flag_l=''

while true
do
  case "$1" in
    -a|--alle)
      alle=true
    ;;
    -d|--download)
      shift
      if [ -n "$1" ]
      then
        downloads[${#downloads[@]}]="$1"
      fi
    ;;
    -i|--intensiv)
      intensiv=true
    ;;
    -l|--leise)
      leise=true
      flag_l="-l"
    ;;
    -s|--suche)
      shift
      if [ -n "$1" ]
      then
        suchen[${#suchen[@]}]="$1"
      fi
    ;;
    -x|--exakt)
      exakt=true
    ;;
    --)
      shift
      break
    ;;
    *)
      >&2 echo "FEHLER: Kenne Option '$1' doch nicht"
      exit 1
    ;;
  esac
  shift
done

if [ $# -ne 0 ]
then
  echo 'FEHLER: Zu viele Argumente'
  usage
fi

if ${alle}
then
  curl "https://wiki.archlinux.org/index.php/Unofficial_user_repositories" 2> /dev/null | \
    sed 's|#.*$||' | \
    sed '
      :begin;
        $!N;
        s@^\(<pre>.*\)\n\([^<]\+\)$@\1 \2@;
      tbegin;
      P;
      D
    ' | \
    grep "^<pre>" | \
    sed "s|^<pre> \[\([^]]*\)].*\sServer = \(\S\+\)\(\s.*\)\?\$|\1 \2/\1.db|" | \
    sed "s|^\(\S\+\)\s\+\(.*\)\$repo|\1 \2\1|; s|\$arch|x86_64|" | \
    sed "s|^\S\+\s\+||" | \
    parallel -j0 "${ich} ${flag_l} -d {}"
fi

for ((i=0;i<${#downloads[@]};i++))
do
  err=false
  ziel="${pfad}/${downloads[${i}]##*/}"
  wget -O "${ziel}" "${downloads[${i}]}" 2> /dev/null || err=true
  if ! ${err}
  then
    if ! verp=$(verpackung "${ziel}") || ! tar ${verp} -tf "${ziel}" &> /dev/null
    then
      err=true
    fi
  fi
  if ${err}
  then
    rm -f "${ziel}"
  else
    ${leise} || echo "${ziel##*/} erfolgreich heruntergeladen"
  fi
done

for ((i=0;i<${#suchen[@]};i++))
do
  suchName="${suchen[${i}]}"
  ${exakt} && suchName="^\(${suchName}\)-[^-]\+-[^-]\+\$"
  for db in ${pfad}/*.db
  do
    if tar $(verpackung "${db}") -tf "${db}" 2> /dev/null | \
      grep "/\$" |
      sed "s|/\$||" |
      grep -q "${suchName}"
    then
      echo "'$(basename "${db}" .db)' enthält ein Paket, welches auf '${suchen[${i}]}' passt:"
      tar $(verpackung "${db}") -tf "${db}" | \
        grep "/\$" |
        sed "s|/\$||" |
        grep "${suchName}" | \
        sed "s|^\(.*\)\$|  '\1'|"
    fi
    if ${intensiv}
    then
      for pkg in $(tar $(verpackung "${db}") -tf "${db}" --wildcards "*/desc" 2> /dev/null)
      do
        inhalt="$(
          tar $(verpackung "${db}") -Oxf "${db}" "${pkg}" 2> /dev/null | \
            sed '
              :begin;
                $!N;
                s@^\(%[^ %]*%.*\)\n\(.*\S.*\)$@\1 \2@;
              tbegin;
              P;
              D
            ' | \
          grep "^%DESC%" | \
            sed "s|^%DESC%\s*||"
        )"
        if echo "${inhalt}" | \
          grep -q "${suchen[${i}]}"
        then
          echo "Die Beschreibung von Paket '${pkg%/desc}' aus '$(basename "${db}" .db)' passt auf '${suchen[${i}]}':"
          echo "  ${inhalt}" | \
            grep --color=auto "${suchen[${i}]}"
        fi
      done
    fi
  done
done