apk-tools Code
Brought to you by:
ncopa
#!/bin/sh
#
# apk_fetch - a remote package fetcher
#
# Copyright (c) 2005 Natanael Copa
#
# Distributed under GPL-2
#
PROGRAM=apk_fetch
#load the libraries
. "${APK_LIBS:=/lib/apk}/libutil.sh"
. "$APK_LIBS/libfetch.sh"
. "$APK_LIBS/libindex.sh"
. "$APK_LIBS/libdb.sh"
. "$APK_LIBS/libpkgf.sh"
# print usage and die
usage() {
echo "$PROGRAM $VERSION"
echo "
usage: $PROGRAM [-fhqRuv] {apkname|URI} ...
$PROGRAM -l
-f Download a package even if recorded as installed.
-h Show help and exit.
-l List available packages in repository index.
-q Quiet mode. Supress non-error messages.
-R Download the packages required by the given packages as well.
-u Update the package repository index.
-v Turn on verbose output.
apkname is a full pkgfilename, pkgname, a pkgname w/o verion or a full URI
Packages are stored in $PACKAGES
"
exit 1
}
# fetch one
do_fetch() {
local p="$1"
# check if package is installed (and skip if it is) unless -f flag set
if [ -z "$fflag" ] && [ "`db_get_installed_pkgv $p`" ] ; then
return
fi
# if its an apk file, make sure dependencies are taken from same place
is_apk "$p" && insert_apk_path "${p%/*}" # `dirname "$p"`
# find the pathname to downloaded package
pkgf="`index_get_pkgf \"$p\"`"
[ $? -ne 0 ] && die "$p not found in '$CACHED_INDEX'."
pkgfpath="$PACKAGES/$pkgf"
# download if not already there
if [ ! -f "$pkgfpath" ] ; then
if is_uri "$p" ; then
fetch "$p" "$PACKAGES" || die "Failed to fetch '$p'."
else
fetch_mirror "$pkgf" "$PACKAGES" || die "Failed to fetch '$p'."
fi
fi
# if it was an uri, the insert the dirname in path so dependencies
# will be downloaded from same uri if possible.
is_uri "$p" && insert_apk_path "${p%/*}" # `dirname "$p"`
# find and get dependencies if we are in recursive mode
if [ "$Rflag" ] ; then
deps=`pkgf_get_deps "$pkgfpath"`
unset deplist
for i in $deps ; do
[ "`db_get_installed_pkgv $i`" ] && [ -z "$fflag" ] && continue
pv=`index_get_pkgv $i`
apk=$pk.apk
[ -z "$pv" ] && die "Failed to find dependency '$i'."
# add to dependency list if the file is not there
[ -f "$PACKAGES/$apk" ] || deplist="$deplist $pv"
done
if [ "$deplist" ] ; then
APK_PATH="$APK_PATH" $PROGRAM $fflag $qflag $Rflag $vflag $deplist \
|| die "Failed to fetch dependency."
fi
fi
}
#parse args
unset vflag
unset Rflag
unset qflag
unset fflag
while getopts "fhLlqRuv" opt ; do
case "$opt" in
f) fflag="-f" ;;
h) usage;;
l) lflag="-l" ;;
q) qflag="-q" ; QUIET="-q" ;;
R) Rflag="-R" ;;
u) uflag="-u"
;;
v) vflag="-v"; VERBOSE="-v" ;;
\?) usage;;
esac
done
shift `expr $OPTIND - 1`
# if no apkname and no -u specified we show help and exit
[ $# -eq 0 ] && [ -z "$uflag$lflag" ] && usage
if [ "$uflag" ] ; then
index_update || eecho "Failed to update repository index"
fi
if [ "$lflag" ]; then
if [ "$VERBOSE" ] ; then
index_list
else
index_list | sed 's/-[0-9].*//'
fi
fi
# if -f is not set we are going to query the database
[ -z "$fflag" ] && db_init
while [ $# -gt 0 ] ; do
do_fetch "$1"
shift
done
exit 0