aboutsummaryrefslogtreecommitdiff
path: root/trunk/lib/common.sh
blob: 92b00bb4a7132ff15772d05f90aa3171efd8a190 (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
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
#!/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 {

  # get the package name
  # in some places (like in /var/log/packages), the package name is appended with
  # an -upgrade information that should be striped
  basename $1 .tgz | sed -e 's/-upgraded-[0-9]*-[0-9]*-[0-9]*,[0-9]*:[0-9]*:[0-9]*$//' \
                         -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`"
  echo $file | sed -e "s/^$pack-//" | cut -d "-" -f 1

}

function package_arch {

  # get ARCH from a package name
  local file pack arch
  basename $1 | sed -e 's/.*\-\(.*\)\-.*.tgz$/\1/' -e 's/_slamd64$//g' -e 's/_sflack$//g'

}

function package_build {

  # get BUILD from a package name
  local file pack build
  basename $1 | sed -e 's/.*\-.*\-\(.*\).tgz$/\1/'

}

# -----------------------------------------------
#       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"
  mkdir -p $root/var/log/packages

  # 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`echo $pack | sed -e 's/\\\+/\+/'`"
    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$//'
  elif [ -f "$1/etc/slamd64-version" ]; then
    cat $1/etc/slamd64-version | awk '{ print $2 }' | sed -e 's/.0$//'
  elif [ -f "$1/etc/bluewhite64-version" ]; then
    cat $1/etc/bluewhite64-version | awk '{ print $2 }' | sed -e 's/.0$//'
  elif [ -f "$1/etc/sflack-version" ]; then
    cat $1/etc/sflack-version | awk '{ print $2 }' | sed -e 's/.0$//'
  else
    aaa_base="`basename $(ls $1/var/log/packages/aaa_base-[0-9]* 2> /dev/null)`"
    echo `package_version $aaa_base`
  fi

}

function default_arch {

  # get arch from /etc/slackware-version

  local arch

  if [ -f "$1/etc/slackware-version" ]; then
    arch="`cat $1/etc/slackware-version | awk '{ print $3 }' | sed -e 's/(//' -e 's/)//'`"
    if [ -z "$arch" ]; then
      arch="i386"
    fi
  elif [ -f "$1/etc/slamd64-version" ] || [ -f "$1/etc/bluewhite64-version" ] || [ -f "$1/etc/sflack-version" ]; then
    arch="x86_64"
  else
    aaa_base="`basename $(ls $1/var/log/packages/aaa_base-[0-9]* 2> /dev/null)`"
    echo `package_arch $aaa_base`
  fi

  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 update_jail_packages {

  # update jail packages according the template
  # usage: update_jail_packages <jail-path>

  # check if installed packages are listed in the template
  for pack in `ls -1 $1/var/log/packages/`; do
    pack=`package_name $pack`
    if ! `grep -v -e "^#" $TEMPLATE_BASE.template | cut -d : -f 1 | awk '{ print $1 }' | grep -q -e "^$pack\$"`; then
      ROOT=$1 removepkg $pack
    fi
  done

  # check if each package from the template is installed
  grep -v -e "^#" $TEMPLATE_BASE.template | cut -d : -f 1 | awk '{ print $1 }' | while read pack; do

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

    if [ -z "$installed" ] || [ "$check" != "0" ]; then
      # the package isn't installed
      ROOT=$1 simplaret install $pack
    fi
  done

}

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

}

# -----------------------------------------------
#                 Error functions
# -----------------------------------------------

function error_codes {

  # Slackbuilds error codes ** not change **
  ERROR_WGET=31       # wget error
  ERROR_MAKE=32       # make source error
  ERROR_INSTALL=33    # make install error
  ERROR_MD5=34        # md5sum error
  ERROR_CONF=35       # ./configure error
  ERROR_HELP=36       # dasable
  ERROR_TAR=37        # tar error
  ERROR_MKPKG=38      # makepkg error
  ERROR_GPG=39        # gpg check error
  ERROR_PATCH=40      # patch error
  ERROR_VCS=41        # cvs error
  ERROR_MKDIR=42      # make directory error
  # Slackbuilds error codes ** not change **

  # Commum error codes
  ERROR_FILE_NOTFOUND=100                   # file not found
  ERROR_NOT_NUMBER=101                      # argument is not a number
  ERROR_PAR_NUMBER=102                      # incorrect number of parameters
  ERROR_COMMON_NOT_FOUND=103                # common.sh not found

  # Createpkg error codes
  ERROR_CREATEPKG_INSTALLPKG=200            # installpkg error
  ERROR_CREATEPKG_DEPENDENCY=201            # dependency error
  ERROR_CREATEPKG_SLACKBUILD_NOTFOUND=202   # Script or package not found

  # Mkbuild error codes
  ERROR_MKBUILD_FILE_NOT_FOUND=500
  ERROR_MKBUILD_CONSTRUCTION=501
  ERROR_MKBUILD_PROGRAM=502
  ERROR_MKBUILD_INPUT_PAR=503
  ERROR_MKBUILD_SVN=504

  # Mkpatch error codes
  ERROR_MKPATCH=600
}

function handle_error {

  # This function deals with internal createpkg errors
  # and also with non-zero exit codes from slackbuilds
  # Input:    $1 - error code
  # Output:   Error mensage
  #
  # check slackbuild exit status are:
  #
  # ERROR_WGET=31;      ERROR_MAKE=32;      ERROR_INSTALL=33
  # ERROR_MD5=34;       ERROR_CONF=35;      ERROR_HELP=36
  # ERROR_TAR=37;       ERROR_MKPKG=38      ERROR_GPG=39
  # ERROR_PATCH=40;     ERROR_VCS=41;       ERROR_MKDIR=42
  #

  # we don't want to process when exit status = 0
  [ "$1" == "0" ] && return

  # Exit codes
  case $1 in
    #
    # Slackbuilds errors
    $ERROR_WGET)
      eecho $error "$BASENAME: error downloading source/package for $2" ;;
    $ERROR_MAKE)
      eecho $error "$BASENAME: error compiling $2 source code" ;;
    $ERROR_INSTALL)
      eecho $error "$BASENAME: error installing $2" ;;
    $ERROR_MD5)
      eecho $error "$BASENAME: error on source code integrity check for $2" ;;
    $ERROR_CONF)
      eecho $error "$BASENAME: error configuring the source code for $2" ;;
    $ERROR_HELP)
      exit 0 ;; # its supposed to never happen here :P
    $ERROR_TAR)
      eecho $error "$BASENAME: error decompressing source code for $2" ;;
    $ERROR_MKPKG)
      eecho $error "$BASENAME: error creating package $2" ;;
    $ERROR_GPG)
      eecho $error "$BASENAME: error verifying GPG signature the source code for $2" ;;
    $ERROR_PATCH)
      eecho $error "$BASENAME: error patching the source code for $2" ;;
    $ERROR_VCS)
      eecho $error "$BASENAME: error downloading $2 source from version control system" ;;
    $ERROR_MKDIR)
      eecho $error "$BASENAME: make directory $2 error, aborting" ;;

    #
    # General errors
    $ERROR_FILE_NOTFOUND)
      eecho $error "$BASENAME: file $2 not found!" ;;
    $ERROR_NOT_NUMBER)
      eecho $error "$BASENAME: $2 need a number argument" ;;
    $ERROR_PAR_NUMBER)
      eecho $error "$BASENAME: incorrect number of parameters" ;;
    #$ERROR_COMMON_NOT_FOUND)
    #  eecho $error "$BASENAME: file $COMMON_SH not found. Check your $BASENAME installation" ;;

    #
    # Createpkg errors
    $ERROR_CREATEPKG_INSTALLPKG)
      eecho $error "$BASENAME: install package $2 error, aborting" ;;
    $ERROR_CREATEPKG_DEPENDENCY)
      eecho $error "$BASENAME: dependency solve error, aborting" ;;
    $ERROR_CREATEPKG_SLACKBUILD_NOTFOUND)
      eecho $error "$BASENAME: SlackBuild or package not found" ;;

    #
    # Mkbuild errors
    $ERROR_MKBUILD_CONSTRUCTION)
      eecho $error "$BASENAME: Construction error in $2 variable." ;;
    $ERROR_MKBUILD_PROGRAM)
      eecho $error "$BASENAME: Program logical error." ;;
    $ERROR_MKBUILD_INPUT_PAR)
      eecho $error "$BASENAME: Input parameter $2 error. See \"mkbuild --help\"." ;;
    $ERROR_MKPATCH)
      eecho $error "$BASENAME: Mkpatch error. Check .mkbuild file." ;;
    $ERROR_MKBUILD_VCS)
      eecho $error "$BASENAME: VCS or empty URL. Disable this sections in .mkbuild file:\n - download_source;\n - md5sum_download_and_check_0;\n - md5sum_download_and_check_1;\n - gpg_signature_check\n - untar_source"
      ;;
    #
    # Others errors
    *)
      eecho $error "$BASENAME: unknown error or user interrupt" ;;
  esac

  is_number $1 && exit $1 || exit 1

}

# -----------------------------------------------
#                 svn functions
# -----------------------------------------------

function build_repo {

  # Checkout a new slackbuild working copy
  # input: $1 - svn directory name
  #        $2 - svn address
  [ $# -ne 2 ] && exit 5
  SVN_BASEDIR="`dirname $1`"
  mkdir -p $SVN_BASEDIR || exit 4
  cd $SVN_BASEDIR
  svn checkout $2
  cd $1

}

function check_repo {

  # Verify if repository exist
  # input: $1 - svn directory name
  #        $2 - svn address
  [ $# -ne 2 ] && exit 5
  [ ! -d "$1" ] && build_repo $1 $2

}

function sync_repo {

  # Synchronize repository
  # input: $1 - svn directory name
  #        $2 - svn address
  [ $# -ne 2 ] && exit 5
  cd $1
  svn update || build_repo $1 $2

}

# -----------------------------------------------
#                 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')
    # colors
    normal="\033[m"
    dark_gray="\033[30;1m"
    gray="\033[37m"
    white="\033[37;1m"

    red=$dark_gray
    blue=$dark_gray
    green=$gray
    yellow=$gray

    # Actions
    commun=$white
    messag=$white
    error=$dark_gray
    alert=$gray
    ;;
  'color')
    # colors
    normal="\033[m"
    dark_gray="\033[30;1m"
    gray="\033[37m"
    white="\033[37;1m"

    red="\033[31;1m"
    blue="\033[34;1m"
    green="\033[32;1m"
    yellow="\033[33;1m"

    # Actions
    commun=$green
    messag=$blue
    error=$red
    alert=$yellow
    ;;
  *)
    commun=""
    messag=""
    error=""
    alert=""
    ;;
  esac

}

function eecho {

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

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

}

function is_number {

    # Check if $1 is a number
    local -i int
    if [ $# -eq 0 ]; then
        return 1
    else
        (let int=$1)  2>/dev/null
        return $? # Exit status of the let thread
    fi

}