#!/bin/bash # # Tool to check whether a file is on a distfile mirror, and prefer # that one if it is # # Johannes Winkelmann, jw at tks6 dot net # # USAGE: # # 1. Add the following to pkgmk.conf, after the PKGMK_SOURCE_DIR export # (if you have any): # # if [ "$PKGMK_DOWNLOAD" = "yes" ]; then # DISTFILE_MIRROR=http://localhost:8000 # . /path/to/distfile-mirror-select # fi # # You can remove the "verbose" argument to suppress diagnostic output ### ## user configuration # PING_TIMEOUT=5 WGET_TIMEOUT=5 # OUTPUT_TARGET is changed to stderr if you're running # sf_replace_source with "verbose" as argument" OUTPUT_TARGET=/dev/null distfile_replace_source() { if [ "$REPLACE_SOURCE_VERBOSE" = "yes" ]; then OUTPUT_TARGET=/dev/stderr fi if [ -z "$DISTFILE_MIRROR" ]; then echo "DISTFILE_MIRROR variable not set; mirroring inactive" \ > /dev/stderr return fi i=0 for s in ${source[*]}; do if [ -f $PKGMK_SOURCE_DIR/`basename $s` ]; then echo "File already downloaded: `basename $s`" > $OUTPUT_TARGET continue fi file=$(basename $s) mirror=$DISTFILE_MIRROR if [ "$file" != "$s" ]; then wget -q --timeout=$WGET_TIMEOUT --spider $mirror/$file if [ $? -eq 0 ]; then echo "$s -> $mirror/$file" > $OUTPUT_TARGET source[$i]="$mirror/$file" fi fi i=$((i+1)) done } distfile_replace_source