[go: up one dir, main page]

Menu

[r170]: / trunk / apk_fetch  Maximize  Restore  History

Download this file

110 lines (93 with data), 2.5 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 ""
	echo "  -f  Download a package even if recorded as installed."
	echo "  -h  Show help and exit."
	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 "fhlqruv" opt ; do
	case "$opt" in 
		f)	fflag="-f" ;;
		h)	usage;;
		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" ] && [ -z "$lflag" ] && usage

[ "$lflag" ] && index_list


# 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
			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
			$PROGRAM $fflag $qflag $rflag $vflag $deplist
		fi
	fi

	shift
done

exit 0