aboutsummaryrefslogtreecommitdiff
path: root/trunk/lib/common.sh
blob: 91f715ffcaf9c4cc593cd6fcafbefa8db4cca946 (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
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
#!/bin/bash
#
# common.sh: common functions for simplepkg
# feedback: rhatto at riseup.net
#
#  common.sh 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.
#
#  common.sh 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
#

BASE_CONF="/etc/simplepkg"
CONF="$BASE_CONF/simplepkg.conf"
JAIL_LIST="$BASE_CONF/jailist"
SIMPLARET="simplaret"

# -----------------------------------------------
#               pkgtool functions
# -----------------------------------------------

function package_name {

  echo "`basename $1 .tgz`" | sed -e 's/-[^-]*-[^-]*-[^-]*$//'

}

# -----------------------------------------------
#             package info functions
# -----------------------------------------------

function package_version {

  # get VERSION from a package name
  local file pack version
  file="`basename $1`"
  pack="`package_name $1`"
  version="`echo $file | sed -e "s/^$pack-//" | cut -d "-" -f 1`"
  echo $version

}

function package_arch {

  # get ARCH from a package name
  local file pack arch
  file="`basename $1`"
  pack="`package_name $1`"
  arch="`echo $file | sed -e "s/^$pack-//" | cut -d "-" -f 2`"
  echo $arch

}

function package_build {

  # get BUILD from a package name
  local file pack build
  file="`basename $1 .tgz`"
  pack="`package_name $1`"
  build="`echo $file | sed -e "s/^$pack-//" | cut -d "-" -f 3`"
  echo $build

}

# -----------------------------------------------
#       package administrative functions
# -----------------------------------------------

function install_packages {

  local check installed unable_to_install root

  # check if is time to clean the local repository
  if [ "$SIMPLARET_CLEAN"  == "1" ]; then
    ARCH=$ARCH VERSION=$VERSION $SIMPLARET --purge
  elif [ ! -z "$SIMPLARET_PURGE_WEEKS" ] && [ "$SIMPLARET_PURGE_WEEKS" != "0" ]; then
    ARCH=$ARCH VERSION=$VERSION $SIMPLARET --purge -w $SIMPLARET_PURGE_WEEKS
  fi

  root="$JAIL_ROOT/$server"

  # now tries to install each package listed in the template
  for pack in `cat $TEMPLATE | grep -v -e "^#" | cut -d : -f 1 | awk '{ print $1 }'`; do

    # try to install the package
    ROOT=/$root ARCH=$ARCH VERSION=$VERSION $SIMPLARET --install $pack --skip-checks

    # check if the package was installed
    pack="`echo $pack | sed -e 's/\+/\\\+/'`"
    installed=`eval "ls /$root/var/log/packages/ | egrep -E '^$pack-[^-]+-[^-]+-[^-]+$'"`
    check=$?

    if [ ! -z "$installed" ] && [ "$check" == "0" ]; then
      # the package is installed
      if [ ! -z "$SIMPLARET_DELETE_DURING" ] && [ "$SIMPLARET_DELETE_DURING" != "0" ]; then
        SILENT=1 ARCH=$ARCH VERSION=$VERSION $SIMPLARET --purge
      fi
    else
      unable_to_install="$unable_to_install\n\t$pack"
    fi

  done

  # purge packages, if needed
  if [ "$SIMPLARET_DELETE_DOWN" == "1" ]; then
    ARCH=$ARCH VERSION=$VERSION $SIMPLARET --purge
  fi

  if [ ! -z "$unable_to_install" ]; then
    echo "mkjail was unable to install the following packages on $root:"
    echo -e "$unable_to_install"
  fi

}

function remove_packages {

  for pack in `cat $TEMPLATE | grep -v -e "^#" | cut -d : -f 1`; do
    ROOT=/$JAIL_ROOT/$server removepkg $pack
  done

}

# -----------------------------------------------
#             config file functions
# -----------------------------------------------

function eval_parameter {

  # usage: eval $1 parameter from $CONF
  # return the evaluated parameter if available or $2 $3 ... $n

  if grep -qe "^$1=" $CONF; then
    grep -e "^$1=" $CONF | cut -d = -f 2 | sed -e 's/"//g' -e "s/'//g" | sed -e 's/ *#.*$//'
  else
    shift
    echo $*
  fi

}

function set_constants {

    # Set common constants
    # on/off
    on=1
    off=0
    # yes/no
    yes=1
    no=0

}

function eval_boolean_parameter {

  # get a boolean parameter from the configuration

  local value

  # get the value
  value="`eval_parameter $1 $2`"

  convert_boolean $value

}

function convert_boolean {

  # force case insensitiveness
  local value="`echo $1 | tr '[:upper:]' '[:lower:]'`"

  # convert it to wheter 0 or 1
  if [ "$value" == "yes" ] || [ "$value" == "1" ] || [ "$value" == "on" ]; then
    echo 1
  else
    echo 0
  fi

}

function eval_config {

  # simplepkg config file evaluation
  # usage: eval_config <program-name> [-u]

  if [ -f "$CONF" ]; then

    DEFAULT_ARCH="`eval_parameter DEFAULT_ARCH $(default_arch)`"
    DEFAULT_VERSION="`eval_parameter DEFAULT_VERSION $(default_version)`"

    STORAGE="`eval_parameter STORAGE /var/simplaret/packages`"
    JAIL_ROOT="`eval_parameter JAIL_ROOT /vservers`"
    PATCHES_DIR="`eval_parameter PATCHES_DIR /var/simplaret/patches`"
    ROOT_PRIORITY="`eval_parameter ROOT_PRIORITY patches slackware extra testing pasture`"
    REPOS_PRIORITY="`eval_parameter REPOS_PRIORITY patches slackware extra testing pasture`"
    SIMPLARET_PURGE_WEEKS="`eval_parameter SIMPLARET_PURGE_WEEKS 0`"
    FTP_TOOL="`eval_parameter FTP_TOOL curl`"
    HTTP_TOOL="`eval_parameter HTTP_TOOL curl`"
    CONNECT_TIMEOUT="`eval_parameter CONNECT_TIMEOUT 0`"
    TEMPLATE_FOLDER="`eval_parameter TEMPLATE_BASE /etc/simplepkg/templates`"
    TEMPLATE_STORAGE_STYLE="`eval_parameter TEMPLATE_STORAGE_STYLE own-folder`"

    SIMPLARET_CLEAN="`eval_boolean_parameter SIMPLARET_CLEAN 1`"
    SIMPLARET_DELETE_DOWN="`eval_boolean_parameter SIMPLARET_DELETE_DOWN 1`"
    SIMPLARET_UPDATE="`eval_boolean_parameter SIMPLARET_UPDATE 0`"
    SIMPLARET_DELETE_DURING="`eval_boolean_parameter SIMPLARET_DELETE_DURING 0`"
    SIMPLARET_PURGE_PATCHES="`eval_boolean_parameter SIMPLARET_PURGE_PATCHES 1`"
    SIMPLARET_DOWNLOAD_FROM_NEXT_REPO="`eval_boolean_parameter SIMPLARET_DOWNLOAD_FROM_NEXT_REPO 1`"
    PASSIVE_FTP="`eval_boolean_parameter PASSIVE_FTP 0`"
    WARNING="`eval_boolean_parameter WARNING 0`"
    SIGNATURE_CHECKING="`eval_boolean_parameter SIGNATURE_CHECKING 0`"
    DEPENDENCY_CHECKING="`eval_boolean_parameter DEPENDENCY_CHECKING 1`"
    TEMPLATES_UNDER_SVN="`eval_boolean_parameter TEMPLATES_UNDER_SVN 0`"
    ADD_TO_JAIL_LIST="`eval_boolean_parameter ADD_TO_JAIL_LIST 1`"

    # Enabling this option (i.e, setting to "1" or "yes"), simplaret will
    # donwload even # already applied patches, a good option when you plan
    # to keep local copies of all needed patches for your system
    DOWNLOAD_EVEN_APPLIED_PATCHES="`eval_boolean_parameter DOWNLOAD_EVEN_APPLIED_PATCHES 0`"

    # Enabling this option, jail-upgrade will look at your
    # standard repositories for new packages; if it find a package
    # with different version of your current installed package and
    # also this package isnt in the packages folder, then the new
    # package is apllied; if in doubt, just say no or leave blank.
    CONSIDER_ALL_PACKAGES_AS_PATCHES="`eval_boolean_parameter CONSIDER_ALL_PACKAGES_AS_PATCHES 0`"

    # Enabling this option (i.e, setting to "1" or "yes"), simplaret will
    # store patches it finds on ROOT repositories on
    #
    #   $PATCHES_DIR/$ARCH/$VERSION/root-$repository_name.
    #
    # By default this option is turned off because it breaks the standard
    # way to store packages and can cause some confusion, but its an useful
    # feature if you like to see all patches apart from common packages and/or
    # stored in the same tree.
    STORE_ROOT_PATCHES_ON_PATCHES_DIR="`eval_boolean_parameter STORE_ROOT_PATCHES_ON_PATCHES_DIR 0`"

    # now we place "patches" on the top of ROOT_PRIORITY
    ROOT_PRIORITY="patches `echo $ROOT_PRIORITY | sed -e 's/patches//'`"


  else
    echo $1 error: config file $CONFIG not found
    exit 1
  fi

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

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

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

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

  if [ "$FTP_TOOL" != "wget" ] && [ "$FTP_TOOL" != "curl" ] && [ "$FTP_TOOL" != "ncftpget" ]; then
    echo "$1 configuration error: invalid value $FTP_TOOL for config parameter FTP_TOOL"
    echo "$1 assuming value \"curl\" for variable FTP_TOOL"
    FTP_TOOL="curl"
  fi

  if [ "$HTTP_TOOL" != "wget" ] && [ "$HTTP_TOOL" != "curl" ]; then
    echo "$1 configuration error: invalid value $HTTP_TOOL for config parameter HTTP_TOOL"
    echo "$1 assuming value \"curl\" for variable HTTP_TOOL"
    HTTP_TOOL="curl"
  fi

  if which $SIMPLARET &> /dev/null; then
    if [ "$SIMPLARET_UPDATE" == "1" ]; then
      if [ "$2" == "-u" ]; then
        ARCH=$ARCH VERSION=$VERSION $SIMPLARET --update
      fi
    fi
  else
    echo "$SIMPLARET not found, please install it before run $0"
  fi

  if [ "$TEMPLATE_STORAGE_STYLE" != "simplepkg-folder" ] && \
     [ "$TEMPLATE_STORAGE_STYLE" != "templates-folder" ] && \
     [ "$TEMPLATE_STORAGE_STYLE" != "own-folder" ]; then
    TEMPLATE_STORAGE_STYLE="own-folder"
  fi

  if [ ! -z "$ROOT" ]; then
    JAIL_ROOT="$ROOT"
  fi

}

# -----------------------------------------------
#           arch and version functions
# -----------------------------------------------

function default_version {

  # get version from /etc/slackware-version
  if [ -f "$1/etc/slackware-version" ]; then
    cat $1/etc/slackware-version | awk '{ print $2 }' | sed -e 's/.0$//'
  else
    aaa_base="`basename $(ls $1/var/log/packages/aaa_base-[0-9]* 2> /dev/null)`"
    pack="`basename $aaa_base | sed -e 's/-[^-]*-[^-]*-[^-]*$//'`"
    echo $aaa_base | sed -e "s/^$pack-//" | cut -d "-" -f 1 | sed -e 's/.0$//'
  fi

}

function default_arch {

  # get arch from /etc/slackware-version

  local arch
  arch="`cat $1/etc/slackware-version | awk '{ print $3 }' | sed -e 's/(//' -e 's/)//'`"

  if [ -z "$arch" ]; then
    echo `uname -m`
  else
    echo $arch
  fi

}

# -----------------------------------------------
#              template functions
# -----------------------------------------------

function search_default_template {

  if [ -e "$BASE_CONF/default.template" ]; then
    TEMPLATE_BASE="$BASE_CONF/default"
    echo $BASENAME using default template
  elif [ -e "$TEMPLATE_FOLDER/default.template" ]; then
    TEMPLATE_BASE="$TEMPLATE_FOLDER/default"
    echo $BASENAME: using default template
  elif [ -e "$TEMPLATE_FOLDER/default/default.template" ]; then
    TEMPLATE_BASE="$TEMPLATE_FOLDER/default/default"
    echo $BASENAME: using default template
  elif [ -e "$BASE_CONF/defaults/templates/default/default.template" ]; then
    TEMPLATE_BASE="$BASE_CONF/defaults/templates/default"
    echo $BASENAME using default template
  else
    echo $BASENAME: error: default template not found
    echo $BASENAME: please create a template using templatepkg
    return 1
  fi

}

function search_template {

  # determine the template to be used
  # usage: search-template <template-name> [--new | --update]

  #
  # templates can be stored either on
  #
  # - $BASE_CONF/template_name.template
  # - $TEMPLATE_FOLDER/template_name.template
  # - $TEMPLATE_FOLDER/template_name/template_name.template
  #
  # also, there's a folder for "oficial" simplepkg templates,
  # $BASE_CONF/defaults/templates/ and you can override any template
  # in the default folder by placing a template with the same name
  # in the template storage folders
  #

  if [ -f "$BASE_CONF/$1.template" ]; then
    TEMPLATE_BASE="$BASE_CONF/$1"
  elif [ -f "$TEMPLATE_FOLDER/$1.template" ]; then
    TEMPLATE_BASE="$TEMPLATE_FOLDER/$1"
  elif [ -f "$TEMPLATE_FOLDER/$1/$1.template" ]; then
    TEMPLATE_BASE="$TEMPLATE_FOLDER/$1/$1"
  elif [ -f "$BASE_CONF/defaults/templates/$1/$1.template" ] && \
       [ "$2" != "--update" ]; then
    TEMPLATE_BASE="$BASE_CONF/defaults/templates/$1/$1"
  else
    if [ "$2" == "--new" ]; then
      # we need to return the path for a new template
      if [ "$TEMPLATE_STORAGE_STYLE" == "simplepkg-folder" ]; then
        TEMPLATE_BASE="$BASE_CONF/$1"
      elif [ "$TEMPLATE_STORAGE_STYLE" == "templates-folder" ]; then
        TEMPLATE_BASE="$TEMPLATE_FOLDER/$1"
      else
        TEMPLATE_BASE="$TEMPLATE_FOLDER/$1/$1"
      fi
    elif [ "$2" == "--update" ]; then
      return 1
    else
      echo $BASENAME: template $1 not found
      search_default_template
    fi
  fi

}

# -----------------------------------------------
#           unix permission functions
# -----------------------------------------------

function numeric_perm {

  # get the numeric permission of a file
  # usage: numeric_perm <file-name>

  # just a bit of forbidden secrets

  if [ -a "$1" ]; then
    ls -lnd $1 | awk '{ print $1 }' | \
    sed -e 's/^.//' -e 's/r/4/g' -e 's/w/2/g' -e 's/x/1/g' \
        -e 's/-/0/g' -e 's/\(.\)\(.\)\(.\)/\1+\2+\3/g' |   \
    fold -w5 | bc -l | xargs | sed -e 's/ //g'
  fi

}

function get_owner {

  # get the numeric owner for a file
  # usage: get_owner <file>

  if [ -a "$1" ]; then
    ls -lnd $1 | awk '{ print $3 }'
  fi

}

function get_group {

  # get the numeric group for a file
  # usage: get_group <file>

  if [ -a "$1" ]; then
    ls -lnd $1 | awk '{ print $4 }'
  fi

}

# -----------------------------------------------
#              subversion functions
# -----------------------------------------------

function use_svn {

  # check if svn usage is enabled

  if [ "$TEMPLATES_UNDER_SVN" == "1" ] && \
     [ "$TEMPLATE_STORAGE_STYLE" == "own-folder" ]; then
     return 0
   else
     return 1
   fi

}

function svn_check {

  # check if a file is under svn
  # usage: svn_check <file>

  local cwd folder

  cwd="`pwd`"
  folder="`dirname $1`"

  if [ -d "$folder/.svn" ]; then

    cd $folder

    if [ "`svn status $1 | awk '{ print $1 }'`" == "?" ]; then
      return 1
    else
      return 0
    fi

    cd $cwd

  else

    return 1

  fi

}

# -----------------------------------------------
#           update jail functions
# -----------------------------------------------

function update_template_files {

  # update template files from svn
  # usage: update_template_files

  if use_svn && [ -d "$TEMPLATE_BASE.d/.svn" ]; then
    echo Checking out last template revision from svn...
    cd `dirname $TEMPLATE_BASE`
    svn update
  fi

}

function copy_template_files {

  # copy template files into jail
  # usage: copy_template_files <jail-path>

  if [ -d "$1" ]; then
    if [ -d "$TEMPLATE_BASE.d" ]; then
      echo "Copying template files to $1..."
      if use_svn && [ -d "$TEMPLATE_BASE.d/.svn" ]; then
        rsync -av --exclude=.svn $TEMPLATE_BASE.d/ $1/
      else
        rsync -av $TEMPLATE_BASE.d/ $1/
      fi
    fi
  fi

}

function set_jail_perms {

  # set template file permissions under a jail
  # usage: set_jail_perms <jail-path>

  if [ -s "$TEMPLATE_BASE.perms" ]; then
    echo Setting jail $1 permissions...
    cat $TEMPLATE_BASE.perms | while read entry; do
      file="`echo $entry | cut -d ";" -f 1`"
      if [ -e "$TEMPLATE_BASE.d/$file" ] && [ -a "$1/$file" ]; then
        owner="`echo $entry | cut -d ";" -f 2`"
        group="`echo $entry | cut -d ";" -f 3`"
        perms="`echo $entry | cut -d ";" -f 4`"
        chmod $perms $1/$file
        chown $owner:$group $1/$file
      fi
    done
  fi

}

# -----------------------------------------------
#           repository build functions
# -----------------------------------------------

function svn_add_meta {

  find . -name '*meta' -exec svn add {} 2> /dev/null \;

}

function gen_filelist {
  # generate FILELIST.TXT
  # usage: gen_filelist

  find . -type f -name '*.tgz' -follow -print | sort | tr '\n' '\0' | \
       xargs -0r ls -ldL --time-style=long-iso > FILELIST.TXT
  echo "Created new FILELIST.TXT"

}

function gen_patches_filelist {

  # generate FILE_LIST
  # usage: gen_patches_filelist <folder>

  if [ ! -z "$1" ] && [ -d "$1" ]; then

    cwd="`pwd`"
    cd $1
    find . -type f -name '*.tgz' -follow -print | sort | tr '\n' '\0' | \
         xargs -0r ls -ldL --time-style=long-iso > FILE_LIST
    cd $cwd

    if [ "$1" == "." ]; then
      echo "Created new FILE_LIST"
    else
      echo "Created new $1/FILE_LIST"
    fi

  fi

}

function gen_packages_txt {

  # generate PACKAGES.TXT
  # usage: gen_packages_txt <folder>

  if [ ! -z "$1" ] && [ -d "$1" ]; then

    cwd="`pwd`"
    cd $1

    echo '' > PACKAGES.TXT
    find . -type f -name '*.meta' -exec cat {} \; >> PACKAGES.TXT
    cat PACKAGES.TXT | gzip -9 -c - > PACKAGES.TXT.gz
    cd $cwd

    if [ "$1" == "." ]; then
      echo "Created new PACKAGES.TXT and PACKAGES.TXT.gz"
    else
      echo "Created new $1/PACKAGES.TXT and $1/PACKAGES.TXT.gz"
    fi

  fi

}

function gen_md5_checksums {

  # generate CHECKSUMS.md5
  # usage: gen_md5_checksums <folder>

  if [ -d "$1" ]; then

    cwd="`pwd`"
    cd $1

    echo '' > CHECKSUMS.md5
    find . -type f -name '*.tgz' -exec md5sum {} \; >> CHECKSUMS.md5
    cat CHECKSUMS.md5 | gzip -9 -c - > CHECKSUMS.md5.gz
    cd $cwd

    if [ "$1" == "." ]; then
      echo "Created new CHECKSUMS.md5 and CHECKSUMS.md5.gz"
    else
      echo "Created new $1/CHECKSUMS.md5 and $1/CHECKSUMS.md5.gz"
    fi

  fi

}

function gen_meta {

  # generate metafiles
  # usage: gen_meta <package-file>

  if [ ! -f $1 ]; then
    echo "File not found: $1"
    exit 1;
  else
    echo "Processing $1"
  fi

  if [ "`echo $1|grep -E '(.*{1,})\-(.*[\.\-].*[\.\-].*).tgz[ ]{0,}$'`" == "" ]; then
    return;
  fi

  NAME=$(echo $1|sed -re "s/(.*\/)(.*.tgz)$/\2/")
  LOCATION=$(echo $1|sed -re "s/(.*)\/(.*.tgz)$/\1/")
  SIZE=$( expr `gunzip -l $1 | tail -n 1|awk '{print $1}'` / 1024 )
  USIZE=$( expr `gunzip -l $1 | tail -n 1|awk '{print $2}'` / 1024 )
  REQUIRED=$(tar xzfO $1 install/slack-required 2>/dev/null|xargs -r -iZ echo -n "Z,"|sed -e "s/,$//")
  CONFLICTS=$(tar xzfO $1 install/slack-conflicts 2>/dev/null|xargs -r -iZ echo -n "Z,"|sed -e "s/,$//")
  SUGGESTS=$(tar xzfO $1 install/slack-suggests 2>/dev/null|xargs -r )
  METAFILE=${NAME%tgz}meta

  echo "PACKAGE NAME:  $NAME" > $LOCATION/$METAFILE

  if [ -n "$DL_URL" ]; then
    echo "PACKAGE MIRROR:  $DL_URL" >> $LOCATION/$METAFILE
  fi

  echo "PACKAGE LOCATION:  $LOCATION" >> $LOCATION/$METAFILE
  echo "PACKAGE SIZE (compressed):  $SIZE K" >> $LOCATION/$METAFILE
  echo "PACKAGE SIZE (uncompressed):  $USIZE K" >> $LOCATION/$METAFILE
  echo "PACKAGE REQUIRED:  $REQUIRED" >> $LOCATION/$METAFILE
  echo "PACKAGE CONFLICTS:  $CONFLICTS" >> $LOCATION/$METAFILE
  echo "PACKAGE SUGGESTS:  $SUGGESTS" >> $LOCATION/$METAFILE
  echo "PACKAGE DESCRIPTION:" >> $LOCATION/$METAFILE

  tar xzfO $1 install/slack-desc | grep -E '\w+\:'|grep -v '^#' >> $LOCATION/$METAFILE

  echo "" >> $LOCATION/$METAFILE

}

# -----------------------------------------------
#                 misc functions
# -----------------------------------------------

function slash {

  # remove additional slashes
  echo $* | sed -e 's/\/\+/\//g'

}

function color_select {

  # Select color mode: gray, color or none (*)
  # commun - Communication
  # messag - Commum messages
  # error - Error messages
  # normal   - turn off color
  case "$1" in
  'gray')
    commun="\033[37;1m"
    messag="\033[37;1m"
    error="\033[30;1m"
    alert="\033[37m"
    normal="\033[m"
    ;;
  'color')
    commun="\033[34;1m" # green
    messag="\033[32;1m" # blue
    error="\033[31;1m" # red
    alert="\033[33;1m" # yellow
    normal="\033[m"       # normal
    ;;
  *)
    commun=""
    messag=""
    error=""
    alert=""
    normal=""
    ;;
  esac

}


function eecho {

  # echoes a message
  # usage: eecho <message-type> <message>
  # message-type can be: commun, messag, error, normal

  echo -e "${1}${2}${normal}"

}