[go: up one dir, main page]

Menu

[r199]: / trunk / apk_fetch  Maximize  Restore  History

Download this file

119 lines (100 with data), 2.9 kB

#!/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 [-fhrv] {apkname|URI} ..."
	echo "       $PROGRAM -L | -l"
	echo ""
	echo "  -f  Download a package even if recorded as installed."
	echo "  -h  Show help and exit."
	echo "  -L  List available packages in repository index. Include version."
	echo "  -l  List available packages in repository index."
	echo "  -q  Quiet mode. Supress non-error messages."
	echo "  -R  Download the packages required by the given packages as well."
	echo "  -u  Update the package repository index."
	echo "  -v  Turn on verbose output."
	echo ""
	echo "apkname is a full pkgfilename, pkgname, a pkgname w/o verion or a full URI"
	echo ""
	echo "Packages are stored in $PACKAGES"
	echo ""
	exit 1
}

#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" ;;
		l)	lflag="-l" ;;
		q)	qflag="-q" ; QUIET="-q" ;;
		R)	Rflag="-R" ;;
		u)	uflag="-u" 
			index_update || eecho "Failed to update repository index"
			;; 
		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$Lflag" ] && usage

[ "$Lflag" ] && index_list
[ "$lflag" ] && index_list | sed 's/-[0-9].*//'



# if -f is not set we are going to query the database
[ -z "$fflag" ] && db_init

while [ $# -gt 0 ] ; do
	p="$1"

	# check if package is installed (and skip if it is) unless -f flag set
	if [ -z "$fflag" ] ; then
		if [ "`db_get_installed_pkgv $p`" ] ; then
			shift
			continue
		fi
	fi
	
	# find the full URI and pathname to downloaded package
	uri="`index_get_uri \"$p\"`"
	[ $? -ne 0 ] && die "$p not found in '$CACHED_INDEX'."
	pkgfpath="$PACKAGES/`basename \"$uri\"`"

	# download if not already there
	if [ ! -f "$pkgfpath" ] ; then
		fetch "$uri" "$PACKAGES" || die "Failed to fetch '$uri'."
	fi
	
	# 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

			apk=`index_get_pkgf $i`
			[ -z "$apk" ] && die "Failed to find dependency '$i'."
			# add to dependency list if the file is not there
			[ -f "$PACKAGES/$apk" ] || deplist="$deplist
\"`dirname \"$uri\"`/$apk\""
		done
		if [ "$deplist" ] ; then
			echo "$deplist" \
				| xargs $PROGRAM $fflag $qflag $Rflag $vflag || die "Failed to fetch dependency."
		fi
	fi

	shift
done

exit 0