#!/usr/bin/env sh
# ipkg-wrapper
#
# This is a wrapper script for ipkg. The script performs various functions when called from the following valid symlinks:
#   ipkg-files  -   lists the files in the given package.
#   ipkg-info   -   shows the info of the given package.
#   ipkg-size   -   calculates the installed size of the given package. Note that the calculated size can be different from
#                   the file versus an installed package due to differences in block size, the size that ls returns rather
#                   than what's recorded in the package, the phase of the moon, etc. IOW, don't sweat the small stuff!
#
# 2005-05-09 Written by GROG! <Uber[dot]GROG[at]Gmail[dot]Com>
# 2005-06-10 GROG! Add remove which runs makecompat & ipkg-link remove if required before removing the package.
# 2005-06-10 GROG! Fix pkgname reference for files/info.
# 2005-06-13 GROG! Don't assume first line of -files output is invalid (non-existant files/directories); ditch it.
# 2005-06-13 GROG! Only count the size of files, not directories.

# set -x # TESTING

if [ $# -ne 1 ]; then
    echo "Usage: ${0##*/} (pkgname|pkgfile)" >&2
    exit 1
fi

ACTION=${0##*-}
PKG=$1

case $ACTION in
    size) if [ -s $PKG -a ! -d $PKG ]; then
              ipkg-files $PKG 2>/dev/null | grep -v "^Package.*:" |
                while read line; do
                    set -- $line
                    echo $3
                done
          else
              ipkg files $PKG | grep -v "^Package.*:" | xargs ls -ld 2>/dev/null |
                while read line; do
                    set -- $line
                    [ -s $9 -a ! -d $9 ] && echo $5
                done
          fi | (while read line; do
                    TOTALSIZE=$((TOTALSIZE+$line))
                done
                echo "Total installed size: $((TOTALSIZE/1024))kb")
          ;;
    files|info)
          if [ -s $PKG -a ! -d $PKG ]; then
            TMPDIR=/tmp/${1##*/}
            TMPDIR=${TMPDIR%%_*}
            \mkdir $TMPDIR >/dev/null
            \cp $PKG $TMPDIR >/dev/null
            \cd $TMPDIR >/dev/null
            (ar -xv ${PKG##*/} || tar -xzf ${PKG##*/}) >/dev/null 2>&1
            case $ACTION in
                files) echo "Package file ${PKG##*/} contains the following files:" >&2
                       tar -tzvf data.tar.gz
                       ;;
                 info) tar -xzf control.tar.gz
                       cat control
                       echo
                       ;;
            esac
            cd - >/dev/null
            rm -Rf $TMPDIR >/dev/null
          else
            ipkg $ACTION $PKG
          fi
          ;;

    remove) TMP=${TMP:-/var/tmp}
            TMPFILE=$TMP/${0##*/}.$$
            trap "rm -f $TMPFILE; exit" 0 1 2 3 15

            ipkg-files $PKG > $TMPFILE

            RUNCOMPAT=$(which runcompat 2>/dev/null)
            if [ -x "$RUNCOMPAT" ]; then
                grep -E "\/bin\/" $TMPFILE | while read BIN; do
                    BIN=$(which ${BIN##*/})
                    if [ -x "$BIN" ]; then
                        diff $BIN $RUNCOMPAT >/dev/null 2>&1 && makecompat $BIN
                    fi
                done
            fi

            grep "^dest " /etc/ipkg.conf | while read DEST; do
                set -- $DEST
                case $2 in
                    root) ;;
                    *) if grep $3 $TMPFILE >/dev/null 2>&1; then
                            echo "unlinking $PKG..."
                            ipkg-link $ACTION $PKG
                            break
                        fi ;;
                esac
            done

            ipkg $ACTION $PKG

            rm -f $TMPFILE
            trap - 0 1 2 3 15
            ;;

    *) echo "This script must be called via one of the following symlink's:" >&2
       echo "    ipkg-files ipkg-info ipkg-remove ipkg-size" >&2
       exit 1 ;;
esac

exit 0

