#! /bin/sh
#
# u7z - 7zip file archive Virtual File System for Midnight Commander ( ftp://ftp.ibiblio.org/pub/Linux/utils/file/managers/mc/ )
# 
# Copyright (C) 2004 Sergiy Niskorodov (sgh at ukrpost dot net)

# Written by Sergiy Niskorodov aka SGh
# Fixed by myspace (7za instead of 7z)
#
# beta version 0.2 (27 Jul 2004)
#
# 7za for linux can be found on http://sourceforge.net/projects/p7zip/


# Thanks to urar VFS authors andrey joukov 2:5020/337.13@fidonet.org, 
# christian.gennerat@alcatel.fr, Andrew V. Samoilov <sav@bcs.zp.ua>
# I use this script like example


# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA


SEVENZ=`which 7za`


mc7zfs_list ()
{
    $SEVENZ l "$1" 2>/dev/null | gawk -v uid=${UID-0} '
BEGIN { flag=0 }
 /^-------/ { flag++; if (flag > 1) exit 0; next }
{
if (flag == 0) next

year=substr($1, 1, 4)
month=substr($1, 6, 2)
day=substr($1, 9, 2)
date=month "-" day "-" year

time=substr($2, 1, 5)

gsub(/\\/, "/", $6)

if (index($3, "D") != 0)
    $3="drwxr-xr-x"
else
if (index($3, ".") != 0)
    $3="-rw-r--r--"

printf "%s   1 %-8d %-8d %8d %s %s %s\n", $3, uid, 0, $4, date, time, $6
}'
}

mc7zfs_copyin ()
{
# preserve pwd.
    pwd=`pwd`
# Create a directory and copy in it the tmp file with the random name
    mkdir "$3.dir"
    cd "$3.dir"
    di="${2%/*}"
# if file is to be written upper in the archive tree, make fake dir
    if test "$di" != "${2##*/}" ; then
        mkdir -p "$di" 
    fi
    cp -fp "$3" "$3.dir/$2" 
    $SEVENZ a "$1" "$2" >/dev/null 2>&1
    cd $pwd
    rm -rf "$3.dir"
}

mc7zfs_copyout ()
{
    dir=tmpdir.${RANDOM}
    mkdir /tmp/"$dir"
    $SEVENZ e "$1" "$2" -o/tmp/"$dir" > /dev/null 2>&1
    EXFN=`basename "$2"`
    cat /tmp/"$dir"/"$EXFN" > "$3"
    rm -rf /tmp/"$dir"
}

mc7zfs_mkdir ()
{
# Function not fully implemented, because 7z cannot keep empty directories
# preserve pwd.
    pwd=`pwd`
# Create a directory and create in it a tmp directory with the good name     
    dir=tmpdir.${RANDOM}
    mkdir $dir
    cd $dir
    mkdir -p "$2"  
# 7z cannot create an empty directory    
#    touch "$2"/.7zfs
    $SEVENZ a -r "$pwd"/"$1" "$2" >/dev/null 2>&1
#    $SEVENZ d "$pwd"/"$1" "$2/.7zfs" >/dev/null
    cd $pwd
    rm -rf $dir
}

mc7zfs_rm ()
{
    $SEVENZ d "$1" "$2" >/dev/null 2>&1
}

umask 077

cmd="$1"
shift

case "$cmd" in
  list)    mc7zfs_list    "$@" ;;
  rm)      mc7zfs_rm      "$@" ;;
  rmdir)   mc7zfs_rm      "$@" ;;
  mkdir)   mc7zfs_mkdir   "$@" ;;
  copyin)  mc7zfs_copyin  "$@" ;;
  copyout) mc7zfs_copyout "$@" ;;
  *) exit 1 ;;
esac
exit 0
