#!/bin/sh
# libutil.sh - Utility functions
#
# Copyright(c) 2005 Natanael Copa
#
# Distributed under GPL-2
#
VERSION=0.9
# echo to stderr
eecho() {
echo $* >&2
}
# echo to stderr and die
die() {
echo -n "$PROGRAM: " >&2
eecho $*
exit 1
}
die_unless_force() {
echo "$PROGRAM: $*" >&2
[ -z "$FORCE" ] && exit 1
}
# remove double / and ./ in pathnames
beautify_path() {
echo "$1" | sed 's:/^[^\.]\./::g; s:/\{2,\}:/:g; s:/\./:/:g'
}
# check if parameter is an uri or not
is_uri() {
echo "$1" | grep "^[a-z+]*:/" >/dev/null
}
# check if parameter is an apk package (contains a / or .apk at the end)
is_apk() {
echo "$1" | grep '/' >/dev/null && return 0
echo "$1" | grep ".apk$" >/dev/null
}
# check if parameter has version number (i.e. if it is an pkgv or pkg)
has_version() {
echo "$1" | grep -- '-[0-9].*' >/dev/null
}
# check if parameter has some kind of wildcard
has_wildcard() {
echo "$1" | grep "[\*\?\[]" >/dev/null
}
# get the scheme for an uri (echo everything before the first ':')
get_uri_scheme() {
echo "$1" | cut -d : -f 1
}
# append APK_PATH if parameter is not an uri
mk_uri() {
if is_uri "$1" ; then
echo "$1"
else
echo "$APK_PATH/$1"
fi
}
# remove version number from package name
rm_ver() {
echo "$1" | sed 's/-[0-9].*//'
}
# initialize a temp directory
# $1 contains the variable name for the directory
# the directory will automatically be deleted upon exit
init_tmpdir() {
local omask=`umask`
local __tmpd="$APK_TMPDIR/$PROGRAM-${$}-`date +%s`"
umask 077 || die "umask"
mkdir "$__tmpd" || exit 1
trap "rm -fr \"$__tmpd\"; exit" 0
umask $omask
eval "$1=\"$__tmpd\""
}
# remove files and empty dirs in specified list.
# environment:
# ROOT: all files are relative this path
# VERBOSE: echo filenames to stdout
# DRYRUN: don't delete anything, just simulate
list_uninstall() {
local f p
local root=${ROOT:-"/"}
sort -r "$1" | while read f ; do
p="`beautify_path \"$root/$f\"`"
if [ "$DRYRUN" ] ; then
[ "$VERBOSE" ] && echo "$p"
else
if [ -d "$p" ] ; then
# try to remove dir, but ignore errors. It might
rmdir "$p" 2>/dev/null && [ "$VERBOSE" ] && echo "$p"
else
rm "$p" && [ "$VERBOSE" ] && echo "$p"
fi
fi
done
}
# list all lines that occur in first list but not second
# the files cannot contain duplicate lines.
list_subtract() {
(
# first we find all uniq lines
cat "$1" "$2" | sort | uniq -u
# then we combine uniq lines with first file ...
cat "$1"
# ...and find all duplicates. Those only exist in first file
) | sort | uniq -d
}
# assign a value to a global var, either from environment or
# from configuraion file
# usage: get_var VARIBALE_NAME DEFAULT_VALUE
get_var() {
local var
# first we check if the envvar is set
eval "var=\$$1"
if [ "$var" ] ; then
echo "$var"
elif [ -f ${APKTOOLS_CONF:="$ROOT/etc/apk.conf"} ] ; then
# then we check the conf file
var=`awk -F = '/^'$1'=/ { print $2 }' "$APKTOOLS_CONF"`
if [ "$var" ] ; then
echo "$var"
else
# else we use the default
echo "$2"
fi
else
# no conf file found use default
echo "$2"
fi
}
###########################################################
# dump global variables
dump_env() {
echo "ROOT=$ROOT"
echo "APKTOOLS_CONF=$APKTOOLS_CONF"
echo "APK_PATH=$APK_PATH"
echo "APK_DBDIR=$APK_DBDIR"
echo "APK_TMPDIR=$APK_TMPDIR"
echo "APK_FETCH=$APK_FETCH"
echo "APK_CACHE=$APK_CACHE"
echo "APK_CACHELEVEL=$APK_CACHELEVEL"
echo "APK_LIBS=$APK_LIBS"
echo "PACKAGES=$PACKAGES"
echo "APKDB=$APKDB"
echo "APK_NOCOMPRESS=$APK_NOCOMPRESS"
echo "REP_DIR=$REP_DIR"
echo "REP_SCHEME=$REP_SCHEME"
echo "INDEX_URI=$INDEX_URI"
echo "CACHED_INDEX=$CACHED_INDEX"
}
#############################################################################
# init_globals sets up the global variables
ROOT="`get_var ROOT /`"
echo "$ROOT" | grep -v "^/" > /dev/null && ROOT="$PWD/$ROOT"
APKTOOLS_CONF="`get_var APKTOOLS_CONF \"$(beautify_path /etc/apk.conf)\"`"
APK_PATH=`get_var APK_PATH ""`
APK_DBDIR="`get_var APK_DBDIR \"$(beautify_path \"$ROOT/var/db/apk\")\"`"
APK_DBDIR_IN_PKG="`get_var APK_DBDIR_IN_PKG ./var/db/apk`"
APK_TMPDIR="`get_var \"APK_TMPDIR\" /tmp`"
APK_CACHE="`get_var APK_CACHE \"$(beautify_path \"$ROOT/var/lib/apk\")\"`"
APK_KEEPCACHE="`get_var APK_KEEPCACHE no`"
APK_LIBS="`get_var APK_LIBS /lib/apk`"
PACKAGES="`get_var PACKAGES \"$(beautify_path \"$ROOT/var/cache/packages\")\"`"
APKDB="`beautify_path \"$APK_DBDIR\"`"
APK_NOCOMPRESS=`get_var APK_NOCOMPRESS ""`
#INDEX="Packages.sha1.gz"
INDEX="INDEX.md5.gz"
CACHED_INDEX="$APK_CACHE/$INDEX"
INDEX_URI="`get_var INDEX_URI \"$APK_PATH/$INDEX\"`"
APK_SUM=`get_var APK_SUM md5`
APK_MKSUM=`get_var APK_MKSUM "${APK_SUM}sum"`
APK_CHKSUM=`get_var APK_CHKSUM "${APK_SUM}sum -c"`