#! /bin/sh
#
# zip/unzip extfs for mc; created for spblinux2 (www.8ung.at/spblinux) 03-2003, Christian Ostheimer
# Note: mc extfs doesn't do a good job in preserving attributes...
zlist()
{
 OLDIFS="$IFS"
 IFS=""
# for z_info in `unzip -qq -v $1 |sed "s/^//"`; do
#    2970  Defl:N     1985  33%  03-22-02 15:41  afddf488  m125Update.prc
 for z_info in `zipinfo -lT --h --t "$1" |sed "s/^//"`; do
#-rw-rw-rw-  2.0 fat     2970 b-     1985 defN 20020322.154154 m125Update.prc
 [ "$z_info" ] || continue
 if [ "unx" = `expr substr "$z_info" 17 3` ]; then
  z_type=`expr substr "$z_info" 1 10`
 else
  z_type="-rw-rw-rw-"
 fi
 z_size=`expr substr "$z_info" 21 8`
 z_date=`expr substr "$z_info" 47 15`
 z_date="`expr substr "$z_info" 51 2`-`expr substr "$z_info" 53 2`-`expr substr "$z_info" 49 2` `expr substr "$z_info" 56 2`:`expr substr "$z_info" 58 2`"
 z_name=`expr substr "$z_info" 63 256`
 printf "%s %3i %8i %8i %8i %s %s" "$z_type" 1 0 0 "$z_size" "$z_date" "$z_name"
 if [ "l" = `expr substr "$z_info" 1 1` ]; then
  printf " -> %s\n" `unzip -p "$1" $z_name`
 else
  printf "\n"
 fi  
 done 
 IFS="$OLDIFS"
}

zout()
{
 unzip -p "$1" "$2" >"$3" 2>/dev/null
}

zrun()
{
 oldpwd=`pwd`
 mkdir /tmp/uzip$PPID
 cd /tmp/uzip$PPID
 zrunfile=`basename "$2"`
 unzip -p "$1" "$2" >"$zrunfile"
 "$zrunfile"
 cd "$oldpwd"
 rm -r /tmp/uzip$PPID
}

zadd()
{
 oldpwd=`pwd`
 mkdir /tmp/uzip$PPID
 cd /tmp/uzip$PPID
 #create 
 zzpath="$3"
 #remove trailing slash
 nn=`expr length "$zzpath"`
 if [ "/" = `expr substr "$zzpath" $nn 1` ]; then
  n=`dc $nn 1 - p`
  zzpath=`expr substr "$zzpath" 1 $n`
 fi
 #remove leading slash
 nn=`expr length "$zzpath"`
 if [ "/" = `expr substr "$zzpath" 1 1` ]; then
  nn=`expr length "$zzpath"`
  n=`dc $nn 1 - p`
  zzpath=`expr substr "$zzpath" 2 $n`
 fi
# echo "$zzpath"
 create_directories "$zzpath"
 case $1 in
  zin)
   ln -s "$4" "$zzpath"
   zip -q "$2" "$zzpath" 1>/dev/null
   ;;
  zmkdir)
   mkdir "$zzpath"
   zip -q "$2" "$zzpath" 1>/dev/null
   ;;
 esac 
 cd "$oldpwd"
 rm -r /tmp/uzip$PPID
}

zrm()
{
 zip -qd "$1" "$2" 
}

zrmdir()
{
 zip -qd "$1" "$2/" 
}

create_directories()
# usage: create_directories aaa/bbb/ccc/dd[/]
#creates subdirectory structure aaa, aaa/bbb, aaa/bbb/ccc
#does NOT create dd (which might be a file or a directory)
{
 tmp_nn=`expr length "$1"`
 #ignore leading slash in tmp_dirname
 if [ "/" = `expr substr "$1" 1 1` ]; then
  tmp_dirname=`expr substr "$1" 2 $tmp_nn`
  tmp_nd=1
 else
  tmp_dirname="$1"
  tmp_nd=0
 fi
 #create subdirectory structure
 while [ 1 ]; do
  #find slash
  tmp_np=`expr index "$tmp_dirname" "/"`
  [ "0" = "$tmp_np" ] && break
  tmp_nd=`dc $tmp_np $tmp_nd + p`
  #ignore trailing slash
  [ "$tmp_nd" = "$tmp_nn" ] && break
  tmp_dirname2=`expr substr "$1" 1 $tmp_nd`
  [ -d "$tmp_dirname2" ] || mkdir "$tmp_dirname2"
  tmp_np=`dc $tmp_np 1 + p`
  tmp_dirname=`expr substr "$tmp_dirname" $tmp_np $tmp_nn`
 done
}

case $1 in
 list) zlist "$2";;
 copyout) zout "$2" "$3" "$4";;
 run) zrun "$2" "$3";;
 copyin) zadd zin "$2" "$3" "$4";;
 mkdir) zadd zmkdir "$2" "$3";;
 rm) zrm "$2" "$3";;
 rmdir) zrmdir "$2" "$3";;
 *) echo "not supported"; exit 1;;
esac
