aboutsummaryrefslogtreecommitdiffstats
path: root/devel/portmk/scripts/distfiles.sh
blob: 994d40433bbaca8b143cbb3145d74c0d318c53b3 (plain) (blame)
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
#!/bin/sh -e
#
# Copyright (c) 2004 Oliver Eikemeier.  All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
# 1. Redistributions of source code must retain the above copyright notice
#    this list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
#
# 3. Neither the name of the author nor the names of its contributors may be
#    used to endorse or promote products derived from this software without
#    specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
# INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
# AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# $FreeBSD$
#
# MAINTAINER=   eik@FreeBSD.org
#
# These variables are referenced and must be exported from the Makefile:
#
# DISABLE_SIZE
# DISTFILES
# DISTINFO_ALGORITHMS
# DISTINFO_LABEL
# DIST_SUBDIR
# ECHO_MSG
# FETCH_AFTER_ARGS
# FETCH_BEFORE_ARGS
# FETCH_CMD
# FETCH_ENV
# FETCH_SYMLINK_DISTFILES
# IGNOREFILES
# LOCALBASE
# MASTER_SITES
# MASTER_SITE_BACKUP
# MASTER_SITE_OVERRIDE
# MASTER_SORT_AWK
# MASTER_SORT_CMD
# MASTER_SORT_ENV
# MK_FILE
# NO_SIZE
# PATCHFILES
# PATCH_SITES
# PERL5
# VALID_ALGORITHMS
#
# These variables are user settable:
#
# DISABLE_SIZE
# NO_CHECKSUM
# FETCH_REGET
#

# utilities

LOCALBASE="${LOCALBASE:-/usr/local}"

AWK=/usr/bin/awk
CAT=/bin/cat
CUT=/usr/bin/cut
EXPR=/bin/expr
FETCH=/usr/bin/fetch
GREP=/usr/bin/grep
LS=/bin/ls
MD5=/sbin/md5
MKDIR='/bin/mkdir -p'
MKTEMP=/usr/bin/mktemp
PERL5="${PERL5:-/usr/bin/perl}"
RM=/bin/rm
[ -x /sbin/rmd160 ] && RMD160=/sbin/rmd160
SED=/usr/bin/sed
SETENV=/usr/bin/env
[ -x /sbin/sha1 ] && SHA1=/sbin/sha1
SORT=/usr/bin/sort
TR=/usr/bin/tr
WC=/usr/bin/wc

if [ -x /usr/bin/openssl ]; then
  OPENSSL=/usr/bin/openssl
elif -x "$LOCALBASE/bin/openssl" ];then
  OPENSSL="$LOCALBASE/bin/openssl"
fi

# global constants

MK_FILE_ENDMARKER='^\.include[   ]*(<bsd\.port(\.post)?\.mk>|"\${MASTERDIR}\/Makefile")$'

FETCH_REGET="${FETCH_REGET:-1}"

### utility functions ###

###
# do_hash is a wrapper for md5/openssl/digest to calculate MD5/SHA1/RMD160 hashes
###

do_hash()
{
  local alg
  alg="$1"
  shift

  case "$alg" in
  [Mm][Dd]5)
    if [ $# -eq 0 ]; then
      $MD5
    else
      $MD5 -- "$@"
    fi;;
  [Ss][Hh][Aa]1)
    if [ -n "$SHA1" ]; then
      echo $SHA1
      if [ $# -eq 0 ]; then
        $SHA1
      else
        $SHA1 -- "$@"
      fi
    else
      if [ $# -eq 0 ]; then
        "${OPENSSL:-true}" sha1
      else
        "${OPENSSL:-true}" sha1 ./"$@" |
          $SED -En -e 's/^[[:alnum:]]+ *\((\.\/)?(.*)\) *= *([[:xdigit:]]+)$/SHA1 (\2) = \3/p'
      fi
    fi;;
  [Rr][Mm][Dd]160)
    if [ -n "$RMD160" ]; then
      if [ $# -eq 0 ]; then
        $RMD160
      else
        $RMD160 -- "$@"
      fi
    else
      if [ $# -eq 0 ]; then
        "${OPENSSL:-true}" rmd160
      else
        "${OPENSSL:-true}" rmd160 ./"$@" |
          $SED -En -e 's/^[[:alnum:]]+ *\((\.\/)?(.*)\) *= *([[:xdigit:]]+)$/RMD160 (\2) = \3/p'
      fi
    fi;;
  [Ss][Ii][Zz][Ee])
    if [ $# -eq 0 ]; then
      $WC -c
    else
      while [ $# -gt 0 ]; do
        # avoid hassle with CLICOLOR_FORCE
        $LS -ALdfln -- "$1" | $AWK '{ print "SIZE ('"$1"') = " $5 }'
        shift
      done
    fi;;
  *)
    return 1;;
  esac
}

###
# extract_sum extracts all checksums for a file in distinfo
###

extract_sum()
{
  local algorithms pattern
  algorithms=`echo -n "$1" | $TR -s ' \t\n' '|'`
  pattern=`echo "$2$3" | $SED -e 's;[/.[\(*+?{|^$];\\\\&;g'`
  # distfiles match [a-zA-Z0-9.,_/@&%?=+-]+

  [ $# -eq 3 ] || return 1

  if [ -f "$MD5_FILE" ]; then
    $SED -En -e '/^('"$algorithms"') *\('"$pattern"'\) *= */s//\1=/p' "$MD5_FILE"
  elif $GREP -qs "^#:distinfo${DISTINFO_LABEL:+\.$DISTINFO_LABEL}$" "$MK_FILE"; then
    $SED -En -e '
      1,/'"$MK_FILE_ENDMARKER"'/d
      /^#/!q
      /^#:distinfo'"${DISTINFO_LABEL:+.$DISTINFO_LABEL}"'$/,/^#:/ {
        s/^#('"$algorithms"') *\('"$pattern"'\) *= */\1=/
        t match
        b nomatch
        :match
        H
        :nomatch
      }
      $ {
        g
        p
      }
    ' "$MK_FILE"
  else
    for alg in $1; do
      for sum in `eval echo \\$DISTINFO_$alg`; do
        if [ "${sum%:*}" = "$3" ]; then
          echo "$alg=${sum##*:}"
        fi
      done
    done
  fi
}

###
# chksumfiles calculates CKSUMFILES = DISTFILES + PATCHFILES - IGNOREFILES
###

chksumfiles()
{
  echo "$IGNOREFILES" '<>' "$DISTFILES" "$PATCHFILES" |
    $TR -s ' \t' '\n' | $AWK '
      BEGIN { isignore=1 }
      /^$/ { next }
      /^<>$/ { isignore=0; next }
      {
        if (isignore !=0) {
          ignore[$0]=1
        } else if (!($0 in ignore)) {
          sub(/:[^:]+$/, "")
          print
        }
      }
    '
}

### fetching distfiles ###

###
# fetch_xxx are wrappers for fetch
###

setup_fetch_cmd()
{
  [ -z "$fetch_cmd" ] || return 0

  case "${FETCH_CMD%% *}" in
  */curl)
    fetch_cmd=fetch_curl;;
  */wget)
    fetch_cmd=fetch_wget;;
  *)
    fetch_cmd=fetch_fetch;;
  esac
}

fetch_curl()
{
  local cksize

  cksize=
  if [ -n "$3" ]; then
    cksize=" --max-filesize $3"
  fi

  $SETENV $FETCH_ENV $FETCH_CMD $FETCH_BEFORE_ARGS$cksize \
    $4 -o "$2" "$1" $FETCH_AFTER_ARGS
}

fetch_fetch()
{
  local cksize

  case "$2" in
  */*)
    [ -d "${file%/*}" ] || $MKDIR "${file%/*}";;
  esac

  symlink=
  if [ -n "$FETCH_SYMLINK_DISTFILES" ]; then
    symlink=" -l"
  fi

  cksize=
  if [ -n "$3" -a "$OSVERSION" -ge 480000 ]; then
    cksize=" -S $3"
  fi

  $SETENV $FETCH_ENV $FETCH_CMD $FETCH_BEFORE_ARGS$symlink$cksize \
    $4 -o "$2" "$1" $FETCH_AFTER_ARGS
}

fetch_wget()
{
  case "$4" in
  file:*)
    fetch_fetch "$@" || return 1
    return 0;;
  esac

  case "$2" in
  */*)
    [ -d "${file%/*}" ] || $MKDIR "${file%/*}";;
  esac

  $SETENV $FETCH_ENV $FETCH_CMD $FETCH_BEFORE_ARGS \
    $4 -O "$2" "$1" $FETCH_AFTER_ARGS
}

###
# get_master_sites returns a list of $1_SITES for groups $2
###

get_master_sites()
{
  [ $# -eq 2 ] || return 1

  # fun with awk
  for group in $2; do
    eval echo "\"\${$1_SITE_SUBDIR}\"" "'<>'" "\"\${$1_SITES}\"" |
    $TR -s ' \t' '\n' |
    $AWK '
      BEGIN {
        issubdir=1
        subdirs=0
      }
      /^$/ { next }
      /^<>$/ {
        issubdir=0
        if (subdirs == 0)
          subdir[subdirs++]=""
        next
      }
      {
        if ($0 ~ /\/:[^\/:]+$/) {
          if ($0 !~ /\/:([^\/:]*,)*'"$group"'(,[^\/:]*)*$/)
            next
          sub(/\/:[^\/:]+$/, issubdir != 0 ? "" : "/")
        } else if ("'"$group"'" != "DEFAULT") {
          next
        }
      }
      {
        if (issubdir != 0)
          subdir[subdirs++]=($0 == "." ? "" : $0 "/")
        else
          for (i in subdir) {
            site=$0
            gsub(/%SUBDIR%\//, subdir[i], site)
            print site
          }
      }
    ';
  done |
  $AWK '{ if ($0 in done) next; done[$0]=1 } { print }'
}

###
# get_master_sites_sorted returns a sorted list of $1_SITES for groups $2
###

get_master_sites_sorted()
{
  get_master_sites "$@" |
  if [ -n "$MASTER_SORT_CMD" ]; then
    $SETENV $MASTER_SORT_ENV $MASTER_SORT_CMD | $TR -s '\n' ' '
  elif [ -z "$KEEP_MASTER_SITE_ORDER" ]; then
    $AWK 'BEGIN { srand() } { print rand() "\t" $0 }' |
    $SORT -n | $CUT -f 2  | $TR -s '\n' ' ' |
    $AWK "$MASTER_SORT_AWK"
  else
    $TR -s '\n' ' ' |
    $AWK "$MASTER_SORT_AWK"
  fi
}

###
# fetch_file tries to fetch one file from {MASTER,PATCH}_SITES
###

fetch_file()
{
  [ $# -eq 5 ] || return 1

  fetch_reget="${FETCH_REGET}"
  SORTED_MASTER_SITES_TMP=`get_master_sites_sorted "$1" "$2"`
  for site in $_MASTER_SITE_OVERRIDE \
      $SORTED_MASTER_SITES_TMP $_MASTER_SITE_BACKUP; do
    $ECHO_MSG ">> Attempting to fetch from $site${4:+ (${4#*=} bytes)}."
    case "$site" in
    *%FILE%*)
      # this expression uses ctrl-A as delimiters
      url=`echo "$site" | $SED -E -e 's/$; s%FILE%'"$file"'g'`;;
    *)
      url="$site$file";;
    esac
    if $fetch_cmd "$url" "$3" "$4"; then
      [ -f "$file" ] || continue
      [ -z "$5" ] && return 0
      for cksum2 in $5; do
        case "${cksum2#*=}" in
        IGNORE)
          return 0;;
        *)
          CKSUM=`do_hash "${cksum2%%=*}" < "$file"`
          if [ "${cksum2#*=}" = "$CKSUM" ]; then
            $ECHO_MSG ">> Checksum OK (${cksum2%%=*}) for $file."
            return 0
          fi;;
        esac
      done
      $ECHO_MSG ">> Checksum mismatch for $_file."
      if [ "$fetch_reget" -gt 0 ]; then
        $ECHO_MSG "===>  Refetch for $fetch_reget more times."
        $RM -f "$file"
        fetch_reget=$(($fetch_reget-1))
      else
        $ECHO_MSG ">> To get this file from more mirrors," \
            "type \"make FETCH_REGET=99 [other args]\"."
        $ECHO_MSG "===>  Giving up on fetching $_file."
        return 1
      fi
    fi
  done
  $ECHO_MSG ">> Couldn't fetch it - please try to retrieve this"
  $ECHO_MSG ">> file manually into $_DISTDIR and try again."
  return 1
}

###
# do_fetch tries to fetch DISTFILES and PATCHFILES
###

do_fetch()
{
  local _DISTDIR
  local DIR
  local file
  local select

  setup_fetch_cmd

  _DISTDIR="$DISTDIR${DIST_SUBDIR:+/$DIST_SUBDIR}"
  DIR="${DIST_SUBDIR:+$DIST_SUBDIR/}"

  for fileset in MASTER PATCH; do
    case "$fileset" in
    MASTER)
      files="$DISTFILES";;
    PATCH)
      files="$PATCHFILES";;
    *)
      return 1;;
    esac
    for _file in $files; do

      file=`echo "$_file" | $SED -E -e 's/:[^:]+$//'`
      select=`echo "$_file" | $SED -En -e 'y/,/ /; s/.*:([^:]+)$/\1/p'`
      [ -n "$select" ] || select="DEFAULT"

      if [ -z "$NO_CHECKSUM" ]; then
        CKSUM2=`extract_sum "$VALID_ALGORITHMS" "$DIR" "$file"`
        if [ -z "$CKSUM2" ]; then
          $ECHO_MSG ">> No checksum entry for $DIR$file."
          $ECHO_MSG ">> Make sure the Makefile and distinfo file ($MD5_FILE) are"
          $ECHO_MSG ">> up to date.  If you are absolutely sure you want to override"
          $ECHO_MSG ">> this check, type \"make NO_CHECKSUM=yes [other args]\"."
          return 1
        fi
        chkfile=true
        for ignore in $IGNOREFILES; do
          if [ "$file" = "$ignore" ]; then
            chkfile=false
          fi
        done
      else
        CKSUM2=
        chkfile=false
      fi

      if [ -f "$_DISTDIR/$file" ]; then
        fileexists=true
      else
        fileexists=false
        if [ -L $file ]; then
          $ECHO_MSG ">> $_DISTDIR/$file is a broken symlink."
          $ECHO_MSG ">> Perhaps a filesystem (most likely a CD) isn't mounted?"
          $ECHO_MSG ">> Please correct this problem and try again."
          return 1
        fi
      fi

      fetchfile=true
      if $fileexists; then
        if [ -n "$CKSUM2" ]; then
          for cksum2 in $CKSUM2; do
            case "${cksum2#*=}" in
            IGNORE)
              if $chkfile; then
                $ECHO_MSG ">> Checksum for $file is set to IGNORE in distinfo file"
                $ECHO_MSG "   even though the file is not in the "'$'"{IGNOREFILES} list."
                return 1
              fi
              fetchfile=false
              break;;
            *)
              if ! $chkfile; then
                $ECHO_MSG ">> Checksum for $file is not set to IGNORE in distinfo file"
                $ECHO_MSG "   even though the file is in the "'$'"{IGNOREFILES} list."
                return 1
              fi
              CKSUM=`do_hash "${cksum2%%=*}" < "$_DISTDIR/$file"`
              if [ "${cksum2#*=}" = "$CKSUM" ]; then
                $ECHO_MSG ">> Checksum OK (${cksum2%%=*}) for $file."
                fetchfile=false
                break
              fi;;
            esac
          done
          if $fetchfile; then
            $ECHO_MSG ">> Checksum mismatch for $file."
          fi
        else
          fetchfile=false
        fi
      else
        if [ -n "$CKSUM2" ]; then
          for cksum2 in $CKSUM2; do
            case "${cksum2#*=}" in
            IGNORE)
              if $chkfile; then
                $ECHO_MSG ">> Checksum for $file is set to IGNORE in distinfo file"
                $ECHO_MSG "   even though the file is not in the "'$'"{IGNOREFILES} list."
                return 1
              fi
              break;;
            *)
              if ! $chkfile; then
                $ECHO_MSG ">> Checksum for $file is not set to IGNORE in distinfo file"
                $ECHO_MSG "   even though the file is in the "'$'"{IGNOREFILES} list."
                return 1
              fi;;
            esac
          done
        fi
        $ECHO_MSG ">> $file doesn't seem to exist in $_DISTDIR."
      fi

      if $fetchfile; then
        if ! { [ -d "$_DISTDIR" ] || $MKDIR "$_DISTDIR"; }; then
           $ECHO_MSG ">> Cannot create $_DISTDIR."
           return 1
        fi
        if [ ! -w "$_DISTDIR" ]; then
           $ECHO_MSG ">> $_DISTDIR is not writable by you; cannot fetch."
           return 1
        fi
        if [ -z "$NO_CHECKSUM" -a -z "$DISABLE_SIZE" ]; then
          CKSIZE=`extract_sum 'SIZE' "$DIR" "$file"`
        else
          CKSIZE=
        fi
        cd "$_DISTDIR"
        if ! fetch_file "$fileset" "$select" "$file" "${CKSIZE#*=}" "$CKSUM2"; then
          return 1
        fi
      fi
    done
  done
}

### missing size ###

###
# print_missing_size prints a list of all files that are not in DISTDIR
###

print_missing_size()
{
  local _DISTDIR
  local DIR
  local file
  local select

  CKSUMFILES="${CKSUMFILES:-`chksumfiles`}"

  DIR="${DIST_SUBDIR:+$DIST_SUBDIR/}"

  for _file in $CKSUMFILES; do
    file="$DIR$_file"
    [ -f "$DISTDIR/$file" ] && continue
    CKSIZE=`extract_sum 'SIZE' "$DIR" "$_file"`
    if [ -n "${CKSIZE#*=}" ]; then
      echo "${CKSIZE#*=}    $file"
    else
      echo "0   $file"
    fi
  done

  for _file in $IGNOREFILES; do
    file="$DIR$_file"
    [ -f "$DISTDIR/$file" ] && continue
    echo "0 $file"
  done
}

### fetch all ##

###
# do_fetch_all tries to fetch all files from *every* site
###

do_fetch_all()
{
  local DIR
  local file
  local select

  setup_fetch_cmd

  _FAILED_SITES=
  DIR="${DIST_SUBDIR:+$DIST_SUBDIR/}"

  for fileset in MASTER PATCH; do
    case "$fileset" in
    MASTER)
      files="$DISTFILES";;
    PATCH)
      files="$PATCHFILES";;
    *)
      return 1;;
    esac

    for _file in $files; do
      file=`echo "$_file" | $SED -E -e 's/:[^:]+$//'`
      select=`echo "$_file" | $SED -En -e 'y/,/ /; s/.*:([^:]+)$/\1/p'`
      [ -n "$select" ] || select="DEFAULT"

      CKSUM2=`extract_sum "$VALID_ALGORITHMS" "$DIR" "$file"`
      if [ -z "$CKSUM2" ]; then
        $ECHO_MSG ">> No checksum recorded for $DIR$file."
        return 1
      fi
      CKSIZE=`extract_sum 'SIZE' "$DIR" "$file"`

      $ECHO_MSG ">> Verifying availability of file $file."
      MASTER_SITES_TMP=`get_master_sites "$fileset" "$select"`
      for site in $MASTER_SITES_TMP; do
        $ECHO_MSG ">> Attempting to fetch from $site${CKSIZE:+ (${CKSIZE#*=} bytes)}."
        case "$site" in
        *%FILE%*)
          # this expression uses ctrl-A as delimiters
          url=`echo "$site" | $SED -E -e 's/$; s%FILE%'"$file"'g'`;;
        *)
          url="$site$file";;
        esac
        if [ -z "$NO_CHECKSUM" ]; then
          for cksum2 in $CKSUM2; do
            _FETCH_RESULT=`{ if ! $fetch_cmd "$url" "-" "${CKSIZE#*=}"; then \
              echo "FAILED: $$?" >&3; fi | do_hash "${cksum2%%=*}"; } 3>&1`;
            case "$_FETCH_RESULT" in
            FAILED:*)
                $ECHO_MSG ">> Fetch from $site failed."
                _FAILED_SITES="${_FAILED_SITES:+$_FAILED_SITES }$url";;
            *)
              case "${cksum2#*=}" in
              IGNORE)
                $ECHO_MSG ">> Checksum IGNORED for $file.";;
              "$_FETCH_RESULT")
                $ECHO_MSG ">> Checksum OK (${cksum2%%=*}) for $file.";;
              *)
                $ECHO_MSG ">> Checksum mismatch (${cksum2%%=*}) for $file."
                _FAILED_SITES="${_FAILED_SITES:+$_FAILED_SITES }$url";;
              esac;;
            esac
            break
          done
        else
          if _FETCH_RESULT=`$SETENV $FETCH_ENV $FETCH -s "$url"`; then
            if [ -z "$DISABLE_SIZE" -a -n "${CKSIZE#*=}" ]; then
              if [ "${CKSIZE#*=}" = "$_FETCH_RESULT" ]; then
                $ECHO_MSG ">> Size OK (${CKSIZE#*=}) for $file."
              else
                $ECHO_MSG ">> Size mismatch ($_FETCH_RESULT) for $file."
                _FAILED_SITES="${_FAILED_SITES:+$_FAILED_SITES }$url"
              fi
            else
              $ECHO_MSG ">> Found $file."
            fi
          else
            echo ">> Fetch from $site failed."
            _FAILED_SITES="${_FAILED_SITES:+$_FAILED_SITES }$url"
          fi
        fi
      done
    done
  done
  if [ -z "$_FAILED_SITES" ]; then
    echo ">> All files are fetchable."
  else
    echo ">> The following files did not fetch correctly:"
    echo "$_FAILED_SITES" | ${TR} ' ' '\n' | ${SED} -e 's/^/ - /'
  fi
}

###
# do_fetch_list generates a list of files to fetch
###

do_fetch_list()
{
  local _DISTDIR
  local DIR
  local file
  local select

  _DISTDIR="$DISTDIR${DIST_SUBDIR:+/$DIST_SUBDIR}"
  DIR="${DIST_SUBDIR:+$DIST_SUBDIR/}"

  for fileset in MASTER PATCH; do
    case "$fileset" in
    MASTER)
      files="$DISTFILES";;
    PATCH)
      files="$PATCHFILES";;
    *)
      return 1;;
    esac

    for _file in $files; do

      file=`echo "$_file" | $SED -E -e 's/:[^:]+$//'`
      select=`echo "$_file" | $SED -En -e 'y/,/ /; s/.*:([^:]+)$/\1/p'`
      [ -n "$select" ] || select="DEFAULT"

      if [ ! -f "$_DISTDIR/$file" ]; then
        SORTED_MASTER_SITES_TMP=`get_master_sites_sorted "$fileset" "$select"`
        for site in $_MASTER_SITE_OVERRIDE \
            $SORTED_MASTER_SITES_TMP $_MASTER_SITE_BACKUP; do
          case "$site" in
          *%FILE%*)
            # this expression uses ctrl-A as delimiters
            url=`echo "$site" | $SED -E -e 's/$; s%FILE%'"$file"'g'`;;
          *)
            url="$site$file";;
          esac
          if [ -n "$FETCH_ENV" ]; then
            echo -n "$SETENV $FETCH_ENV "
          fi
          echo -n "$FETCH_CMD $FETCH_BEFORE_ARGS" \
            "-o '$file' '$url' $FETCH_AFTER_ARGS || "
        done
        echo "echo '$file not fetched'"
      fi
    done

  done
}

### distinfo ###

###
# make_tmpsum generates a temporary distinfo file
###

make_tmpsum()
{
  local NO_CHECKSUM DISABLE_SIZE

  NO_CHECKSUM=true
  DISABLE_SIZE=true

  do_fetch || return 1

  CKSUMFILES="${CKSUMFILES:-`chksumfiles`}"

  if ! TMPMD5_FILE=`$MKTEMP -q "$MD5_FILE.XXXXXX"`; then
    echo ">> makesum: Can't create temporary checksum file"
    return 1
  fi

  cd "$DISTDIR"
  DIR="${DIST_SUBDIR:+$DIST_SUBDIR/}"

  MY_ALGS="$DISTINFO_ALGORITHMS";
  [ -z "$NO_SIZE" ] && MY_ALGS="$MY_ALGS SIZE"

  # faster, but not the same sort order as the `classic' distinfo
  #for alg in $MY_ALGS; do
  #  do_hash "$alg" `echo "$CKSUMFILES" | $SED -e "s^$DIR"` >> "$TMPMD5_FILE"
  #done

  for _file in $CKSUMFILES; do
    file="$DIR$_file"
    if [ -r "$file" ]; then
      for alg in $MY_ALGS; do
        do_hash "$alg" "$file" >> "$TMPMD5_FILE"
      done
    else
      $ECHO_MSG ">> $file is not in $DISTDIR"
      $RM "$TMPMD5_FILE"
      return 1
    fi
  done
  for _file in $IGNOREFILES; do
    file="$DIR$_file"
    echo "MD5 ($file) = IGNORE" >> "$TMPMD5_FILE"
  done
}

###
# replace_distinfo adds or replaces a distinfo entry in MK_FILE
###

replace_distinfo()
{
  [ $# -eq 1 -a -f "$1" ] || return 1

  if ! TMPMK_FILE=`$MKTEMP -q "$MK_FILE.XXXXXX"`; then
    $ECHO_MSG ">> makesum: Can't create temporary Makefile"
    return 1
  fi
  if ! $AWK -v MD5_FILE="$1" '
    BEGIN { seen=0; ignore=0; first=1 }
    /'"$MK_FILE_ENDMARKER"'/ { seen=NR }
    NR == 1, seen != 0 { print; next }
    !/^#/ { exit 1 }
    /^#:/ { ignore=0 }
    /^#:distinfo'"${DISTINFO_LABEL:+\.$DISTINFO_LABEL}"'$/ {
      if (first)
        while ((getline md5_line < MD5_FILE) > 0) {
          if (first) {
            print "#:distinfo'"${DISTINFO_LABEL:+\.$DISTINFO_LABEL}"'"
            first=0
          }
          print "#" md5_line
        }
      ignore=1
    }
    ignore == 0 { print }
    END {
      if (seen == 0)
        exit 1
      if (first)
        while ((getline md5_line < MD5_FILE) > 0) {
          if (first) {
            print "#:distinfo'"${DISTINFO_LABEL:+\.$DISTINFO_LABEL}"'"
            first=0
          }
          print "#" md5_line
        }
    }
  ' "$MK_FILE" > "$TMPMK_FILE"; then
    $ECHO_MSG ">> Parse error: $MK_FILE, end of file not found"
    $RM "$TMPMK_FILE"
    return 1
  fi
  $CAT "$TMPMK_FILE" > "$MK_FILE"
  $RM "$TMPMK_FILE"
}

###
# replace_distinfo2 adds or replaces a distinfo entry in MK_FILE
###

replace_distinfo2()
{
  [ $# -eq 1 -a -f "$1" ] || return 1

  if [ ! -x "$PERL5" ]; then
    $ECHO_MSG ">> makesum: perl 5 required, please install lang/perl"
    return 1
  fi

  #XXX TODO: honor status of $ECHO_MSG in `die'
  if ! $PERL5 -w -e '
    use strict;
    srand();
    my $marker="##-MARKER_".rand()."-##";
    my $valid_algs=join "|", "SIZE", split " ", $ARGV[3];
    my $distsubdir=$ARGV[1] ? "$ARGV[1]/" : "";
    my $distlabel=$ARGV[2] ? "$ARGV[2]_" : "";
    my %distinfo;

    open DISTINFO, "<$ARGV[0]"
      or die ">> migratesum2: error reading $ARGV[0]\n";
    while(<DISTINFO>) {
      push @{$distinfo{$1}{$2}}, $3
        if /^(\w+) *\($distsubdir(.*)\) *= *([\da-f]+)$/o
    }
    close DISTINFO;
    my $makefile;
    open MAKEFILE, "<$ARGV[4]"
      or die ">> migratesum2: error reading $ARGV[4]\n";
    {
      local $/;
      $makefile = <MAKEFILE>;
    }
    close MAKEFILE;
    my $newinfo;
    foreach my $key (split " ", $ARGV[3]) {
      next
        if !$distinfo{$key};
      my @chksum = ();
      foreach my $file (sort keys %{$distinfo{$key}}) {
        push @chksum, map "$file:$_", @{$distinfo{$key}{$file}};
      }
      $newinfo .= "DISTINFO_$distlabel$key+=\t";
      $newinfo .= join " \\\n\t\t", "", @chksum;
      $newinfo .= "\n";
    }
    $makefile =~ s/^DISTINFO_$distlabel(?:$valid_algs)[+:!?]?=.*(?:\\\n.*)*[^\\\n]\n/$marker\n/mg ||
    $makefile =~ s/^(COMMENT[+:!?]?=.*(?:\\\n.*)*[^\\\n])\n+/$1\n\n$marker\n\n/m ||
    die ">> migratesum2: error parsing $ARGV[4]\n";

    $makefile =~ s/^$marker\n/$newinfo/m;
    $makefile =~ s/^$marker\n//mg;

    open MAKEFILE, ">$ARGV[5]"
      or die ">> migratesum2: error writing $ARGV[4]\n";
    print MAKEFILE $makefile;
    close MAKEFILE;
  ' "$1" "$DIST_SUBDIR" "$DISTINFO_LABEL" "$VALID_ALGORITHMS SIZE" "$MK_FILE" "$MK_FILE"; then
    return 1
  fi
}


###
# do_makesum (re-)calculates distinfo
###

do_makesum()
{
  local algorithms
  algorithms=`echo -n "$VALID_ALGORITHMS SIZE" | $TR -s ' \t\n' '|'`

  make_tmpsum || return 1

  if [ -f "$MD5_FILE" ]; then
    $CAT "$TMPMD5_FILE" > "$MD5_FILE"
  elif $GREP -qs "^#:distinfo${DISTINFO_LABEL:+\.$DISTINFO_LABEL}$" "$MK_FILE"; then
    replace_distinfo "$TMPMD5_FILE"
  elif $GREP -Eqs "^DISTINFO${DISTINFO_LABEL:+_$DISTINFO_LABEL}_($algorithms)[+:!?]?=" "$MK_FILE"; then
    replace_distinfo2 "$TMPMD5_FILE"
  elif [ -s "$TMPMD5_FILE" ]; then
    $CAT "$TMPMD5_FILE" > "$MD5_FILE"
  fi

  $RM "$TMPMD5_FILE"
}

###
# do_migratemakesum integrates distinfo into the port's Makefile
###

do_migratesum()
{
  if [ ! -f "$MD5_FILE" ]; then
    $ECHO_MSG ">> migratesum: no $MD5_FILE"
    return 1
  fi

  if replace_distinfo "$MD5_FILE"; then
    $RM "$MD5_FILE"
    echo ">> Done."
  else
    return 1
  fi
}

###
# do_migratemakesum2 integrates distinfo into the port's Makefile
###

do_migratesum2()
{
  if [ ! -f "$MD5_FILE" ]; then
    $ECHO_MSG ">> migratesum2: no $MD5_FILE"
    return 1
  fi

  if replace_distinfo2 "$MD5_FILE"; then
    $RM "$MD5_FILE"
    echo ">> Done."
  else
    return 1
  fi
}

###
# main
###

opt_fetch_list=false
opt_makesum=false
opt_migratesum=false
opt_migratesum2=false
opt_fetch=false
opt_fetch_all=false
opt_missing_size=false

while getopts "LmMNfFS" opt; do
  case "$opt" in
  L) opt_fetch_list=true;;
  m) opt_makesum=true;;
  M) opt_migratesum=true;;
  N) opt_migratesum2=true;;
  f) opt_fetch=true;;
  F) opt_fetch_all=true;;
  S) opt_missing_size=true;;
  ?) echo "Usage: $0 -LmMfFS"; exit 1;;
  esac
done

shift $(($OPTIND-1))

$opt_fetch_list &&
  { do_fetch_list || exit 1; }

$opt_fetch &&
  { do_fetch || exit 1; }

$opt_makesum &&
  { do_makesum || exit 1; }

$opt_migratesum &&
  { do_migratesum || exit 1; }

$opt_migratesum2 &&
  { do_migratesum2 || exit 1; }

$opt_fetch_all &&
  { do_fetch_all || exit 1; }

$opt_missing_size &&
  { print_missing_size || exit 1; }

exit 0