#!/bin/sh
#Barry Kauler 2005 www.puppylinux.com
#frontend for XArchive.
#well, i want this to be a universal archiver frontend for puppy.
#v4.00 25apr2008 BK: now have full dpkg-deb in Puppy.

PARAMIN="$@"

CDIR="`pwd`"

#note, do not cd to directory this app is located in.

#v2.11 G2 has suggested this...
if echo "$1" | grep -qi '\.gz$' ;then
   if ! echo "$1" | grep -qi '\.tar\.gz$' ;then
      xmessage -buttons "Yes,No" -nearmouse "Do you want to decompress $1 ?
(it will decompress in current location
 and the original file will be deleted)"
      [ $? -eq 101 ] || exit
      gunzip "$1"
      sync
      exit
   fi
fi

#also this...
if echo "$1" | grep -qi '\.bz2$' ;then
   if ! echo "$1" | grep -qi '\.tar\.bz2$' ;then
      xmessage -buttons "Yes,No" -nearmouse "Do you want to decompress $1 ?
(it will decompress in current location
 and the original file will be deleted)"
      [ $? -eq 101 ] || exit
      xmessage "Uncompressing, please wait..." &
      UPID=$!
      bunzip2 "$1"
      sync
      kill $UPID
      exit
   fi
fi


#in order of preference...
if [ ! "`which xarchiver`" = "" ];then
 XARCHIVE="xarchiver"
else
 if [ ! "`which TkZip`" = "" ];then
  XARCHIVE="TkZip"
 else
  if [ ! "`which guitar`" = "" ];then
   XARCHIVE="guitar"
  else
   exec xmessage -bg "orange red" -center -title "PupZip: ERROR" "No archive GUI program found.
Please install XArchiver."
  fi
 fi
fi

#the full alternatives, dotpups by GuestToo...
DPKGDEB="dpkg-deb"
[ ! "`which dpkg-deb2`" = "" ] && DPKGDEB="dpkg-deb2"
RPM2CPIO="rpm2cpio2"
[ ! "`which rpm2cpio2`" = "" ] && RPM2CPIO="rpm2cpio2"

if [ "$PARAMIN" = "" ];then
 #just start xarchiver...
 exec $XARCHIVE
fi

if [ "$XARCHIVE" = "xarchiver" ];then
 MSGE="Drag and drop invocation of XArchiver is achieved by dragging any file or
directory to the open Xarchiver window on the desktop. If you drag one of the
recognised archived files, such as .tar.gz, .rpm, .deb, .zip, .tar.bz2,
it will open in XArchiver and you will be given the opportunity to extract
it. If you drag an ordinary file to the open window, such as for example
\"myfile.txt\", XArchiver will popup a dialog asking if you want to add
it to an existing archive or create a new one."
else
 MSGE="There is no support for drag-and-drop of archive files to the desktop icon,
either for files and directories -- for that you need to have 
XArchiver installed and open, the preferred archiver program for Puppy."
fi

if [ "$PARAMIN" = "--help" ];then
 xmessage -bg "orange" -center -title "PupZip: help" "
This is a frontend to $XARCHIVE, which in turn is a frontend to the
archiver utilities in Puppy (such as gzip, bzip2, dpkg, rpm, zip).

Note: $XARCHIVE can be started in the conventional way via the menu.

$MSGE

PupZip can also be invoked from Rox by the \"Open With...\" menu,
by right-clicking on a file or directory."
 exit
fi


NORMFILE="`echo -n "$PARAMIN" | grep -iv "\\.tar" | grep -iv "\\.bz" | grep -iv "\\.gz" | grep -iv "\\.rpm" | grep -iv "\\.deb" | grep -iv "\\.zip" | grep -iv "\\.tgz" | grep -iv "\\.tbz" | grep -iv "\\.lzh"`"

if [ "$NORMFILE" = "" ];then
 #it is an archive file...
 
 #get absolute path of file...
 if [ "`echo -n "$PARAMIN" | cut -b 1`" = "/" ];then
  FULLSPEC="$PARAMIN"
 else
  #relative path...
  if [ "$CDIR" = "/" ];then
   FULLSPEC="/$PARAMIN"
  else
   FULLSPEC="$CDIR/$PARAMIN"
  fi
 fi

 #busybox applets are not adequate for handling rpm, deb...
 AFILE="`basename "$PARAMIN"`"
 if [ "`echo -n "$AFILE" | grep '\.'`" = "" ];then
  #file has no extension, so treat as ordinary file...
  if [ "$XARCHIVE" = "xarchiver" ];then
   exec xarchiver --add=ask "$PARAMIN"
  else
   exec xmessage -bg "orange red" -center -title "PupZip: ERROR" "Not an archive file"
  fi
 else
  AEXT="$AFILE"
  while [ ! "`echo -n "$AEXT" | grep '\.'`" = "" ];do
   AEXT="`echo -n "$AEXT" | cut -f 2-6 -d '.'`"
  done
 fi
 #drops here with AEXT=extension.
 case $AEXT in
  rpm|RPM)
   mkdir /tmp/temprpm
   cd /tmp/temprpm
   $RPM2CPIO "$FULLSPEC" | cpio -d -i -m
   if [ ! $? -eq 0 ];then
    exec xmessage -bg "orange red" -center -title "PupZip: ERROR" "An error has occurred opening the RPM file.
Suggestion: install unrpm.pup DotPup package, which contains an alternative
RPM extraction program, that PupZip will automatically use if present."
   fi
   sync
   tar -c -f /tmp/temprpm.tar .
   rm -rf /tmp/temprpm
   cd /
   $XARCHIVE /tmp/temprpm.tar
   rm -f /tmp/temprpm.tar
   cd $CDIR
   exit
   ;;
#v4.00 remove...
#  deb|DEB)
#   mkdir /tmp/temprpm
#   cd /tmp/temprpm
#   $DPKGDEB -x "$FULLSPEC"
#   if [ ! $? -eq 0 ];then
#    exec xmessage -bg "orange red" -center -title "PupZip: ERROR" "An error has occurred opening the DEB file."
#   fi
#   tar -c -f /tmp/temprpm.tar .
#   rm -rf /tmp/temprpm
#   cd /
#   $XARCHIVE /tmp/temprpm.tar
#   rm -f /tmp/temprpm.tar
#   cd $CDIR
#   exit
#   ;;
 esac
 exec $XARCHIVE "$PARAMIN"
else
 if [ "$XARCHIVE" = "xarchiver" ];then
  exec xarchiver --add=ask "$NORMFILE"
 else
  exec xmessage -bg "orange red" -center -title "PupZip: ERROR" "Not an archive file"
 fi
fi

###END###
