#!/bin/sh
#
# Written by Jakub Jelinek 1995
# - MS-DOS ZIP-files fix by Arpad Gereoffy 1999 (a.k.a. A'rpi/ESP-team)
# - NTF (NTFS?) ZIP files support by A'rpi/ESP-team 2000
# - works with dir/filenames with extra chars. fix by A'rpi/ESP-team 2000
#
# (C) 1995 The Free Software Foundation.
#
# Updated by Oleg V. Konovalov <olegarch@mail.ru> 2004
# - works with long dir/filenames in all kind of ZIPs
# - BE (BeOS) ZIP files support
#
#
# how abount id's for other OS, QNX for example?
#
#


XZIP=zip
XUNZIP=unzip
XZIPINFO="unzip -Z"
#
#If you don't have zipinfo, set ZIPINFO=
#

mczipfs_list ()
{
DOZIPINFO=no
#echo "$1" > /tmp/extfs-zip-archivename2
if test -n "$XZIPINFO"; then
    DOZIPINFO=
    $XZIPINFO -l "$1" | gawk -v uid=${UID-0} -v zipfile="$1" -v xunzip=${XUNZIP-unzip} '

BEGIN {
  dir="";
  rx = "[^ 	]+[ 	]+";
  rx = "^" rx rx rx rx rx rx rx rx;
}

/^Archive/ { next }
/^[0-9]*\ file/ { next }

/^[^ 	].*:$/ {
  if ($0 ~ /^\//) dir=substr($0, 2);
  else dir=$0;
  if (dir ~ /\/:$/) sub(/:$/, "", dir);
  else sub(/:$/, "/", dir);
  next;
}

######## ms-dos/win[9x|nt|2k|xp] part

($3 ~ /fat/) || ($3 ~ /ntf/) { 

split($8, a, "-")

if (a[3] < 50)
  a[3] = 2000 + a[3]
else
  a[3] = 1900 + a[3]

{ $9 != "" } {
  name=$0
  sub(rx, "", name)

# cut date from name, huh!
    name = substr(name, 7)
# and lead "/" cut too
    if (name ~ /^\^/) name=substr(name, 2)

# vfs cannot treat dirs as filenames
if (name ~ /\/$/)
    rights="d" substr($1, 2, 3) substr($1, 2, 3) substr($1, 2, 3)
else
    rights=substr($1, 1, 4) substr($1, 2, 3) substr($1, 2, 3)

    printf "%s   1 %-8d %-8d %8d %3s %2d %4d %s %s %s\n", rights, uid, 0, $4, a[2], a[1], a[3], $9, dir, name
}

next
}
######## end of ms-dos/win[9x|nt|2k|xp] part

######## unix/beos part

($3 ~ /unx/) || ($3 ~ /be/) { 

split($8, a, "-")

if (a[3] < 50)
    a[3] = 2000 + a[3]
else
    a[3] = 1900 + a[3]

{ $9 != "" } {
  name=$0
  sub(rx, "", name)

# cut date from name, huh!
    name = substr(name, 7)
# and lead "/" cut too
    if (name ~ /^\^/) name=substr(name, 2)

# we must quotate zipname and filename!
if ($1 ~ /^l/ ) {
    arrow=" -> "
    linkname=""
    cmd=xunzip " -p " "\""zipfile"\"" " "  "\"" name "\""
    cmd | getline linkname
} else {
    arrow=""
    linkname=""
}

    printf "%s   1 %-8d %-8d %8d %3s %2d %4d %s %s %s%s%s\n", $1, uid, 0, $4, a[2], a[1], a[3], $9, dir, name, arrow, linkname
}

next
}
######## end of unix/beos part

{
exit 214
}' 2>/dev/null

    if test $? = 214; then
        DOZIPINFO=no
    fi
fi

if test -n "$DOZIPINFO"; then
    $XUNZIP -v $1 | mawk -v uid=${UID-0} '
BEGIN { hyphens=0 }

/^Archive/ { next }
/^\ Length/ { next }
/^\ ------/ { if (hyphens > 1) exit 0; hyphens=1; next }
{ 
if (hyphens < 1) next;
if ($8 ~ /^\^/)
    $8=substr($8, 2)
if ($8 ~ /\/$/)
    printf "drwxr-xr-x   1 %-8d %-8d %8d %s %s %s\n", uid, 0, $1, $5, $6, $8
else
    printf "-rw-r--r--   1 %-8d %-8d %8d %s %s %s\n", uid, 0, $1, $5, $6, $8 
}' 2>/dev/null
fi
}

mczipfs_copyout ()
{
    $XUNZIP -p "$1" "$2" > "$3" 2>/dev/null
}

case "$1" in
  list) mczipfs_list "$2"; exit 0;;
  copyout) mczipfs_copyout "$2" "$3" "$4"; exit 0;;
esac
exit 1
