#!/bin/sh
|
|
# --- SDE-COPYRIGHT-NOTE-BEGIN ---
|
|
# This copyright note is auto-generated by ./scripts/Create-CopyPatch.
|
|
#
|
|
# Filename: bin/sde-download2
|
|
# Copyright (C) 2007 - 2008 The OpenSDE Project
|
|
#
|
|
# More information can be found in the files COPYING and README.
|
|
#
|
|
# This program 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; version 2 of the License. A copy of the
|
|
# GNU General Public License can be found in the file COPYING.
|
|
# --- SDE-COPYRIGHT-NOTE-END ---
|
|
|
|
#Description: Download sources
|
|
|
|
[ -n "$SDEROOT" ] ||
|
|
export SDEROOT=$( cd "${0%/*}/.."; pwd -P )
|
|
|
|
. $SDEROOT/lib/libsde.in
|
|
|
|
download_usage() {
|
|
local progname=${0##*/}
|
|
cat <<EOT >&2
|
|
Usage: $progname [DISCRIMINATOR] [OPTIONS] [ITEMS...]
|
|
|
|
Accepted discriminators:
|
|
* config (cfg)
|
|
* package (pkg) - default
|
|
* repository (repo)
|
|
|
|
For more information read \`sde help download\`.
|
|
EOT
|
|
}
|
|
|
|
# detect discriminator
|
|
discriminator=package
|
|
if [ $# -gt 0 ]; then
|
|
case "$1" in
|
|
-*) ;;
|
|
config|cfg)
|
|
discriminator=config; shift ;;
|
|
package|pkg)
|
|
discriminator=package; shift ;;
|
|
repository|repo)
|
|
discriminator=repository; shift ;;
|
|
*)
|
|
echo_error "$1: invalid discriminator"
|
|
download_usage
|
|
exit -1 ;;
|
|
esac
|
|
fi
|
|
|
|
shortopts='nqvc:m:e'
|
|
longopts='dry-run,quiet,verbose,cfg,timeout:,cfg,check:,mirror:,alt-dir:,mode:,extenders'
|
|
options=$( getopt -o "$shortopts" -l "$longopts" -- "$@" )
|
|
|
|
if [ $? -ne 0 ]; then
|
|
download_usage
|
|
exit -1
|
|
fi
|
|
|
|
# load new arguments list
|
|
eval set -- "$options"
|
|
|
|
dryrun=
|
|
verbose=1
|
|
timeout=normal
|
|
check=always
|
|
mirror=
|
|
altdir=
|
|
mode=hardlink
|
|
extenders=
|
|
config=
|
|
|
|
# load global settings
|
|
eval $( $SDEROOT/bin/sde-config-ini --file=$SDESETTINGS download )
|
|
eval $( $SDEROOT/bin/sde-config-ini --file=$SDESETTINGS download-$sdever )
|
|
|
|
while [ $# -gt 0 ]; do
|
|
case "$1" in
|
|
--) shift; break ;;
|
|
|
|
-n|--dry-run)
|
|
dryrun=yes ;;
|
|
-e|--extenders)
|
|
extenders=yes ;;
|
|
-v|--verbose)
|
|
let verbose++ ;;
|
|
-q|--quiet)
|
|
let verbose-- ;;
|
|
|
|
--timeout)
|
|
shift
|
|
case "$1" in
|
|
normal|long|no)
|
|
timeout="$1" ;;
|
|
*)
|
|
echo_abort 1 "$1: invalid timeout value: [normal*|long|no]"
|
|
;;
|
|
esac ;;
|
|
--check)
|
|
shift
|
|
case "$1" in
|
|
always|download|never)
|
|
check="$1" ;;
|
|
*)
|
|
echo_abort 1 "$1: invalid ckeck value: [always*|download|never]"
|
|
;;
|
|
esac ;;
|
|
--mirror)
|
|
shift
|
|
case "$1" in
|
|
none|auto|http://*|https://*|ftp://*|file://*)
|
|
mirror="$1" ;;
|
|
*)
|
|
echo_abort 1 "$1: invalid mirror value"
|
|
;;
|
|
esac ;;
|
|
--alt-dir)
|
|
shift
|
|
if [ "$1" = "none" -o -z "$1" ]; then
|
|
altdir=
|
|
$SDEROOT/bin/sde-config-ini --file=$SDESETTINGS --delete "download.altdir"
|
|
elif [ -d "$1/" ]; then
|
|
altdir="$( cd "$1"; pwd -P )"
|
|
$SDEROOT/bin/sde-config-ini --file=$SDESETTINGS "download.altdir=$altdir"
|
|
else
|
|
echo_abort 1 "$1: directory not found."
|
|
fi ;;
|
|
-m|--mode)
|
|
shift
|
|
case "$1" in
|
|
hardlink|copy|move)
|
|
mode="$1" ;;
|
|
*)
|
|
echo_abort 1 "$1: invalid altdir mode [hardlink*|copy|move]"
|
|
;;
|
|
esac ;;
|
|
-c|--cfg)
|
|
shift
|
|
config=${1:-default}
|
|
;;
|
|
esac
|
|
shift
|
|
done
|
|
|
|
# verbose messages
|
|
if [ $verbose -gt 1 -a -d "$altdir" ]; then
|
|
echo_info "Using '$altdir' to look for files locally."
|
|
fi
|
|
|
|
# constants to pass to sde-download-get
|
|
downloadgetopt=
|
|
if [ $verbose -gt 2 ]; then
|
|
downloadgetopt='-v'
|
|
elif [ $verbose -lt 0 ]; then
|
|
downloadgetopt='-q'
|
|
fi
|
|
if [ -n "$dryrun" ]; then
|
|
downloadgetopt="$downloadgetopt --dry-run"
|
|
fi
|
|
|
|
case "$discriminator" in
|
|
package)
|
|
$SDEROOT/bin/sde-list-download ${config:+--cfg $config} ${extenders:+--extenders} -pkl -- "$@"
|
|
;;
|
|
repository)
|
|
$SDEROOT/bin/sde-list-download ${config:+--cfg $config} ${extenders:+--extenders} -rpkl -- "$@"
|
|
;;
|
|
config)
|
|
[ -z "$config" ] || set -- "$config" "$@"
|
|
|
|
[ $# -gt 0 ] || set -- default
|
|
|
|
for config; do
|
|
$SDEROOT/bin/sde-list-download --cfg "$config" -pkl
|
|
done
|
|
;;
|
|
esac | while read pkg cksum file location; do
|
|
# temporal download location
|
|
tmpfile="$SDEROOT/tmp/down.$( echo $file | tr '/' '-' )"
|
|
mkdir -p "$SDEROOT/tmp"
|
|
[ $? -eq 0 ] || exit -1
|
|
|
|
if [ -s "$SDEROOT/$file" ]; then
|
|
:
|
|
elif [ -e "$tmpfile.lock" ]; then
|
|
echo_warning "$file: skipping."
|
|
else
|
|
check_this=yes
|
|
|
|
# look on $altdir, to save time, bandwidth and diskspace
|
|
#
|
|
if [ -n "$altdir" ]; then
|
|
altfile=$(find $altdir/ -name ${file##*/} | head -n 1)
|
|
|
|
if [ -r "$altfile" ]; then
|
|
# a file was found, what should I do with it?
|
|
[ $verbose -le 0 ] || echo_info "$pkg:${file##*/} found in $altfile ($mode)."
|
|
|
|
[ "$check" = "always" ] || check_this=no
|
|
|
|
case "$mode" in
|
|
hardlink) cp -lf "$altfile" "$tmpfile" ;;
|
|
copy) cp -f "$altfile" "$tmpfile" ;;
|
|
move) mv -f "$altfile" "$tmpfile" ;;
|
|
esac
|
|
fi
|
|
fi
|
|
|
|
# try to download the file
|
|
#
|
|
if [ ! -s "$tmpfile" ]; then
|
|
|
|
[ "$check" != "never" ] || check_this=no
|
|
|
|
# verbose message
|
|
if [ $verbose -le 0 ]; then
|
|
:
|
|
elif [ $verbose -le 1 ]; then
|
|
echo_info "Downloading $pkg:$file"
|
|
else
|
|
echo_info "Downloading $pkg:$file ($cksum) $location"
|
|
fi
|
|
|
|
"$SDEROOT"/bin/sde-download-get $downloadgetopt -- "$tmpfile" $location
|
|
if [ $? -ne 0 ]; then
|
|
echo_warning "$file: failed to download"
|
|
rm -f "$tmpfile"
|
|
fi
|
|
fi
|
|
|
|
# validate the file against the known checksum
|
|
#
|
|
if [ -s "$tmpfile" -a "$check_this" = "yes" -a "$cksum" != "X" ]; then
|
|
cksum_real=$( sh $SDEROOT/lib/sde-download/cksum.sh $tmpfile | cut -d' ' -f2 )
|
|
if [ "$cksum" = "0" ]; then
|
|
echo_warning "$pkg:${file##*/} is missing a checksum value, ignoring. ($cksum_real)"
|
|
elif [ "$cksum" != "$cksum_real" ]; then
|
|
echo_error "$pkg:${file##*/} has a wrong checksum, failing. ($cksum != $cksum_real)"
|
|
mv -f "$tmpfile" "$tmpfile.cksum-err"
|
|
elif [ $verbose -gt 0 ]; then
|
|
echo_info "$pkg:${file##*/} is correct."
|
|
fi
|
|
fi
|
|
|
|
# and finally accept the file
|
|
#
|
|
if [ -s "$tmpfile" ]; then
|
|
mkdir -p "${file%/*}"
|
|
mv -f "${tmpfile}" "$file"
|
|
fi
|
|
|
|
fi
|
|
done
|
|
|