#! /bin/sh
#
# Zoo file system
#
# Copyright ? U. N. Known
#
# This filesystem is _dangerous_. It used to create symlinks in filesystem
# with zoo file, it used to happily delete file from your filesystem.
# Now it is 'only' very ugly (it creates temporary files in ~/.mc/
#

ZOO=zoo

mczoofs_list ()
{
    $ZOO l "$1" | gawk -v uid=${UID-0} '
BEGIN { hyphens=0 }
/^---/ { if (hyphens > 0) exit 0; hyphens=1; next }
/^[^\ ]/ { next }
{
if (NF < 8)
    next
if ($8 ~ /^\^/)
    $8=substr($8, 2)
if ($6 > 50)
    $6=$6 + 1900
else
    $6=$6 + 2000
split($7, a, ":")
if ($8 ~ /\/$/)
    printf "drwxr-xr-x   1 %-8d %-8d %8d %s %2d %4d %02d:%02d %s\n", uid, 0, $1, $5, $4, $6, a[1], a[2], $8
else
    printf "-rw-r--r--   1 %-8d %-8d %8d %s %2d %4d %02d:%02d %s\n", uid, 0, $1, $5, $4, $6, a[1], a[2], $8
}' 2>/dev/null
    exit 0
}

mczoofs_copyout ()
{
    $ZOO xp "$1" "$2" | tail +6l > "$3" 2>/dev/null
    exit 0
}

# zoo is stupid and won't be happy when the name has no extension.
# We have to do a small trick :) [pretty dirty, broken hack -- pavel]
if echo "$2" | grep '\.zoo$'; then
  ARCHIVE="$2"
else
  tmpdir=`mktemp -d ${TMPDIR:-/tmp}/mctmpdir-uzoo.XXXXXX` || exit 1
  ARCHIVE=$tmpdir/tmp.zoo
  ln -s $2 $ARCHIVE
  trap 'rm -rf $tmpdir' 0 1 2 3 5 13 15
fi

umask 077

case "$1" in
  list) mczoofs_list "$ARCHIVE"; exit $?;;
  copyout) mczoofs_copyout "$ARCHIVE" "$3" "$4"; exit $?;;
esac
exit 1
