aboutsummaryrefslogtreecommitdiff
path: root/src/simplaret
blob: 8735074e34c54b28c962690347d7e0c878b62267 (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
#!/bin/bash
#
# simplaret v0.2: simplepkg's retrieval tool
# feedback: rhatto at riseup.net | gpl
#
#  Simplaret is free software; you can redistribute it and/or modify it under the
#  terms of the GNU General Public License as published by the Free Software
#  Foundation; either version 2 of the License, or any later version.
#
#  Simplaret is distributed in the hope that it will be useful, but WITHOUT ANY
#  WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
#  A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License along with
#  this program; if not, write to the Free Software Foundation, Inc., 59 Temple
#  Place - Suite 330, Boston, MA 02111-1307, USA
#

BASENAME="`basename $0`"
REPOS_CONF="/etc/simplepkg/repos.conf"
COMMON="/usr/libexec/simplepkg/common.sh"

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

function simplaret_usage {

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

}

function simplaret_get_index {

  for file in `simplaret_metafiles`; do
    simplaret_download $1 $file $2 --no-verbose
  done

}

function simplaret_backup_index {

  for file in `simplaret_metafiles`; do
    if [ -f "$1/$file" ]; then
      mv $1/$file $1/$file.old
    fi
  done

}

function simplaret_check_index {

  for file in `simplaret_metafiles`; do
    if [ ! -f "$1/$file" ] && [ -f "$1/$file" ]; then
      echo restoring old $file to $1...
      mv $1/$file.old $1/$file
     else
      rm -f $1/$file.old
    fi
  done

}

function simplaret_download {

  # download a file from a repo to a folder
  # usage: simplaret <repository_url> <package> <destination-folder> [--no-verbose]

  local protocol file
  local wget_timeout wget_passive_ftp wget_verbose
  local curl_timeout curl_passive_ftp curl_verbose
  local ncftpget_timeout ncftpget_passive_ftp

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

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

  if [ ! -z "$CONNECT_TIMEOUT" ] || [ "$CONNECT_TIMEOUT" != "0" ]; then
    wget_timeout="--timeout $CONNECT_TIMEOUT"
    ncftpget_timeout="-t $CONNECT_TIMEOUT"
    curl_timeout="--connect-timeout $CONNECT_TIMEOUT"
  fi

  if [ "$4" == "--no-verbose" ]; then
    wget_verbose="--no-verbose"
    curl_verbose="-#"
    echo ""
  fi

  if [ "$protocol" == "http" ]; then

    echo Getting $1/$file:
    if [ "$HTTP_TOOL" == "wget" ]; then
      wget $wget_timeout $wget_verbose $1/$2 -O $3/$file
    elif [ "$HTTP_TOOL" == "curl" ]; then
      curl $curl_timeout $curl_verbose $1/$2 > $3/$file
    else
      echo $BASENAME: error: invalid value for config variable HTTP_TOOL: $HTTP_TOOL
      echo $BASENAME: please check your config file $CONF
      exit 1
    fi

  elif [ "$protocol" == "ftp" ]; then
    echo Getting $1/$file:

    if [ "$PASSIVE_FTP" == "1" ]; then
      wget_passive_ftp="--passive-ftp"
      ncftpget_passive_ftp="-F"
      curl_passive_ftp="--ftp-pasv"
    fi 

    if [ "$FTP_TOOL" == "ncftpget" ]; then
      ncftpget -c $ncftpget_timeout $ncftpget_passive_ftp $1/$2 > $3/$file
    elif [ "$FTP_TOOL" == "wget" ]; then
      wget $wget_timeout $wget_passive_ftp $wget_verbose $1/$2 -O $3/$file
    elif [ "$FTP_TOOL" == "curl" ]; then
      curl $curl_timeout $curl_passive_ftp $curl_verbose $1/$2 > $3/$file
    else
      echo $BASENAME: error: invalid value for config variable FTP_TOOL: $FTP_TOOL
      echo $BASENAME: please check your config file $CONF
      exit 1
    fi

  elif [ "$protocol" == "file" ]; then

    url="`echo $1 | sed -e 's/file:\/\///'`"
    if [ -f "$3/$file" ]; then
      rm -f $3/$file
    fi
    echo -n "Copying $url/$2..."
    if [ -f "$url/$2" ]; then
      cp $url/$2 $3/$file 2> /dev/null
    fi
    if [ -f "$3/$file" ]; then
      echo " done."
    else
      echo " failed."
    fi

  else

    echo $BASENAME error: invalid protocol $protocol

  fi

}

function simplaret_repository {

  # return a repository definition from $REPOS_CONF file
  # usage: simplaret_repository [root|repos|noarch|patches]

  local definition

  if [ -z "$1" ]; then
    definition="ROOT"
  else
    definition="`echo $1 | tr '[:lower:]' '[:upper:]'`"
  fi

  if [ "$definition" == "REPOS" ] || [ "$definition" == "PATCHES" ]; then
    definition="$definition-$ARCH-$VERSION"
  elif [ "$definition" == "ROOT" ]; then
    definition="$definition-$ARCH"
  fi

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

}

function simplaret_repository_name {

  # return a repository name according the value of $repository

  if [ -z "$repository" ]; then
    false
  elif echo $repository | grep -qe %; then
    repository_name="`echo $repository | cut -d % -f 1`"
    if [ -z "$repository_name" ]; then
      echo $BASENAME: you should set a name for the repository $repository
      echo $BASENAME: please correct your $REPOS_CONF
      exit 1
    fi
  else
    echo $BASENAME: you should set a name for the repository $repository
    echo $BASENAME: please correct your $REPOS_CONF
    exit 1
  fi

}

function simplaret_repository_url {

  # return a repository url according the value of $repository

  if echo $repository | grep -qe %; then
    repository_url="`echo $repository | cut -d % -f 2`"
    if [ -z "$repository_url" ]; then
      echo $BASENAME: you should set a url for the repository $repository
      echo $BASENAME: please correct your $REPOS_CONF
      exit 1
    fi
  else
    echo $BASENAME: you should set a url for the repository $repository
    echo $BASENAME: please correct your $REPOS_CONF
    exit 1
  fi

  if [ "$repos_type" == "root" ]; then
    simplaret_distro_folder
    repository_url="$repository_url/$DISTRO_FOLDER/$EXTRA_FOLDER" 
  fi

}

function simplaret_set_storage_folder {

  storage="$STORAGE/$ARCH/$VERSION/$repos_type"
  if [ "$repos_type" == "noarch" ]; then
    storage="$STORAGE/noarch"
  elif [ "$repos_type" == "patches" ]; then
    storage="$PATCHES_DIR/$ARCH/$VERSION"
  fi

}

function simplaret_update {

  local storage

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

  for repos_type in patches root repos noarch; do

    simplaret_set_storage_folder

    for repository in `simplaret_repository $repos_type`; do

      simplaret_repository_name
      simplaret_repository_url

      if [ ! -d "$storage/$repository_name" ]; then
        mkdir -p $storage/$repository_name
      else
        simplaret_backup_index $storage/$repository_name
      fi

      simplaret_get_index $repository_url $storage/$repository_name
      simplaret_check_index $storage/$repository_name

      unset repository_name repository_url repository_protocol

    done
  done

}

function simplaret_find_package {

  # grep packages in a repository's file list
  # usage: simplaret_find_package <package-name|-all> <repository-folder>

  if [ "$1" == "-all" ]; then
    grep -e ".tgz$" $2/`simplaret_filelist` | awk '{ print $8 }'
  else
    grep $1 $2/`simplaret_filelist` | awk '{ print $8 }' | grep -e ".tgz$"
  fi

}

function simplaret_show_package {

  # print a package result
  # usage: simplaret_show_package <package-file-name> [--basename-only|--filename-only|--formatted]

  if [ "$2" == "--basename-only" ]; then
    echo `basename $1`
  elif [ "$2" == "--filename-only" ]; then
    echo $1
  elif [ "$2" == "--formatted" ]; then
    echo $1,$repos_type,$repository
  else 
    if echo $1 | grep -q "/patches/"; then
      patch="(patch)"
    fi
    if [ "$repos_type" == "noarch" ]; then
      echo $name repository $repository_name: `basename $1` $patch
    else
      echo $name repository $repository_name, arch: $ARCH, version: $VERSION: `basename $1` $patch
    fi
  fi
  unset patch

}

function simplaret_filelist {

  if [ "$repos_type" == "patches" ]; then
    echo FILE_LIST
  else
    echo FILELIST.TXT
  fi

}

function simplaret_metafiles {

  echo `simplaret_filelist` CHECKSUMS.md5

}

function simplaret_search {

  # search packages
  # usage: simplaret_search [package-name] [-display_mode]
  # display_mode can be any accepted by simplaret_show_package

  local priority message pattern mode

  if [ ! -z "$1" ] && ! echo $1 | grep -q -e "^-"; then
    pattern="$1"
    mode="$2"
  else
    pattern="-all"
    mode="$1"
  fi

  for repos_type in patches root repos noarch; do

    name="`echo $repos_type | tr '[:lower:]' '[:upper:]'`"
    simplaret_set_storage_folder

    for repository in `simplaret_repository $repos_type`; do
      
      simplaret_repository_name

      if [ ! -f "$storage/$repository_name/`simplaret_filelist`" ]; then
        if [ "$WARNING" != "0" ] || [ ! -z "$SILENT" ]; then
          if [ "$repos_type" == "noarch" ]; then
            message=""
          else
            message="on arch $ARCH version $VERSION"
          fi
          echo warning: no file list for $repository_name repository $repository_name $message
          echo please do a simplaret --update
        fi
      else

        if [ "$repos_type" == "root" ]; then
          # root repositories has ROOT_PRIORITY
          for priority in $ROOT_PRIORITY; do
            for file in `simplaret_find_package $pattern $storage/$repository_name | grep "/$priority/"`; do
              simplaret_show_package $file $mode
            done
          done
        else
          for file in `simplaret_find_package $pattern $storage/$repository_name`; do
            simplaret_show_package $file $mode
          done
        fi

      fi

    done
  done

}

function simplaret_purge {

  # purge simplaret package cache
  # usage: simplaret_purge [-w N]

  local mtime mtime_message which and_patches

  if [ "$2" == "-w" ] && [ ! -z "$3" ]; then
    mtime="-mtime +`echo "$3*7" | bc -l`"
    mtime_message="older than $3 weeks"
  elif [ "$SIMPLARET_PURGE_WEEKS" != "0" ]; then
    mtime="-mtime +`echo "$SIMPLARET_PURGE_WEEKS*7" | bc -l`"
    mtime_message="older than $SIMPLARET_PURGE_WEEKS weeks"
  else
    mtime=""
    mtime_mesage=""
  fi

  which="root repos noarch"
  and_patches=""

  if [ "$SIMPLARET_PURGE_PATCHES" == "1" ]; then
    which="patches $which"
    and_patches="including patches"
  fi

  if [ -z "$SILENT" ]; then
    echo "$BASENAME: purging all packages $mtime_message for:"
    echo -e "\t- arch $ARCH and version $VERSION $and_patches"
    echo -e "\t- noarch folder"
  fi

  for repos_type in $which; do

    simplaret_set_storage_folder

    for file in `find $storage/ $mtime 2> /dev/null`; do
      for extension in tgz asc meta txt slack-required; do
        if echo $file | grep -qe ".$extension$"; then
          rm $file
        fi
      done
    done

  done

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

}

function simplaret_search_and_delete {

  local file candidate place basename

  for file in `find $2/ -name $1*tgz 2> /dev/null`; do
    candidate="`basename $file`"
    if [ "`package_name $candidate`" == "$1" ]; then
      # check if has the same version and build number, otherwise erase the old one
      for result in `simplaret_search $(package_name $candidate) --basename-only`; do
        if [ "`package_name $candidate`" == "`package_name $result`" ]; then
          if [ "`package_version $candidate`" == "`package_version $result`" ] && \
             [ "`package_build $candidate`" == "`package_build $result`" ]; then
             LAST_DOWNLOADED_PACKAGE="$file"
             if [ "$3" != "--silent" ]; then
               echo Package $candidate already downloaded
               # echo Package $candidate stored at `dirname $file`
             else
               true
               # echo $file
             fi
             return 1
           else
             place="`dirname $file`"
             basename="`basename $file tgz`"
             rm -f $file
             rm -f $place/$candidate.slack-required
             rm -f $file.asc $place/$basename.meta $place/$basename.txt
             break
           fi
        fi
      done
    fi
  done

}

function simplaret_get {

  # get a package
  # usage: simplaret_get <package-name> [--silent]

  local silent

  # prevent user to stay in $storage
  cd 

  # first search for an already downloaded package
  for repos_type in patches root repos noarch; do

    simplaret_set_storage_folder
    simplaret_search_and_delete $1 $storage $2

    if [ "$?" == "1" ]; then
      return 0
    fi

  done

  # then search for the package in the repositories
  for result in `simplaret_search $1 --formatted`; do

    file="`echo $result | cut -d , -f 1`"
    repos_type="`echo $result | cut -d , -f 2`"
    repository="`echo $result | cut -d , -f 3`"

    simplaret_set_storage_folder
    simplaret_repository_name

    candidate="`basename $file`"
    if [ "`package_name $candidate`" == "$1" ]; then
      simplaret_repository_url

      # if repos_type == root, the package is a patch and
      # STORE_ROOT_PATCHES_ON_PATCHES_DIR config parameter is enabled
      # save it on $PATCHES_DIR/root-$repository_name, so all patches
      # are placed in the same tree
      if [ "$repos_type" == "root" ] && \
         [ "$STORE_ROOT_PATCHES_ON_PATCHES_DIR" == "1" ] && echo $file | grep -q "patches"; then
        folder="$PATCHES_DIR/$ARCH/$VERSION/root-$repository_name"
      else
        folder="$storage/$repository_name"
      fi

      # download the package
      simplaret_download $repository_url $file $folder

      if [ -f "$folder/$candidate.asc" ]; then
        rm $folder/$candidate.asc
      fi

      if [ -f "$folder/$1.slack-required" ]; then
        rm $folder/$1.slack-required
      fi

      # download the signature, if exist
      if simplaret_check_url $repository_url/$file.asc; then
        simplaret_download $repository_url $file.asc $folder
      fi

      # download slack-required, if exist
      if simplaret_check_url $repository_url/`dirname $file`/$1.slack-required; then
        simplaret_download $repository_url `dirname $file`/$1.slack-required $folder
      fi

      if [ ! -f "$folder/$candidate" ]; then
        LAST_DOWNLOADED_PACKAGE="0"
        if [ "$2" != "--silent" ]; then
          echo Error downloading $candidate from $repos_type repository $repository_url, please check your settings
        fi
        return 1
      else
        LAST_DOWNLOADED_PACKAGE="$folder/$candidate"
        if [ "$2" != "--silent" ]; then
          silent=""
          echo Package $candidate stored at $folder
        else
          # echo $folder/$candidate
          silent="--silent"
        fi
        if [ -f "$folder/$candidate.asc" ] || [ "$SIGNATURE_CHECKING" == "1" ]; then
          gpg --verify $folder/$candidate.asc $folder/$candidate
        fi
        simplaret_checksum $storage/$repository_name/CHECKSUMS.md5 $folder/$candidate $silent
        return $?
      fi

    fi

  done

}

function simplaret_search_and_process_patch {

  local package_version package_build installed_version
  local installed_build repos_type get is_patch package_match

  # get the repository type
  repos_type="`echo $sugested | cut -d , -f 2`"

  # get just the file name
  sugested="`echo $sugested | cut -d , -f 1`"

  if echo $sugested | grep -q "patches"; then
    is_patch="yes"
  else
    is_patch="no"
  fi

  # now split the file name into pieces
  package_version="`package_version $sugested`"
  package_build="`package_build $sugested`"
  sugested="`package_name $sugested`"

  # check if the patch was already downloaded
  if echo "$DOWNLOADED_PATCHES" | grep -q " $ARCH:$VERSION:$sugested "; then
    if [ "$IS_UPGRADE" != "1" ]; then
      echo Package $sugested already downloaded
      # echo "Jail $root needs package $sugested (already downloaded, skipping)"
      return
    fi
  fi

  # search if its installed in the jail
  installed_packs="`ls /$root/var/log/packages/$sugested* 2> /dev/null`"
  if [ ! -z "$installed_packs" ]; then

    for installed in $installed_packs; do
      if [[ "$sugested" == "`package_name $installed.tgz`" ]]; then
        package_installed="1"
        installed_version="`package_version $installed.tgz`"
        installed_build="`package_build $installed.tgz`"
        break
      fi
    done

    get="no"

    # if the package is installed, download the patch
    if [ "$package_installed" == "1" ]; then
      if [ "$repos_type" == "patches" ]; then

        if  [ "$package_version" != "$installed_version" ] || [ "$package_build" != "$installed_build" ]; then
          get="yes"
          package_match="no"
        elif [ "$DOWNLOAD_EVEN_APPLIED_PATCHES" == "1" ]; then
          get="yes"
        fi

      elif [ "$repos_type" == "root" ] && [ "$is_patch" == "yes" ]; then

        if  [ "$package_version" != "$installed_version" ] || [ "$package_build" != "$installed_build" ]; then
          get="yes"
          package_match="no"
        elif [ "$DOWNLOAD_EVEN_APPLIED_PATCHES" == "1" ]; then
          get="yes"
        fi

      else
        # here, we're dealing with repositories other than ROOT and REPOS,
        # so we need to check if either version or build number are different,
        # otherwise all installed packages would be downloaded
        if [ "$package_version" != "$installed_version" ] || [ "$package_build" != "$installed_build" ]; then
          get="yes"
          package_match="no"
        fi
      fi
    fi

    # finally, get the package
    if [ "$get" == "yes" ]; then
      if [ "$IS_UPGRADE" == "1" ]; then
        if [ "$package_match" == "no" ]; then
          simplaret_install $sugested
        else
          simplaret_get $sugested
        fi
      else
        simplaret_get $sugested
      fi
      if [ "$?" == "0" ]; then
        DOWNLOADED_PATCHES="$DOWNLOADED_PATCHES $ARCH:$VERSION:$sugested " # the ending space is important
      fi
    fi

  fi

  unset package_installed get

}

function simplaret_get_jail_patches {

  # get patches from a jail
  # usage: simplaret_get_jail_patches <jail-folder>

  local oldarch oldversion

  if [ ! -z "$1" ]; then
    root="$1"
  else
    root="/"
  fi

  # save current arch and version
  oldarch="$ARCH"
  oldversion="$VERSION"

  ARCH="`default_arch $root`"
  VERSION="`default_version $root`"

  # in case there's something wrong with the jail, abort
  if [ -z "$VERSION" ] || [ -z "$ARCH" ]; then
    return
  fi

  # we need to do that for each arch/version pairs, but just once for each pair
  if ! echo "$DISTRO_UPDATED" | grep -q " $ARCH:$VERSION "; then
    simplaret_update
    DISTRO_UPDATED="$DISTRO_UPDATED $ARCH:$VERSION " # the ending space is important
    echo ""
  fi

  echo Fetching patches for arch $ARCH and version $VERSION for jail $root

  # list all available patches from PATCHES and ROOT repositories
  for sugested in `simplaret_search --formatted | grep patches | grep -v ",repos," | grep -v ",noarch,"`; do
    simplaret_search_and_process_patch
  done

  # grab patches from every other places
  if [ "$CONSIDER_ALL_PACKAGES_AS_PATCHES" == "1" ]; then

    for sugested in `simplaret_search --formatted | grep patches | grep ",repos," | grep ",noarch,"`; do
      simplaret_search_and_process_patch
    done

    for sugested in `simplaret_search --formatted | grep -v patches`; do
      simplaret_search_and_process_patch
    done

  fi

  # restore arch and version
  ARCH="$oldarch"
  VERSION="$oldversion"

}

function simplaret_get_patches {

  local jailpath oldroot

  if [ "$1" == "--upgrade" ]; then
    IS_UPGRADE="1"
  fi

  oldroot="$ROOT"

  # first get patches from the root system
  simplaret_get_jail_patches

  # then get the needed patches for each installed jail
  if [ -s "$JAIL_LIST" ]; then
    for jailpath in `cat $JAIL_LIST`; do
      if [ -d "$jailpath/var/log/packages" ]; then
        ROOT="$jailpath"
        simplaret_get_jail_patches $jailpath
      fi
    done
  fi

  ROOT="$oldroot"

}

function simplaret_checksum {

  # simplaret_checksum <md5file> <file-name> [--silent]

  if [ ! -f "$1" ] || [ ! -f "$2" ]; then
    if [ "$3" != "--silent" ]; then
      echo Checksum error: file not found
    fi
    return 1
  fi

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

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

}

function simplaret_install {

  # download and install a package
  # usage: simplaret_install <package-name> [--skip-checks]

  local package root jail_arch jail_version slack_required dep dependency tmp

  root="/$ROOT"
  mkdir -p $root/var/log/setup/tmp

  if [ "`echo $1 | sed -e 's/\(..\).*/\1/g'`" == "--" ]; then
    echo $BASENAME: install: syntax error: expected package name
    return 1
  fi

  # now we check if ARCH and VERSION from the
  # repository are the same of the jail
  if [ "$2" != "--skip-checks" ]; then
    jail_arch="`default_arch $root`"
    jail_version="`default_version $root`"
    if [ "$ARCH" != "$jail_arch" ]; then
      echo "$BASENAME: requested repository arch ($ARCH) doesn't match jail arch ($jail_arch)"
      echo "$BASENAME: please use \"$BASENAME --get $1 --skip-checks\" to ignore this warning and install anyway"
      return
    elif [ "$VERSION" != "$jail_version" ]; then
      echo "$BASENAME: requested repository version ($VERSION) doesn't match jail version ($jail_version)"
      echo "$BASENAME: please use \"$BASENAME --get $1 --skip-checks\" to ignore this warning and install anyway"
      return 1
    fi
  fi

  # package="`simplaret_get $1 --silent`"
  simplaret_get $1 --silent
  package="$LAST_DOWNLOADED_PACKAGE"

  if [ "$package" != "0" ] && [ ! -z "$package" ]; then
    slack_required="`dirname $package`/$1.slack-required"
    if [ -f "$package" ]; then

      if [ -f "$slack_required" ] && [ "$DEPENDENCY_CHECKING" == "1" ]; then
        # this routine checks for dependencies in package's slack-required
        # procedure adapted from createpkg script
        ( cat $slack_required | while read dep; do
           if [ ! -z "$dep" ]; then
            dependency="`echo $dep | awk '{ print $1 }'`"
            simplaret_solve_dep $1 $dependency $root
           fi
           true
         done )
      fi

      ROOT=$root upgradepkg --install-new $package
      LAST_DOWNLOADED_PACKAGE="0"

    else
      echo "error: could not install package $1: file not found"
      LAST_DOWNLOADED_PACKAGE="0"
      return 1
    fi
  else
    echo "error: could not install package $1"
    LAST_DOWNLOADED_PACKAGE="0"
    return 1
  fi

}

function simplaret_distro_folder {

  # first we point to the correct arch
  simplaret_set_arch

  # then we set the distro folder
  if [ "$ARCH" == "i386" ]; then
    DISTRO="slackware"
    DISTRO_FOLDER="$DISTRO-$VERSION"
  elif [ "$ARCH" == "x86_64" ]; then
    # EXTRA_FOLDER="tree"
    DISTRO="slamd64"
    DISTRO_FOLDER="$DISTRO-$VERSION"
  elif [ "$ARCH" == "s390" ]; then
    DISTRO="slack390"
    DISTRO_FOLDER="$DISTRO-$VERSION"
  elif [ "$ARCH" == "x86_uclibc" ]; then
    DISTRO="ucslack"
    DISTRO_FOLDER="$DISTRO-$VERSION"
  elif [ "$ARCH" == "arm" ]; then
    DISTRO="armedslack"
    DISTRO_FOLDER="$DISTRO-$VERSION"
  elif [ "$ARCH" == "powerpc" ]; then
    DISTRO="slackintosh"
    DISTRO_FOLDER="$VERSION"
  elif [ "$ARCH" == "sparc" ]; then
    DISTRO="splack"
    DISTRO_FOLDER="tree-$VERSION"
  else
    DISTRO="$ARCH"
    DISTRO_FOLDER="$DISTRO-$VERSION"
  fi

}

function simplaret_set_arch {

  # set correct value for ARCH

  local repos_type new_arch

  # any arch defined in ARCH_i386 that hasn't an entry
  # on $REPOS_CONF will be mapped to i386

  ARCH_i386=" nocona prescott pentium4m pentium4 pentium-m pentium3m pentium3 "
  ARCH_i386="$ARCH_i386 pentium2 i686 pentium-pro i586 pentium-mmx pentium i486 "
  ARCH_i386="$ARCH_i386 athlon-mp athlon-xp athlon4 athlon-tbird athlon k6 k6-2 "
  ARCH_i386="$ARCH_i386 k6-3 winchip-c6 winchip2 c3 c3-2 i386 "

  # any arch defined in ARCH_x86_64 that hasn't an entry
  # on $REPOS_CONF will be mapped to x86_64

  ARCH_x86_64=" k8 opteron athlon64 athlon-fx x86_64 "

  for repos_type in patches root repos noarch; do
    if [ -z "`simplaret_repository $repos_type`" ]; then
      # there's no repository definition for that arch
      if echo "$ARCH_i386" | grep -q " $ARCH "; then
        new_arch="i386"
      elif echo "$ARCH_x86_64" | grep -q " $ARCH "; then
        new_arch="x86_64"
      else
        echo "$BASENAME: error: no repository definition for arch $ARCH"
        echo "$BASENAME: please check your $CONF and $REPOS_CONF config files"
        exit 1
      fi
    else
      return
    fi
  done

  echo "$BASENAME: changing arch from $ARCH to $new_arch"

  ARCH="$new_arch"

}

function simplaret_check_url {

  # check if a given url exist, use just with small files
  # usage: simplaret_check_url <url>

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

  if [ ! -z "$CONNECT_TIMEOUT" ] || [ "$CONNECT_TIMEOUT" != "0" ]; then
    curl_timeout="--connect-timeout $CONNECT_TIMEOUT"
  fi

  if [ "`curl $curl_timeout -I $1 2> /dev/null | head -n 1 | awk '{ print $2 }'`" == "200" ]; then
    # server gave a 200 response, so the url exist
    return 0
  else
    # the url is missing
    return 1
  fi

}

function simplaret_solve_dep {

  # solve dependency for a package
  # this function was adapted from createpkg script
  # usage: simplaret_solve_dep <package-name> <package-depencency-name> [root-folder]

  local installed check exit_code

  local package="$1"
  local pack="$2"
  local root="/$3"

  installed=`eval "ls $root/var/log/packages/ | egrep '^$pack-[^-]+-[^-]+-[^-]+$'"`
  check=$?

  if [ -z "$installed" ]; then
    if [ $check -ne 0 ]; then
      echo "$BASENAME: processing $1 dependency $pack" 
      # simplaret_install $pack
      SIMPLARET_CHILD=$SIMPLARET_CHILD ROOT=$root ARCH=$ARCH VERSION=$VERSION \
      simplaret --install $pack
    fi
  fi

}

if [ -z "$1" ]; then
  simplaret_usage
  exit 1
else
  eval_config $BASENAME
fi

# This is used to show how many children process we have
if [ -z "$SIMPLARET_CHILD" ]; then
  SIMPLARET_CHILD="1"
else
  let SIMPLARET_CHILD++
fi

BASENAME="`basename $0`[$SIMPLARET_CHILD]"

case $1 in
  "--update" | "update") simplaret_update ;;
  "--search" | "search") shift ; simplaret_search $* ;;
  "--get" | "get") shift ; simplaret_get $* ;;
  "--get-patches" | "get-patches") simplaret_get_patches ;;
  "--purge" | "purge") shift ; simplaret_purge $* ;;
  "--install" | "install") shift ; simplaret_install $* ;;
  "--upgrade" | "upgrade") simplaret_get_patches --upgrade ;;
  "--remove" | "remove") removepkg $2 ;;
  *) simplaret_usage ;;
esac