aboutsummaryrefslogtreecommitdiff
path: root/trunk/lib/common.sh
blob: d583a92c19f5aa9a11124e9ad2602ef8c662dad4 (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
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
#!/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
#
# $Rev$ - $Author$
#

BASE_CONF="/etc/simplepkg"
CONF="$BASE_CONF/simplepkg.conf"
HOME_CONF="$HOME/.simplepkg/simplepkg.conf"
DEFAULT_CONF="$BASE_CONF/defaults/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="`check_installed $pack $root`"
    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 {

  local pack

  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 $HOME_CONF, $CONF or $DEFAULT_CONF
  # return the evaluated parameter if available or $2 $3 ... $n

  if [ -e "$HOME_CONF" ] && grep -qe "^$1=" $HOME_CONF; then
    grep -e "^$1=" $HOME_CONF | tail -n 1 | cut -d = -f 2 | sed -e 's/"//g' -e "s/'//g" | sed -e 's/ *#.*$//'
  elif [ -e "$CONF" ] && grep -qe "^$1=" $CONF; then
    grep -e "^$1=" $CONF | tail -n 1 | cut -d = -f 2 | sed -e 's/"//g' -e "s/'//g" | sed -e 's/ *#.*$//'
  elif [ -e "$DEFAULT_CONF" ] && grep -qe "^$1=" $DEFAULT_CONF; then
    grep -e "^$1=" $DEFAULT_CONF | tail -n 1 | 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 "$DEFAULT_CONF" ]; then

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

    TMP="`eval_parameter TMP /tmp`"
    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 compact`"

    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 $DEFAULT_CONF 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" ] && \
     [ "$TEMPLATE_STORAGE_STYLE" != "compact" ]; then
    TEMPLATE_STORAGE_STYLE="compact"
  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="i486"
    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

}

function default_distro {

  # get distro name from /etc/slackware-version
  if [ "`default_arch`" == "x86_64" ]; then
    if [ -f "$1/etc/slamd64-version" ]; then
      echo slamd64
    elif [ -f "$1/etc/bluewhite64-version" ]; then
      echo bluewhite64
    elif [ -f "$1/etc/sflack-version" ]; then
      echo sflack
    else
      echo slamd64
    fi
  else
    if [ -f "$1/etc/slackware-version" ]; then
      cat $1/etc/slackware-version | awk '{ print $1 }' | tr '[[:upper:]]' '[[:lower:]]'
    else
      echo slackware
    fi
  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 "$TEMPLATE_FOLDER/default/packages" ]; then
    TEMPLATE_BASE="$TEMPLATE_FOLDER/default"
    echo $BASENAME: using default template
  elif [ -e "$BASE_CONF/defaults/templates/default/default.template" ]; then
    TEMPLATE_BASE="$BASE_CONF/defaults/templates/default/default"
    echo $BASENAME using default template
  elif [ -e "$BASE_CONF/defaults/templates/default/packages" ]; 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 "$TEMPLATE_FOLDER/$1/packages" ]; then
    TEMPLATE_BASE="$TEMPLATE_FOLDER/$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"
      elif [ "$TEMPLATE_STORAGE_STYLE" == "own-folder" ]; then
        TEMPLATE_BASE="$TEMPLATE_FOLDER/$1/$1"
      elif [ "$TEMPLATE_STORAGE_STYLE" == "compact" ]; then
        TEMPLATE_BASE="$TEMPLATE_FOLDER/$1"
      else
        TEMPLATE_BASE="$TEMPLATE_FOLDER/$1"
      fi
    elif [ "$2" == "--update" ]; then
      return 1
    else
      echo $BASENAME: template $1 not found
      search_default_template
    fi
  fi

}

function template_packages {

  if [ "$TEMPLATE_STORAGE_STYLE" == "compact" ]; then
    echo $TEMPLATE_BASE/packages
  else
    echo $TEMPLATE_BASE.template
  fi

}

function template_perms {

  if [ "$TEMPLATE_STORAGE_STYLE" == "compact" ]; then
    echo $TEMPLATE_BASE/perms
  else
    echo $TEMPLATE_BASE.perms
  fi

}

function template_files {

  if [ "$TEMPLATE_STORAGE_STYLE" == "compact" ]; then
    echo $TEMPLATE_BASE/files
  else
    echo $TEMPLATE_BASE.d
  fi

}

function template_scripts {

  if [ "$TEMPLATE_STORAGE_STYLE" == "compact" ]; then
    echo $TEMPLATE_BASE/scripts
  else
    echo $TEMPLATE_BASE.s
  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

}

function is_writable_folder {

  # check if a folder is writable
  # usage: is_writable_folder <folder>

  local tmpfile folder="$1"

  if mkdir -p $folder &> /dev/null && tmpfile=`mktemp $folder/is_writable_folder.XXXXXX`; then     
    rm -f $tmpfile
    return 0
  else
    return 1
  fi

}

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

function templates_under_svn {

  # check if svn usage is enabled

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

}

function svn_check {

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

  local folder file

  folder="`dirname $1`"
  file="`basename $1`"

  if [ ! -e "$folder/$file" ]; then

    return 1

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

    return 0

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

    (
      cd $folder

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

  else
    return 1
  fi

}

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

  local folder url
  folder="$1"
  url="$1"

  mkdir -p $folder
  cd $folder
  su_svn update || build_repo $folder $url

}

function svn_add {

  if [ -e "$1" ] && [ -d "`dirname $1`/.svn" ] && ! svn_check $1; then
    su_svn add $1
  fi

}

function svn_del {

  local file folder

  file="$1"
  folder="`dirname $file`"

  if [ -e "$file" ] && [ -d "$folder/.svn" ] && svn_check $file; then
    chown_svn $file && chgrp_svn $file
    ( cd $folder && su_svn del --force `basename $file` )
  else
    ( cd $folder && rm -rf `basename $file` )
  fi

}

function svn_copy {

  # svn add file
  # usage: svn_copy <orig> <dest>

  [ $# -ne 2 ] && handle_error $ERROR_PAR_NUMBER

  if [ -e "$1" ]; then

    local orig file dest

    orig="`dirname $1`"
    file="`basename $1`"
    dest="$2"

    if [ -d "$dest" ]; then
      dest="$dest/$file"
    fi

    # copy file
    if ! is_the_same $orig $(dirname $dest); then
      cp $orig/$file $dest
    fi

    # add file to the revision system
    if [ -d "`dirname $dest`/.svn" ]; then
      chown_svn $dest && chgrp_svn $dest
      ( cd `dirname $dest` && svn_add `basename $dest` )
    fi

  fi

}

function svn_mkdir {

  # svn make directory
  # usage: svn_mkdir <folder>

  [ $# -ne 1 ] && handle_error $ERROR_PAR_NUMBER

  DIR_LIST=`echo $1 | tr '/' ' '`

  DIR=""
  for i in $DIR_LIST; do
    DIR=$DIR/$i
    if [ ! -e ${DIR:1} ]; then
      su_svn mkdir ${DIR:1}
    elif [ -d "${DIR:1}" ] && [ ! -d "${DIR:1}/.svn" ]; then
      su_svn add ${DIR:1}      
    fi
  done

}

function su_svn {

  # execute svn using a different user
  if [ ! -z "$SVN_USER" ]; then
    if [ "`whoami`" != "$SVN_USER" ]; then
      su $SVN_USER -c "svn $*"
    else
      svn $*
    fi
  else
    svn $*
  fi

}

function chown_svn {

  # set svn folder ownership
  if [ ! -z "$SVN_USER" ] && [ ! -e "$1" ]; then
    chown -R $SVN_USER $1
  fi

}

function chgrp_svn {

  # set svn folder group
  if [ ! -z "$SVN_GROUP" ] && [ ! -e "$1" ]; then
    chgrp -R $SVN_GROUP $1
  fi

}

function svn_remove_empty_folders {

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

  local folder

  for folder in `find $1 -type d -print | grep -v "/\.svn" | sort -r`; do
    if [ "`ls -A -1 $folder | grep -v -e '^\.svn' | wc -l`" -eq "0" ]; then
      svn_del $folder
    fi
  done

}

function commit_changes {

  # usage: commit_changes <path>

  local repos="$1" tmpfile
  shift

  if [ -d "$repos/.svn" ]; then
    cwd="`pwd`"
    chown_svn $repos && chgrp_svn $repos
    cd $repos
    if [ ! -z "$1" ]; then
      if tmpfile=`mktemp $TMP/simplepkg_commit.XXXXXX`; then
        echo $* > $tmpfile
        chmod +r $tmpfile
        su_svn commit -F $tmpfile
        rm -f $tmpfile
      else
        su_svn commit
      fi
    else
      su_svn commit
    fi
    cd $cwd
  fi

}

function check_svn_repo {

  # check a svn repository URL
  # usage: set_svn_repo <repository>

  if [ ! -z "$1" ]; then
    if echo $1 | grep -q -v -e "^svn://"; then
      if echo $1 | grep -q -v -e "svn+.\+://"; then
        if echo $1 | grep -q -v -e "^file://"; then
          echo $BASENAME: invalid repository URL $1
          return 1
        fi
      fi
    fi
  else
    echo $BASENAME: no repository defined
    return 1
  fi

}

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

function update_template_files {

  # update template files from svn
  # usage: update_template_files

  if templates_under_svn && [ -d "`template_files`/.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_packages) | 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_packages` | cut -d : -f 1 | awk '{ print $1 }' | while read pack; do

    # check if the package is installed
    pack="`echo $pack | sed -e 's/\+/\\\+/'`"
    installed="`check_installed $pack $1`"
    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_files`" ]; then
      echo "Copying template files to $1..."
      if templates_under_svn && [ -d "`template_files`/.svn" ]; then
        rsync -av --exclude=.svn `template_files`/ $1/
      else
        rsync -av `template_files`/ $1/
      fi
    fi
  fi

}

function set_jail_perms {

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

  if [ -s "`template_perms`" ]; then
    echo Setting jail $1 permissions...
    cat `template_perms` | while read entry; do
      file="`echo $entry | cut -d ";" -f 1`"
      if [ -e "`template_files`/$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"

  svn_add FILELIST.TXT

}

function gen_patches_filelist {

  # generate FILE_LIST
  # usage: gen_patches_filelist <folder>

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

    local folder
    folder="$1"

    (

      cd $folder

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

      svn_add FILE_LIST

    )

    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

    local folder
    folder="$1"

    (

      cd $folder

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

      svn_add PACKAGES.TXT
      svn_add PACKAGES.TXT.gz

    )

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

  fi

}

function gen_md5_checksums {

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

  if [ -d "$1" ]; then

    local folder
    folder="$1"

    (

      cd $folder

      echo 'MD5 digest for files in this directory.' > CHECKSUMS.md5
      echo '' >> CHECKSUMS.md5
      find . -type f -name '*.tgz' -exec md5sum {} \; >> CHECKSUMS.md5
      cat CHECKSUMS.md5 | gzip -9 -c - > CHECKSUMS.md5.gz

      svn_add CHECKSUMS.md5
      svn_add CHECKSUMS.md5.gz

    )

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

  fi

}

function update_md5_checksum {

  # update CHECKSUMS.md5
  # usage: update_md5_checksums <folder> <file>

  if [ -z "$2" ] || [ ! -d "$1" ] || [ ! -f "$1/$2" ]; then
    return 1
  else
    local file folder
    file="$2"
    folder="$1"
  fi

  (

    cd $folder

    if [ ! -f CHECKSUMS.md5 ]; then
      gen_md5_checksums .
    else
      # remove the old entry and add a new one
      sed -i "/ \.*\/*$(regexp_slash $file)$/d" CHECKSUMS.md5
      file="`echo $file | sed -e 's/\.*\/*//'`" # remove additional ./
      md5sum ./$file >> CHECKSUMS.md5
    fi

    cat CHECKSUMS.md5 | gzip -9 -c - > CHECKSUMS.md5.gz

    echo "Updated CHECKSUMS.md5 at $folder"

    svn_add CHECKSUMS.md5
    svn_add CHECKSUMS.md5.gz

  )

}

function gen_meta {

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

  if [ ! -f $1 ]; then
    return 1
  else
    file="$1"
  fi

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

  NAME=$(echo $file | sed -re "s/(.*\/)(.*.tgz)$/\2/")
  LOCATION=$(echo $file | sed -re "s/(.*)\/(.*.tgz)$/\1/")
  SIZE=$( expr `gunzip -l $file | tail -n 1 | awk '{ print $1 }'` / 1024 )
  USIZE=$( expr `gunzip -l $file | tail -n 1 | awk '{ print $2 }'` / 1024 )
  REQUIRED=$(tar xzfO $file install/slack-required 2>/dev/null | grep -v -e "^#" | xargs -r -iZ echo -n "Z," | sed -e "s/,$//")
  CONFLICTS=$(tar xzfO $file install/slack-conflicts 2>/dev/null | grep -v -e "^#" | xargs -r -iZ echo -n "Z," | sed -e "s/,$//")
  SUGGESTS=$(tar xzfO $file install/slack-suggests 2>/dev/null | grep -v -e "^#" | 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 $file install/slack-desc | grep -E '\w+\:' | grep -v '^#' >> $LOCATION/$METAFILE

  echo "" >> $LOCATION/$METAFILE
  echo "Created metafile for `basename $file`"

  ( cd `dirname $file` && svn_add `basename $file .tgz`.meta )

}

function repo_gpg_key {

  # usage: repo_gpg_key <folder>

  local folder="$1" tmp_gpg_folder

  if [ -z "$SIGN_KEYID" ]; then
    echo "GPG-KEY checking failed, no sign key id set."
    return 1
  fi

  if [ $SIGN_PACKAGES -eq $on ]; then
    if [ -f "$folder/GPG-KEY" ]; then
      if ! gpg --with-colons < $folder/GPG-KEY | cut -d : -f 5 | grep -q -e "$SIGN_KEYID$"; then
        echo "Adding OpenPGP key id $SIGN_KEYID to $folder/GPG-KEY file..."

        tmp_gpg_folder="`mktemp -d $TMP/tmp_gpg_folder.XXXXXX`"
        tmp_gpg_pubkey="`mktemp -d $TMP/tmp_gpg_pubkey.XXXXXX`"

        if [ ! -z "$SIGN_PACKAGES_USER" ]; then
          chown $SIGN_PACKAGES_USER $tmp_gpg_folder
          chown $SIGN_PACKAGES_USER $tmp_gpg_pubkey

          # merge pubkey information in a temporary keyring
          su $SIGN_PACKAGES_USER -c "gpg --export --armor $SIGN_KEYID > $tmp_gpg_pubkey/pubkey.asc"
          su $SIGN_PACKAGES_USER -c "gpg --homedir $tmp_gpg_folder --import < $folder/GPG-KEY"
          su $SIGN_PACKAGES_USER -c "gpg --homedir $tmp_gpg_folder --import < $tmp_gpg_pubkey/pubkey.asc"

          # export temporary keyring to repository keyring
          su $SIGN_PACKAGES_USER -c "gpg --homedir $tmp_gpg_folder --export --armor" > $folder/GPG-KEY
        else
          # merge pubkey information in a temporary keyring
          gpg --export --armor $SIGN_KEYID > $tmp_gpg_pubkey/pubkey.asc
          gpg --homedir $tmp_gpg_folder --import < $folder/GPG-KEY
          gpg --homedir $tmp_gpg_folder --import < $tmp_gpg_pubkey/pubkey.asc

          # export temporary keyring to repository keyring
          gpg --homedir $tmp_gpg_folder --export --armor > $folder/GPG-KEY
        fi

        # cleanup
        rm -rf $tmp_gpg_folder $tmp_gpg_pubkey

      fi
    else
      echo "Adding OpenPGP key id $SIGN_KEYID to $folder/GPG-KEY file..."
      if [ ! -z "$SIGN_PACKAGES_USER" ]; then
        su $SIGN_PACKAGES_USER -c "gpg --export --armor $SIGN_KEYID" > $folder/GPG-KEY
      else
        gpg --export --armor $SIGN_KEYID > $folder/GPG-KEY
      fi
    fi
    svn_add $folder/GPG-KEY
  fi

}

# -----------------------------------------------
#                 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
  ERROR_MANIFEST=43   # manifest 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" ;;
    $ERROR_MANIFEST)
      eecho $error "$BASENAME: problem with Manifest file, 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

}

# -----------------------------------------------
#                 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

}

function regexp_slash {

  # escape slashes
  echo $1 | sed -e 's/\//\\\//g'

}

function is_the_same {

  # check if two files are in fact the same
  # usage: is_the_same <path1> <path2>

  if [ -e "$1" ] && [ -e "$2" ] && \
     [ "`stat -c '%d' $1`" == "`stat -c '%d' $2`" ] && \
     [ "`stat -c '%i' $1`" == "`stat -c '%i' $2`" ]; then
     return 0
  else
    return 1
  fi

}

function check_gnupg {

  # setup gnupg keyring if needed
  # usage: check_gnupg [username]

  local user="$1" home

  if [ ! -z "$user" ]; then
    home="`grep "^$user:" /etc/passwd | cut -d : -f 6`"
    if [ ! -d "$home/.gnupg" ]; then
      echo "Setting up gnupg..."
      su $user -c "gpg --list-keys"
    fi
  else
    if [ ! -d "$HOME/.gnupg" ]; then
      echo "Setting up gnupg..."
      gpg --list-keys
    fi
  fi

}

function rmd160sum {

  # computes RIPEMD-160 message digest
  # usage: rmd160sum <file>

  local sum file="$1"

  if [ ! -e "$file" ]; then
    return 1
  fi

  sum="`openssl rmd160 $file | awk '{ print $2 }'`"
  echo "$sum  $file"

}

function gethash {

  # get a file's hash
  # usage: gethash <algorithm> <file>

  if [ ! -e "$2" ]; then
    return 1
  fi

  $1sum $file | awk '{ print $1 }'

}

function file_size {

  # get file size
  # usage: filesize <file>

  if [ ! -e "$1" ]; then
    return 1
  fi

  wc -c $file | awk '{ print $1 }'

}

function file_extension {

  # output file extension
  # usage: filesize <filename>

  echo `basename $1` | sed -e 's/.*\.\(.*\)$/\1/'

}

function check_installed {

  # checks if a package is installed 
  # usage: check_installed <package_name> [root]

  eval "ls /$2/var/log/packages/ | grep -E '^$1-[^-]+-[^-]+-[^-]+$'"

}