#!/bin/sh
#fetch routines
# wget fetcher
wget_fetch() {
local qflag
[ ! "$VERBOSE" ] && qflag="-q"
wget $qflag -P $2 $1
}
# local filesystem "fetcher"
cp_fetch() {
local src dest=$2
src=`echo $uri | sed 's|^[a-z]*:/||'`
[ "$VERBOSE" ] && eecho "$src -> $dest"
cp $1 $2
}
# local mount "fetcher"
mnt_fetch() {
local src uri=$1 dest=$2
local wasmounted=`mount | grep "$MNT"`
if [ -z "$wasmounted" ] ; then
mount $MNT || return 1
fi
src=`echo $uri | sed 's|^.*:/|'$MNT'/|'`
[ "$VERBOSE" ] && eecho "$uri -> $dest"
cp $src $dest
[ -z "$wasmounted" ] && umount $MNT
}
# fetcher for rsync
rsync_fetch() {
local vflag
[ "$VERBOSE" ] && vflag="-v"
rsync $vflag $1 $2
}
# look for an executable in PATH
find_exe() {
local i oifs=$IFS
IFS=:
for i in $PATH ; do
IFS=$oifs
[ -x "$i/$1" ] && return
IFS=:
done
IFS=$oifs
return 1
}
# search for mount point in fstab
find_mnt() {
MNT=`awk '{print $2}' /etc/fstab | grep $1`
[ -z "$MNT" ] && return 1
}
fetch() {
is_uri "$1" || die "'$1' is not a valid URI. Please verify APK_PATH and INDEX_URI."
local scheme=`get_uri_scheme $1`
case "$scheme" in
file) fetcher=cp_fetch;;
http) find_exe wget && fetcher=wget_fetch;;
ftp) find_exe wget && fetcher=wget_fetch;;
rsync) find_exe rsync && fetcher=rsync_fetch;;
*) find_mnt "$scheme" && fetcher=mnt_fetch;;
esac
[ -z "$fetcher" ] && die "URI scheme '$scheme' is not supported."
$fetcher $1 $2
}