#!/bin/sh
# apk functions
# if the config file exist, read it
[ -f /etc/apk.conf ] && . /etc/apk.conf
# Set some defaults
ROOT=${ROOT-/}
PKGMETA="var/lib/apkdb"
APKDB=`echo "${ROOT}/${PKGMETA}" | sed 's://:/:g'`
OLDPWD=`pwd`
# set apklibs if not alread set
[ -z "$APKLIBS" ] && APKLIBS="/lib/apk"
#listfile=${APKDB}/${pkg}.list
#sha1file=${APKDB}/${pkg}.sha1
#metaball="${APKDB}/${pkg}.meta.tar.gz"
###########################################################
# error echo. echo to stderr
eecho() {
echo $* >&2
}
beautify_path() {
echo $1 | sed 's:/\./::g; s://:/:g'
}
###########################################################
# create sha1 sum of package files
create_sha1sum() {
local sha1file listfile opwd
opwd=`pwd`
listfile=${APKDB}/${1}.list
sha1file=${APKDB}/${1}.sha1
cd ${ROOT}
rm -f ${sha1file}
while read i ; do
[ -f ${i} ] && sha1sum ${i} >> ${sha1file}
done < ${listfile}
cd $opwd
}
###########################################################
# Create sha1 sum of config files
create_config_sha1sum() {
local sha1file listfile opwd
opwd=`pwd`
listfile=${APKDB}/${1}.config
# if there is no config file, just return
[ ! -f $listfile ] && return
sha1file=${APKDB}/${1}.cfg.sha1
cd ${ROOT}
rm -f ${sha1file}
while read i ; do
[ -f ${i} ] && sha1sum ${i} >> ${sha1file}
done < ${listfile}
cd $opwd
}
###########################################################
# Get description
get_description() {
local opwd
opwd=`pwd`
local pkgv
cd ${APKDB}
pkgv=$1
tar -zxf ${pkgv}.meta.tar.gz -O ${pkgv}.control \
| grep ^Description | sed 's/^.*: //'
cd $opwd
}
###########################################################
# all installed packages with description
all_installed_with_desc() {
local opwd
opwd=`pwd`
cd ${APKDB}
for i in *.meta.tar.gz ; do
pkgv=`basename $i .meta.tar.gz`
echo -n -e "$pkgv\t"
echo `get_description ${pkgv}`
done
cd $opwd
}
##########################################################
# All installed pacages
all_installed() {
local opwd pkgv i
opwd=`pwd`
cd ${APKDB}
for i in *.meta.tar.gz ; do
pkgv=`basename $i .meta.tar.gz`
echo "$pkgv"
done
cd $opwd
}
##########################################################
# is package installed?
is_installed() {
local pkg=$1
all_installed | grep '^${pkg}-[0-9].*'
}
###########################################################
# Display the Control Meta file
get_control() {
local pkgv opwd
opwd=`pwd`
cd ${APKDB}
pkgv=$1
tar -zxf ${pkgv}.meta.tar.gz -O ${pkgv}.control
cd $opwd
}
###########################################################
# get the package name and versions
get_installed_pkgv() {
local opwd metafile
opwd=`pwd`
cd ${APKDB}
# possible bug: if there are more than one version installed,
# we might get problem here...
metafile=`ls ${1}[\-0-9]*.meta.tar.gz 2>/dev/null`
# if metafile dont exist, return empty
[ -z $metafile ] && return
echo `basename $metafile .meta.tar.gz`
cd $opwd
}
###########################################################
decompress_meta() {
local metafile
metafile=${APKDB}/${1}.meta.tar.gz
if [ ! -f $metafile ] ; then
eecho "$1 does not seem to be installed. Did you specify version? Aborting."
exit 1
fi
tar -C $APKDB -zxf $metafile
}
############################################################
compress_meta() {
local opwd metaball pkgv1
opwd=`pwd`
pkgv=$1
metaball="${APKDB}/${pkgv}.meta.tar.gz"
cd $APKDB
rm -f ${metaball}
metas=`ls ${pkgv}.*`
if [ -z "$metas" ] ; then
eecho "No meta files were found. Aborting."
exit 1
fi
tar -czf ${metaball} ${metas}
rm ${metas}
cd $opwd
}
############################################################
pkg_listfiles() {
local opwd pkgv
opwd=`pwd`
pkgv=$1
cd ${APKDB}
tar -zxf ${pkgv}.meta.tar.gz -O ${pkgv}.list
cd $opwd
}
############################################################
pkg_check() {
local pkgv opwd
opwd=`pwd`
pkgv=$1
decompress_meta $pkgv
cd ${ROOT}
sha1sum -c ${APKDB}/${pkgv}.sha1
compress_meta $pkgv
cd $opwd
}
############################################################
# funcs for mounting/umounting local media
# note that is a Method is to be classified as "local media" it has to be
# specified in /etc/fstab and have a mount point in /media/
# mounts a "cdrom:/pkgs/file" style URI and echos the "/media/cdrom/pkgs/file"
get_media_type() {
echo $1 | sed 's|:/.*||'
}
############################################################
# this func should not be called in a subshell since it sets
# some global vars.
# ideas on how to solve this better is warmly welcome. /NC
media_mount() {
MEDIA=`get_media_type $1`
MEDIA_WAS_MOUNTED=`mount | grep /media/$MEDIA`
if [ -z "$MEDIA_WAS_MOUNTED" ] ; then
mount "/media/$MEDIA" || exit 1
fi
}
############################################################
# this function depends on global var MEDIA
get_mounted_path() {
echo $1 | sed 's|^.*:/|/media/'$MEDIA'/|'
}
############################################################
# this function depends on globals MEDIA and MEDIA_WAS_MOUNTED
media_umount() {
if [ -z "$MEDIA_WAS_MOUNTED" ] ; then
umount "/media/$MEDIA" || exit 1
fi
}
############################################################
# This func examine the APK_PATH variable and loads the repository module
load_repmod() {
local reptype repmod
# just check for debugging
if [ $rflag != "on" ] ; then
eecho "-r was not specified. Contact developer of apk-tools."
exit 1
fi
if [ -z "$APK_PATH" ] ; then
eecho "Please set the APK_PATH variable."
exit 1
fi
reptype=`echo $APK_PATH | cut -f 1 -d ':'`
repmod=$APKLIBS/$reptype.repmod
if [ -f $repmod ] ; then
. $repmod
else
eecho "Repository type $reptype is not supported. Sorry..."
exit 1
fi
}
###################################################3
# get dependencies from a .apk file
get_depend() {
local pkgf opwd i ctlfile
opwd=`pwd`
pkgf=$1
pkgv=`basename $1 .apk`
for i in `tar -zxf ${pkgf} -O ./${PKGMETA}/${pkgv}.control | grep ^Depend: | cut -d : -f 2` ; do
echo $i | grep 'virtual' > /dev/null && eecho "Warning: virtual dependency: $i"
echo $i | sed 's:^.*/::' | sed 's/-[0-9].*$//'
done
}