#!/bin/sh
META_SUFFIX=mtgz
# Utility functions
###########################################################
# error echo. echo to stderr
eecho() {
echo $* >&2
}
###########################################################
# remove double /'s in pathnames
beautify_path() {
echo $1 | sed 's:/\./::g; s:/\{2,\}:/:g'
}
###########################################################
# print error message and exit with error
die() {
eecho $*
exit 1
}
###########################################################
# 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"
else
# then we check the conf file
var=`awk -F = '/^'$1'=/ { print $2 }' ${APKTOOLS_CONF:="/etc/apk.conf"}`
if [ "$var" ] ; then
echo "$var"
else
# else we use the default
echo $2
fi
fi
}
# remove version number from package name
# pkgv2pkg
rm_ver() {
echo $1 | sed 's/-[0-9].*//'
}
#############################################################################
#
# init_globals sets up the global variables
init_globals() {
ROOT=`get_var ROOT /`
APKTOOLS_CONF=`get_var APKTOOLS_CONF /etc/apk.conf`
APK_PATH=`get_var APK_PATH ""`
APK_DBDIR=`get_var APK_DBDIR /var/db/pkg`
APK_TMPDIR=`get_var APK_TMPDIR /tmp`
APK_FETCH=`get_var APK_FETCH wget`
APK_CACHE=`get_var APK_CACHE /var/cache/apk`
APK_CACHELEVEL=`get_var APK_CACHELEVEL /etc/apk.conf`
APK_LIBS=`get_var APK_LIBS /lib/apk`
PACKAGES=`get_var PACKAGES "$APK_CACHE"`
APKDB=`beautify_path "$ROOT/$APK_DBDIR"`
REP_DIR=`beautify_path $( echo "$APK_PATH" | cut -d : -f 2)`
REP_SCHEME=`echo "$APK_PATH" | cut -d : -f 1`
INDEX="Index.tar.gz"
INDEX_CACHE="$APK_CACHE/$INDEX"
INDEX_URI="$APK_PATH/$INDEX"
OLDPWD=`pwd`
}
###########################################################
# 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 "REP_DIR=$REP_DIR"
echo "REP_SCHEME=$REP_SCHEME"
echo "INDEX_URI=$INDEX_URI"
echo "INDEX_CACHE=$INDEX_CACHE"
}
###########################################################
# create sha1 sum of package files
# Needs the meta to be in $APK_TMPDIR
create_sha1sum() {
local sha1file listfile opwd
opwd=`pwd`
listfile=${APK_TMPDIR}/${1}/CONTENTS
sha1file=${APK_TMPDIR}/${1}/SHA1
cd $ROOT
rm -f ${sha1file}
while read i ; do
[ -f ${i} ] && sha1sum ${i} >> ${sha1file}
done < ${listfile}
cd $opwd
}
###########################################################
# Create sha1 sum of backup files
create_backup_sha1sum() {
local sha1file listfile opwd
opwd=`pwd`
listfile=${APK_TMPDIR}/${1}/BACKUPFILES
# 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
db_get_description() {
local opwd
opwd=`pwd`
local pkgv
cd ${APKDB}
pkgv=$1
tar -zxf ${pkgv}.$META_SUFFIX -O ${pkgv}/DESC
cd $opwd
}
###########################################################
# all installed packages with description
db_all_installed_with_desc() {
local opwd
opwd=`pwd`
cd ${APKDB}
for i in *.$META_SUFFIX ; do
pkgv=`basename $i .$META_SUFFIX`
echo -n -e "$pkgv\t"
echo `db_get_description ${pkgv}`
done
cd $opwd
}
##########################################################
# All installed pacages
db_all_installed() {
local opwd pkgv i
opwd=`pwd`
cd ${APKDB}
for i in *.$META_SUFFIX ; do
basename $i .$META_SUFFIX
done
cd $opwd
}
##########################################################
# is package installed?
# returns pkgv or nothing if pkg is not installed
db_get_installed_pkgv() {
local meta
db_all_installed | grep '^${pkg}-[0-9].*'
}
###########################################################
# get the package name and versions
db_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_SUFFIX 2>/dev/null`
# if metafile dont exist, return empty
[ "$metafile" ] && basename $metafile .$META_SUFFIX
cd $opwd
}
###########################################################
db_decompress_meta() {
local metafile
metafile=${APKDB}/${1}.$META_SUFFIX
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
}
############################################################
db_compress_meta() {
local opwd metaball pkgv1
opwd=`pwd`
pkgv=$1
metaball="${APKDB}/${pkgv}.$META_SUFFIX"
cd $APKDB
rm -f ${metaball}
metas=${pkgv}
if [ -z "$metas" ] ; then
eecho "No meta files were found. Aborting."
exit 1
fi
tar -czf ${metaball} ${metas}
rm -rf ${metas}
cd $opwd
}
############################################################
db_pkgv_metafile() {
local opwd pkgv
opwd=`pwd`
pkgv=$1
cd ${APKDB}
tar -zxf ${pkgv}.$META_SUFFIX -O ${pkgv}/$2
cd $opwd
}
db_pkgv_listfiles() {
db_pkgv_metafile $1 CONTENTS
}
db_pkgv_backupfiles() {
db_pkgv_metafile $1 BACKUPFILES
}
############################################################
# 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
}
rep_init() {
true
}
rep_fetch() {
true
}
rep_cache_fetch() {
true
}
rep_cleanup() {
true
}
############################################################
# This func examine the APK_PATH variable and loads the repository module
load_repmod() {
local reptype repmod
[ -z "$APK_PATH" ] && die "Please set the APK_PATH variable."
repmod=$APK_LIBS/$REP_SCHEME.repmod
if [ -f $repmod ] ; then
. $repmod
else
die "Repository type $REP_SCHEME is not supported. Sorry..."
fi
}
# Init the cached index.
# fetch from repository if zero length or not existing.
# sets the global variable INDEX_CACHE
init_index_cache() {
if [ -f "$INDEX_CACHE" ] ; then
if [ `ls -s $INDEX_CACHE | awk '{print $1}'` -lt 1 ] ; then
rm $INDEX_CACHE
unset INDEX_CACHE
fi
else
unset INDEX_CACHE
fi
[ -z "$INDEX_CACHE" ] && INDEX_CACHE=`rep_cache_fetch $INDEX_URI`
[ -z "$INDEX_CACHE" ] && die "Failed to download $INDEX_URI"
}
clean_cache() {
case "$APK_CACHELEVEL" in
0) # remove archives and index
rm -f $INDEX_CACHE/*.apk $INDEX_CACHE/$INDEX
;;
1) # remove archives. Keep index.
rm -f $INDEX_CACHE/*.apk
;;
esac
}
open_repository() {
load_repmod
rep_init
}
close_repository() {
rep_cleanup
clean_cache
}
# functions that queries the cached index
# parameter: DEPEND | REQURIED_BY | SHASUMS
index_cat() {
tar -O -zxf $INDEX_CACHE $1
}
index_get_deps() {
local pkg
init_index_cache
pkg=`rm_ver $1`
index_cat DEPEND | grep "^${pkg}:" | cut -d : -f 2
clean_cache
}
index_get_reqs() {
local pkg
init_index_cache
pkg=`rm_ver $1`
index_cat REQUIRED_BY | grep "^${pkg}:" | cut -d : -f 2
}
# get the package filename (wihtout path)
index_get_pkgf() {
init_index_cache
index_cat SHASUMS | awk "/ ${1}-[0-9].*/ {print \$2}" | tail -n 1
}
# get the package name-version from index
index_get_pkgv() {
basename `index_get_pkgf` .apk
}
#########
# init code
#########
init_globals