#!/bin/sh
META_SUFFIX=mtgz
VERSION=0.99
# 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
}
# 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 .apk at the end)
is_apk() {
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
}
# 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 (contains "^[a-z]*:/")
# if parameter is not an apk, find the version number also
get_apk_uri() {
local pkgf p=$1
if is_uri $p ; then
echo $p
elif is_apk $p ; then
echo "$APK_PATH/$p"
else
pkgf=`index_get_pkgf $p`
# return error in case the package is not in index
[ -z "$pkgf" ] && return 1
echo "$APK_PATH/$pkgf"
fi
}
# append APK_PATH if parameter is not an uri
mk_uri() {
if is_uri $1 ; then
echo $1
else
echo "$APK_PATH/$1"
fi
}
###########################################################
# 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:="/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
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/apk`
APK_DBDIR_IN_PKG=`get_var APK_DBDIR_IN_PKG ./var/db/apk`
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"`
INDEX="Index.tar.gz"
CACHED_INDEX="$APK_CACHE/$INDEX"
INDEX_URI=`get_var 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 "CACHED_INDEX=$CACHED_INDEX"
}
###########################################################
# 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
}
# Init ths insaled package database
db_init() {
mkdir -p ${APKDB}
}
###########################################################
# 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
}
############################################################
# This func examine the APK_PATH variable and loads the repository module
load_repmod() {
local reptype repmod uri
is_uri "$1" || die "Cannot load repository module. Not an URI: $1"
uri=$1
REP_DIR=`beautify_path $( echo "$uri" | cut -d : -f 2)`
REP_SCHEME=`echo "$uri" | cut -d : -f 1`
# check if repmod is already loaded
if [ "$REP_SCHEME" = "$REPMOD" ] ; then
return 0
fi
# unload any previously loaded repmods by loading the null repmod
. $APK_LIBS/null.repmod || die "null repmod is missing. Aborting"
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 not existing.
# sets the global variable INDEX_CACHE
init_index_cache() {
local opwd
mkdir -p $APK_CACHE
if [ -f "$INDEX_CACHE" ] ; then
echo $INDEX_CACHE
else
opwd=`pwd`
cd $APK_CACHE
INDEX_CACHE=`rep_fetch $INDEX`
cd $opwd
[ -z "$INDEX_CACHE" ] && die "Failed to download $INDEX_URI"
fi
}
# remove all downloaded packages unless $APK_KEEPCACHE is set
clean_packages() {
[ "$APK_KEEPCACHE" ] && rm -r $APK_CACHE/*.apk
}
# functions that queries the cached index
# parameter: DEPEND | REQURIED_BY | SHASUMS
index_cat() {
init_index_cache
tar -O -zxf $INDEX_CACHE $1
}
index_get_deps() {
local pkg
pkg=`rm_ver $1`
index_cat DEPEND | grep "^${pkg}:" | cut -d : -f 2
}
# reqursively find all dependencies
index_recurse_deps() {
local i
for i in `index_get_deps $1` ; do
index_recurse_deps $i
done
echo $1
}
index_all_deps() {
index_recurse_deps $1 | sort | uniq
}
index_get_reqs() {
local pkg
pkg=`rm_ver $1`
index_cat REQUIRED_BY | grep "^${pkg}:" | cut -d : -f 2
}
# get the package filename (wihtout path)
# parameter can either be a:
# pkgf - just return the parameter
# pkgv - just append .apk
# pkg - append version number and .apk
#
index_get_pkgf() {
local regexp p=$1
if is_apk $p ; then
echo $p
return
elif has_version $p ; then
regexp=" $p.apk"
else
regexp=" $p-[0-9].*"
fi
index_cat SHASUMS | awk "/$regexp/ {print \$2}" | tail -n 1
}
# get the package name-version from index
index_get_pkgv() {
basename `index_get_pkgf` .apk
}
#############################################################################
# functions to handle apk files
#############################################################################
# extract dependency from file without installing it
pkgf_get_deps() {
local pkgv=`basename $1 .apk`
tar -O -zxf $1 $APK_DBDIR_IN_PKG/$pkgv/DEPEND
}
#pkgf_install() {
#
#}
#########
# init code
#########
init_globals