#!/bin/sh
# TazPkg - Tiny autonomous zone packages manager, hg.slitaz.org/tazpkg
# remove - TazPkg module
# Remove packages

. /lib/libtaz.sh
. /usr/libexec/tazpkg/getenv
log_pkg() {
[ -w "$LOG" ] &&
echo "$(date +'%F %T') - $1 - $PACKAGE ($VERSION$EXTRAVERSION)">> "$LOG"
}
call_pre_remove() {
local tmp
if grep -q '^pre_remove()' "$1"; then
action 'Execute pre-remove commands...'
tmp="$(mktemp)"
cp "$1" "$tmp"
sed -i 's|$1/*$INSTALLED|$INSTALLED|g' "$tmp"
( . "$tmp"; pre_remove "$root" )
status
rm "$tmp"
fi
}
call_post_remove() {
local tmp
if grep -q '^post_remove()' "$1"; then
action 'Execute post-remove commands...'
tmp="$(mktemp)"
cp "$1" "$tmp"
sed -i 's|$1/*$INSTALLED|$INSTALLED|g' "$tmp"
( . "$tmp"; post_remove "$root" )
status
rm "$tmp"
fi
}
virtual_pkg() {
debug "\nvirtual_pkg('$1', '$2')"
local i
unset IFS
for i in $(grep -hs "^$1=" "$2/packages.equiv" | sed "s/^$1=//"); do
if echo $i | fgrep -q : ; then
if [ -f $INSTALLED/${i%:*}/receipt ]; then
echo ${i#*:}
return
fi
elif ! grep -q "^$1	" "$2/packages.info" || [ -f "$INSTALLED/$i/receipt" ]; then
echo $i
return
fi
done
echo $1
}
for rep in $PRIORITY; do
[ ! -f "$rep/packages.info" ] && continue
PACKAGE="$(virtual_pkg "$1" "$rep")"
[ "$PACKAGE" != "$1" ] && break
done
if [ ! -f "$INSTALLED/$PACKAGE/receipt" ]; then
newline; _ 'Package "%s" is not installed.' "$PACKAGE"
exit 1
fi
. "$INSTALLED/$PACKAGE/receipt"
ALTERED="$(awk -F$'\t' -vp=" $PACKAGE " 'index(" " $8 " ", p) { printf "  %s\n", $1 }' "$PKGS_DB/installed.info")"
if [ -n "$ALTERED" ]; then
_ 'The following packages depend on package "%s":' "$PACKAGE"
echo "$ALTERED"
fi
REFRESH=$(cd "$INSTALLED"; grep -sl "^$PACKAGE$" */modifiers)
if [ -n "$REFRESH" ]; then
_ 'The following packages have been modified by package "%s":' "$PACKAGE"
for i in $REFRESH; do
echo "  ${i%/modifiers}"
done
fi
if [ -n "$noconfirm" ] || im && [ -z "$auto" ]; then
confirm "$(_ 'Remove package "%s" (%s)? (y/N)' "$PACKAGE" "$VERSION$EXTRAVERSION")"
if [ "$?" -ne 0 ]; then
newline; _ 'Uninstallation of package "%s" cancelled.' "$PACKAGE"
exit 0
fi
fi
title 'Removing package "%s"' "$PACKAGE"
tazpkg -u "$PACKAGE" --nowarning | grep -v '^$'
call_pre_remove "$INSTALLED/$PACKAGE/receipt"
action 'Removing all files installed...'
IFS=$'\n'
files2remove="$(mktemp)"
debug '\nDetermine which files to remove...'
if [ -f "$INSTALLED/$PACKAGE/modifiers" ]; then
debug '  (modifiers detected)'
mods="$(mktemp)"
for mod in $(cat "$INSTALLED/$PACKAGE/modifiers"); do
cat "$INSTALLED/$mod/files.list">> "$mods" 2>/dev/null
done
awk -vroot="$root" -vfl="$INSTALLED/$PACKAGE/files.list" '
{
if (FILENAME == fl)
f[$0] = 1;
else
f[$0] = "";
}
END {
for (i in f) {
if (f[i] == 1) printf "%s%s\n", root, i;
}
}' "$INSTALLED/$PACKAGE/files.list" "$mods"> "$files2remove"
rm "$mods"
else
debug '  (modifiers not detected)'
awk -vroot="$root" '{ printf "%s%s\n", root, $0; }' \
"$INSTALLED/$PACKAGE/files.list"> "$files2remove"
fi
debug 'Removing files...'
xargs rm -f < "$files2remove"
debug 'Removing folders...'
awk '
BEGIN {
FS = "/"; OFS = "/";
}
{
$NF = "";
if (! a[$0]) {
a[$0] = 1; print;
}
}' "$files2remove" | xargs rmdir -p 2>/dev/null
rm "$files2remove"
unset IFS
status
call_post_remove "$INSTALLED/$PACKAGE/receipt"
. /usr/libexec/tazpkg/bb restore
fl="$INSTALLED/$PACKAGE/files.list"; upd=0
fgrep    /usr/share/applications/    "$fl" | fgrep -q .desktop && udesk='yes'
fgrep -q /usr/share/mime             "$fl" && umime='yes'
fgrep -q /usr/share/icon/hicolor     "$fl" && uicon='yes'
fgrep    /usr/share/glib-2.0/schemas "$fl" | fgrep -q .xml && uschm='yes'
fgrep    /usr/lib/gdk-pixbuf         "$fl" | fgrep -q .so && upixb='yes'
if fgrep -q /lib/modules             "$fl"; then
ukrnl='yes'
if fgrep -q /kernel/fs/ "$fl"; then
ukrnlfs='yes'
fi
fi
if [ -n "$udesk$umime$uicon$uschm$upixb$ukrnl" ]; then
action 'Update system databases...'
upd=1
fi
[ -n "$udesk" ] && chroot "$root/" /usr/bin/update-desktop-database /usr/share/applications 2>/dev/null
[ -n "$umime" ] && chroot "$root/" /usr/bin/update-mime-database /usr/share/mime
[ -n "$uicon" ] && chroot "$root/" /usr/bin/gtk-update-icon-cache /usr/share/icons/hicolor
[ -n "$uschm" ] && chroot "$root/" /usr/bin/glib-compile-schemas /usr/share/glib-2.0/schemas 2>&1 | fgrep -v '/apps/'
[ -n "$upixb" ] && chroot "$root/" /usr/bin/gdk-pixbuf-query-loaders --update-cache
if [ -n "$ukrnlfs" ]; then
for i in $(awk -F/ '{if($6=="fs" && $8~$7)print $7}' "$fl" | sort -u); do
grep -i "/^$i\$/d" "$root/etc/filesystems"
done
fi
[ -n "$ukrnl" ] && grep '/lib/modules' "$fl" | cut -d'/' -f4 | uniq | xargs chroot "$root/" /sbin/depmod -a
[ "$upd" -eq 1 ] && status
action 'Removing package receipt...'
rm -rf "$INSTALLED/$PACKAGE"
sed -i "/ $PACKAGE-$VERSION$EXTRAVERSION.tazpkg$/d" "$PKGS_DB/installed.$SUM"
sed -i "/^$PACKAGE	/d" "$PKGS_DB/installed.info"
status
footer "$(_ 'Package "%s" (%s) removed.' "$PACKAGE" "$VERSION$EXTRAVERSION")"
log_pkg Removed
if ! im && [ -z "$auto" ]; then exit 0; fi
if [ -n "$ALTERED" ]; then
if [ -n "$auto" ]; then
answer=0
else
confirm "$(_ 'Remove packages depending on package "%s"? (y/N)' "$PACKAGE")"
answer=$?
fi
if [ "$answer" -eq 0 ]; then
for i in $ALTERED; do
if [ -d "$INSTALLED/$i" ]; then
tazpkg remove $i
fi
done
fi
fi
if [ -n "$REFRESH" ]; then
if [ -n "$auto" ]; then
answer=0
else
confirm "$(_ 'Reinstall packages modified by package "%s"? (y/N)' "$PACKAGE")"
answer=$?
fi
if [ "$answer" -eq 0 ]; then
for i in $REFRESH; do
if [ "$(wc -l < "$INSTALLED/$i")" -gt 1 ]; then
_ 'Package "%s" was modified by "%s" and other packages. It will not be reinstalled.' \
"${i%/modifiers}" "$PACKAGE"
_ 'Check "%s" for reinstallation.' "$INSTALLED/$i"
continue
fi
rm -r "$INSTALLED/$i"
tazpkg get-install ${i%/modifiers} --forced
done
fi
fi
