#! /bin/sh
#
# Written by Ralf Hoffmann <ralf@boomerangsworld.de>
# uxdms 0.9.0
#
# (C) 2007 Ralf Hoffmann <ralf@boomerangsworld.de>
# May be distributed under the terms of the GNU General
# Public License V2 or higher
#
# requires xdms (http://zakalwe.fi/~shd/foss/xdms/)
#

XDMS=xdms
AWK=gawk

extfs_xdms_list ()
{
  if test -f "$1"; then
    t=$(ls -l "$1" | $AWK '{print $6, $7, $8}')
    name=$(basename "$1" | sed 's/\(.*\)\.dms/\1/')
    adfname="$name.adf"

    adfsize=$($XDMS f "$1" 2>/dev/null | $AWK 'BEGIN { sum = 0 } /^[ ]*[0-9]+/ { sum += $3 } END { print sum }')
    statsize=$($XDMS f "$1" 2>/dev/null | wc -c)

    echo "-rw-r--r-- 1 $(id -nu) $(id -ng) $adfsize $t $adfname"
    echo "-rw-r--r-- 1 $(id -nu) $(id -ng) $statsize $t STAT"
  fi
}

extfs_xdms_copyout ()
{
    name=$(basename "$1" | sed 's/\(.*\)\.dms/\1/')
    adfname="$name.adf"
    if test "$2" = "$adfname"; then
        $XDMS -q u "$1" +"$3"
    elif test "$2" = "STAT"; then
        $XDMS f "$1" > "$3" 2>/dev/null
    fi
}

# override any locale for dates
LC_ALL=C
export LC_ALL

umask 077

case "$1" in
    list)
        extfs_xdms_list "$2"
        ;;
    copyout)
        shift
        extfs_xdms_copyout "$@"
        ;;
    *)
        echo "extfs_xdms: unknown command: \"$1\"." 1>&2
        exit 1
        ;;
esac

exit 0
