aboutsummaryrefslogtreecommitdiff
path: root/simplaret
blob: ed984021275a3c38656659e109963f4edeaccd7a (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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
#!/bin/bash
#
# simplaret v0.1.1: simplepkg's package grabber
# feedback: rhatto at riseup.net | gpl
#
#  Simplaret is free software; you can redistribute it and/or modify it under the
#  terms of the GNU General Public License as published by the Free Software
#  Foundation; either version 2 of the License, or any later version.
#
#  Simplaret is distributed in the hope that it will be useful, but WITHOUT ANY
#  WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
#  A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License along with
#  this program; if not, write to the Free Software Foundation, Inc., 59 Temple
#  Place - Suite 330, Boston, MA 02111-1307, USA
#
# config file simplepkg.conf:
#
#   DEFAULT_ARCH="i386"
#   DEFAULT=VERSION="10.2"
#   STORAGE="/storage/packages"
#   PASSIVE_FTP="1"
#
# config file repos.conf:
#
#   ROOT-i386="http://slack.sarava.org/packages/slackware"
#   REPOS-i386-10.2="slack.sarava.org%http://slack.sarava.org/packages/slackware/slackware-10.2/"
#   ROOT-x86_64="http://darkstar.ist.utl.pt/pub/slamd64/"
#   REPOS-x86_64-10.2="slack.sarava.org%http://slack.sarava.org/packages/slamd64/slamd64-10.2/"
#
# usage:
#
#   --update: baixa lista de pacotes de cada repositorio
#   --search: busca
#   --get [-a]: baixa um unico pacote
#   --get-pacthes: baixa os patches e faz o checksum / gpg
#   --purge [-w N]: remove o cache
#
# precedence:
#
# - checa antes o repositorio raiz, caso o pacote nao seja encontrado segue na ordem das
#   definicoes REPOS do arquivo de configuracao
#

SIMPLARET_CONF="/etc/simplepkg/simplepkg.conf"
REPOS_CONF="/etc/simplepkg/repos.conf"
COMMON="/usr/libexec/simplepkg/common.sh"

function simplaret_usage {

  echo "usage: [ARCH=otherarch] [VERSION=otherversion] `basename $0` <OPTION> package-name"
  echo -e "\t OPTIONS: --update, --search, --get, --get-patches"
  exit 1

}

function simplaret_eval_config {

  if grep -qe "^DEFAULT_ARCH=" $SIMPLARET_CONF; then
    DEFAULT_ARCH="`grep -e "^DEFAULT_ARCH=" $SIMPLARET_CONF | cut -d = -f 2 | sed -e 's/"//g' -e "s/'//g" | cut -d "#" -f 1`"
  else
    DEFAULT_ARCH="i386"
  fi

  if grep -qe "^DEFAULT_VERSION=" $SIMPLARET_CONF; then
    DEFAULT_VERSION="`grep -e "^DEFAULT_VERSION=" $SIMPLARET_CONF | cut -d = -f 2 | sed -e 's/"//g' -e "s/'//g" | cut -d "#" -f 1`"
  else
    DEFAULT_VERSION="10.2"
  fi

  if grep -qe "^STORAGE=" $SIMPLARET_CONF; then
    STORAGE="`grep -e "^STORAGE=" $SIMPLARET_CONF | cut -d = -f 2 | sed -e 's/"//g' -e "s/'//g" | cut -d "#" -f 1`"
  else
    STORAGE="/var/simplaret"
  fi

  if grep -qe "^PASSIVE_FTP=" $SIMPLARET_CONF; then
    PASSIVE_FTP="`grep -e "^PASSIVE_FTP=" $SIMPLARET_CONF | cut -d = -f 2 | sed -e 's/"//g' -e "s/'//g" | cut -d "#" -f 1`"
  else
    PASSIVE_FTP="0"
  fi

  if grep -qe "^WARNING=" $SIMPLARET_CONF; then
    WARNING="`grep -e "^WARNING=" $SIMPLARET_CONF | cut -d = -f 2 | sed -e 's/"//g' -e "s/'//g" | cut -d "#" -f 1`"
  else
    WARNING="0"
  fi

}

function simplaret_get_index {

  for file in FILELIST.TXT CHECKSUMS.md5 CHECKSUMS.md5.asc; do
    simplaret_download $1 $file $2
  done

}

function simplaret_backup_index {

  for file in FILELIST.TXT CHECKSUMS.md5 CHECKSUMS.md5.asc; do
    if [ -f "$1/$file" ]; then
      mv $1/$file $1/$file.old
    fi
  done

}

function simplaret_check_index {

  for file in FILELIST.TXT CHECKSUMS.md5 CHECKSUMS.md5.asc; do
    if [ ! -f "$1/$file" ] && [ -f "$1/$file" ]; then
      echo restoring old $file to $1...
      mv $1/$file.old $1/$file
     else
      rm -f $1/$file.old
    fi
  done

}

function simplaret_download {

  # downloads a file from a repo to a folder
  # usage: simplaret <repos_url> <package> <destination-folder>

  protocol="`echo $1 | cut -d : -f 1`"

  if [ ! -d "$3" ]; then
    mkdir -p $3
  fi

  if [ "$protocol" == "http" ]; then
    (cd $3 && wget $1/$2)
  elif [ "$protocol" == "ftp" ]; then
    if [ "$PASSIVE_FTP" == "1" ]; then
      passive_ftp="--passive-ftp"
    fi 
    (cd $3 && wget $passive_ftp $1/$2)
  elif [ "$protocol" == "file" ]; then
    url="`echo $1 | sed -e 's/file:\/\///'`"
    cp $url/$2 $3 2> /dev/null
  fi

}

function simplaret_repos_name {

  if echo $repos | grep -qe %; then
    repos_name="`echo $repos | cut -d % -f 1`"
    if [ -z "$repos_name" ]; then
      echo you should set a name for the repository $repos for arch $ARCH and version $VERSION
      echo please correct your $REPOS_CONF so this repository definition looks like this:
      echo REPOS-$ARCH-$VERSION=\"repository-name%repository-url\"
      exit 1
    fi
  else
    echo you should set a name for the repository $repos for arch $ARCH and version $VERSION
    echo please correct your $REPOS_CONF so this repository definition looks like this:
    echo REPOS-$ARCH-$VERSION=\"repository-name%$repos_url\"
    exit 1
  fi

}

function simplaret_repos_url {

  if echo $repos | grep -qe %; then
    repos_url="`echo $repos | cut -d % -f 2`"
    if [ -z "$repos_url" ]; then
      echo you should set a url for the repository $repos for arch $ARCH and version $VERSION
      echo please correct your $REPOS_CONF so this repository definition looks like this:
      echo REPOS-$ARCH-$VERSION=\"repository-name%repository-url\"
      exit 1
    fi
  else
    echo you should set a url for the repository $repos for arch $ARCH and version $VERSION
    echo please correct your $REPOS_CONF so this repository definition looks like this:
    echo REPOS-$ARCH-$VERSION=\"repository-name%repository-url\"
    exit 1
  fi

}

function simplaret_root_url {

  root_url="`grep -e "^ROOT-$ARCH=" $REPOS_CONF | cut -d = -f 2 | sed -e 's/"//g' -e "s/'//g" | cut -d "#" -f 1`"

}

function simplaret_update {

  echo updating package information for arch $ARCH and version $VERSION... 

  if [ ! -d "$STORAGE/$ARCH/$VERSION" ]; then
    mkdir -p $STORAGE/$ARCH/$VERSION
  else
    simplaret_backup_index $STORAGE/$ARCH/$VERSION
  fi

  simplaret_root_url
  simplaret_get_index "$root_url/$DISTRO_FOLDER/$extra_folder" $STORAGE/$ARCH/$VERSION
  simplaret_check_index $STORAGE/$ARCH/$VERSION

  for repos in `grep -e "^REPOS-$ARCH-$VERSION=" $REPOS_CONF | cut -d = -f 2 | sed -e 's/"//g' -e "s/'//g" | cut -d "#" -f 1`; do

    simplaret_repos_name
    simplaret_repos_url

    if [ ! -d "$STORAGE/$ARCH/$VERSION/$repos_name" ]; then
      mkdir -p $STORAGE/$ARCH/$VERSION/$repos_name
    else
      simplaret_backup_index $STORAGE/$ARCH/$VERSION/$repos_name
    fi

    simplaret_get_index $repos_url $STORAGE/$ARCH/$VERSION/$repos_name
    simplaret_check_index $STORAGE/$ARCH/$VERSION/$repos_name

    unset repos_name repos_url repos_protocol
 
  done

}

function simplaret_search {

  if [ ! -f "$STORAGE/$ARCH/$VERSION/FILELIST.TXT" ]; then
    if [ "$WARNING" != "0" ] || [ ! -z "$SILENT" ]; then
      echo warning: no file list for root repository on arch $ARCH version $VERSION, please do a simplaret --update
    fi
  else
    for file in `grep $1 $STORAGE/$ARCH/$VERSION/FILELIST.TXT | awk '{ print $8 }' | grep -e ".tgz$"`; do
      if echo $file | grep -q "patches"; then
        patch="(patch)"
      fi
      if [ "$2" == "-silent" ]; then
        echo `basename $file`
      else 
        echo root repos, arch: $ARCH, version: $VERSION: `basename $file` $patch
      fi
      unset patch
    done
  fi

  for repos in `grep -e "^REPOS-$ARCH-$VERSION=" $REPOS_CONF | cut -d = -f 2 | sed -e 's/"//g' -e "s/'//g" | cut -d "#" -f 1`; do

    simplaret_repos_name

    if [ ! -f "$STORAGE/$ARCH/$VERSION/$repos_name/FILELIST.TXT" ]; then
      if [ "$WARNING" != "0" ] || [ ! -z "$SILENT" ]; then
        echo warning: no file list for $repos_name repository on arch $ARCH version $VERSION, please do a simplaret --update
      fi
    else
      for file in `grep $1 $STORAGE/$ARCH/$VERSION/$repos_name/FILELIST.TXT | awk '{ print $8 }' | grep -e ".tgz$"`; do
        if echo $file | grep -q "patches"; then
          patch="(patch)"
        fi
        if [ "$2" == "-silent" ]; then
          echo `basename $file`
        else 
          echo $repos_name, arch: $ARCH, version: $VERSION: `basename $file` $patch
        fi
      done
    fi

  done

}

function simplaret_purge {

  if [ "$2" == "-w" ] && [ ! -z "$3" ]; then
    if [ -z "$SILENT" ]; then
      echo purging all packages older than $3 weeks for arch $ARCH and version $VERSION
    fi
    mtime="`echo "$3*7" | bc -l`"
    for file in `find $STORAGE/$ARCH/$VERSION/ -mtime +$mtime | bc -l 2> /dev/null`; do
      for extension in tgz asc; do
        if echo $file | grep -qe ".$extension$"; then
          rm $file
        fi
       done
    done
  else
    if [ -z "$SILENT" ]; then
      echo purging all packages for arch $ARCH and version $VERSION
    fi
    for file in `find $STORAGE/$ARCH/$VERSION/ 2> /dev/null`; do
      for extension in tgz asc; do
        if echo $file | grep -qe ".$extension$"; then
          rm $file
        fi
      done
    done
  fi

  if [ -z "$SILENT" ]; then
    echo done purging simplaret cache, please run simplaret --update to retrieve new package listings on this arch and version
  fi

}

function simplaret_get {

  # search for an already downloaded package
  for file in `find $STORAGE/$ARCH/$VERSION/ -name $1*tgz 2> /dev/null`; do
    candidate="`basename $file`"
    if [ "`package_name $candidate`" == "$1" ]; then
      echo package $candidate already downloaded and stored at `dirname $file`
      exit 0
    fi
  done

  # first search for the package in the root repository
  if [ ! -f "$STORAGE/$ARCH/$VERSION/FILELIST.TXT" ]; then
    if [ "$WARNING" != "0" ] || [ ! -z "$SILENT" ]; then
      echo warning: no file list for root repository on arch $ARCH version $VERSION, please do a simplaret --update
    fi
  else
    for file in `grep $1 $STORAGE/$ARCH/$VERSION/FILELIST.TXT | awk '{ print $8 }' | grep -e ".tgz$"`; do
      candidate="`basename $file`"
      if [ "`package_name $candidate`" == "$1" ]; then
        simplaret_root_url
        simplaret_download $root_url/$DISTRO_FOLDER/$extra_folder $file $STORAGE/$ARCH/$VERSION
        if [ ! -f "$STORAGE/$ARCH/$VERSION/$candidate" ]; then
          echo error downloading $candidate from root repository $root_url, please check your settings
          exit 1
        else
          echo package $candidate stored at $STORAGE/$ARCH/$VERSION
          simplaret_checksum $STORAGE/$ARCH/$VERSION/CHECKSUMS.md5 $STORAGE/$ARCH/$VERSION/$candidate
          exit 0
        fi
      fi
    done
  fi

  # then search for the package in contrib repos
  for repos in `grep -e "^REPOS-$ARCH-$VERSION=" $REPOS_CONF | cut -d = -f 2 | sed -e 's/"//g' -e "s/'//g" | cut -d "#" -f 1`; do
    simplaret_repos_name
    simplaret_repos_url
    if [ ! -f "$STORAGE/$ARCH/$VERSION/$repos_name/FILELIST.TXT" ]; then
      if [ "$WARNING" != "0" ] || [ ! -z "$SILENT" ]; then
        echo warning: no file list for $repos_name repository on arch $ARCH version $VERSION, please do a simplaret --update
      fi
    else
      for file in `grep $1 $STORAGE/$ARCH/$VERSION/$repos_name/FILELIST.TXT | awk '{ print $8 }' | grep -e ".tgz$"`; do
        candidate="`basename $file`"
        if [ "`package_name $candidate`" == "$1" ]; then
          simplaret_download $repos_url $file $STORAGE/$ARCH/$VERSION/$repos_name
          if [ ! -f "$STORAGE/$ARCH/$VERSION/$repos_name/$candidate" ]; then
            echo error downloading $candidate from repository $repos_url, please check your settings
            echo will try to fetch from the next repo, if exists...
          else
            echo package $candidate stored at $STORAGE/$ARCH/$VERSION/$repos_name
            simplaret_checksum $STORAGE/$ARCH/$VERSION/$repos_name/CHECKSUMS.md5 $STORAGE/$ARCH/$VERSION/$repos_name/$candidate
            exit 0
          fi
        fi
      done
    fi
  done

}

function simplaret_get_patches {

  if [ ! -d "$PATCHES_DIR/$ARCH/$VERSION" ]; then
    mkdir -p $PATCHES_DIR/$ARCH/$VERSION
  fi

  echo fetching patches for arch $ARCH and version $VERSION...

  # check for the downloaded patches
  for patch in `ls $PATCHES_DIR/$ARCH/$VERSION/*tgz 2> /dev/null`; do
    file="`find $STORAGE/$ARCH/$VERSION/ -name $patch 2> /dev/null`"
    if [ ! -f "$file" ]; then
      rm $patch
    else
      ln -sf $file $PATCHES_DIR/$ARCH/$VERSION/
    fi
  done

  # grab new patches
  for patch in `simplaret_search patches -silent`; do
    simplaret --get `package_name $patch`
    file="`find $STORAGE/$ARCH/$VERSION/ | grep "$patch" 2> /dev/null`"
    if [ ! -f "$file" ]; then
      echo error downloading package $file
      exit 1
    else
      ln -sf $file $PATCHES_DIR/$ARCH/$VERSION/
      echo patch stored as a symlink on $PATCHES_DIR/$ARCH/$VERSION/
    fi
  done

}

function simplaret_checksum {

  # simplaret_checksum <md5file> <file-name>

  if [ ! -f "$1" ] || [ ! -f "$2" ]; then
    echo checksum error: file not found
    exit 1
  fi

  pack="`basename $2`"
  checksum="`grep -e "$pack\$" $1 | awk '{ print $1 }'`"

  if [ -z "$checksum" ]; then
    echo file $2 not in checksum $1
    exit 1
  elif [ "$checksum" != "`md5sum $2 | awk '{ print $1 }'`" ]; then
    echo checksum mismatch for file `basename $file`
  else
    echo checksum ok for file `basename $file`
  fi

}

if [ -f "$COMMON" ]; then
  source $COMMON
else
  echo "error: file $COMMON found, check your `basename $0` installation"
  exit 1
fi

if [ -z "$1" ]; then
  simplaret_usage
  exit 1
else
  server="$1"
  eval_config `basename $0`
  simplaret_eval_config
fi

if [ ! -d "$STORAGE" ]; then
  mkdir -p $STORAGE
fi

if [ -z "$ARCH" ]; then
  ARCH="$DEFAULT_ARCH"
fi

if [ -z "$VERSION" ]; then
  VERSION="$DEFAULT_VERSION"
fi

if [ "$ARCH" == "i386" ]; then
  DISTRO="slackware"
  DISTRO_FOLDER="$DISTRO-$VERSION"
elif [ "$ARCH" == "x86_64" ]; then
  # extra_folder="tree"
  DISTRO="slamd64"
  DISTRO_FOLDER="$DISTRO-$VERSION"
elif [ "$ARCH" == "s390" ]; then
  DISTRO="slack390"
  DISTRO_FOLDER="$DISTRO-$VERSION"
elif [ "$ARCH" == "x86_uclibc" ]; then
  DISTRO="ucslack"
  DISTRO_FOLDER="$DISTRO-$VERSION"
elif [ "$ARCH" == "arm" ]; then
  DISTRO="armedslack"
  DISTRO_FOLDER="$DISTRO-$VERSION"
elif [ "$ARCH" == "powerpc" ]; then
  DISTRO="slackintosh"
  DISTRO_FOLDER="$VERSION"
elif [ "$ARCH" == "sparc" ]; then
  DISTRO="splack"
  DISTRO_FOLDER="tree-$VERSION"
fi

case $1 in
  --update) simplaret_update ;;
  --search) simplaret_search $2 ;;
  --get) simplaret_get $2 ;;
  --get-patches) simplaret_get_patches ;;
  --purge) simplaret_purge ;;
  *) simplaret_usage ;;
esac