#!/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.
#w474 support .delta, .bfe files.
#100616 support .xz files. support slackware and arch pkgs.
#110620 busybox rpm2cpio does not work with mageia rpms, use 'exploderpm' instead (in /usr/sbin, permanent builtin).
#120203 rodin.s: internationalized.
#120512 added support for .rar (needs unrar or rar pkg installed).

export TEXTDOMAIN=pupzip
export TEXTDOMAINDIR=/usr/share/locale
export OUTPUT_CHARSET=UTF-8
eval_gettext () {
  local myMESSAGE=$(gettext "$1")
  eval echo \"$myMESSAGE\"
}

PARAMIN="$@"

CDIR="`pwd`"

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

#ask if this is a slackware or arch binary pkg...
if [ "`echo -n "$PARAMIN" | grep -E '\.tgz$|\.txz$|\.pkg\.tar\.gz$'`" != "" ];then
 xmessage -bg yellow -buttons "$(gettext 'Yes'):100,$(gettext 'No'):101" -center "$(gettext 'Is this a Slackware or Arch package, that you want to install?')
$(gettext 'If you answer Yes, the Package Manager application will run...')
$(gettext 'If you answer No, the XArchive application will run...')"
 [ $? -eq 100 ] && exec petget $PARAMIN
fi

#w474
if [ "`echo -n "$1" | grep '\.delta$'`" != "" ];then
 exec xdelta_gui "$1"
fi
if [ "`echo -n "$1" | grep '\.bfe$'`" != "" ];then
 exec bcrypt_gui "$1"
fi

#v2.11 G2 has suggested this...
if echo "$1" | grep -qi '\.gz$' ;then
   if ! echo "$1" | grep -qi '\.tar\.gz$' ;then
      xmessage -buttons "$(gettext 'Yes'),$(gettext 'No')" -center "$(gettext 'Do you want to decompress') ${1}?
($(gettext 'it will decompress in current location')
 $(gettext '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 "$(gettext 'Yes'),$(gettext 'No')" -center "$(gettext 'Do you want to decompress') ${1}?
($(gettext 'it will decompress in current location
 and the original file will be deleted'))"
      [ $? -eq 101 ] || exit
      xmessage "$(gettext 'Uncompressing, please wait...')" &
      UPID=$!
      bunzip2 "$1"
      sync
      kill $UPID
      exit
   fi
fi

#100616 .xz files...
if echo "$1" | grep -qi '\.xz$' ;then
 if ! echo "$1" | grep -qi '\.tar\.xz$' ;then
  if [ "`which xz`" == "" ];then
   xmessage -bg red -center "`gettext \"ERROR: 'xz' utility not installed\"`"
   exit
  fi
  xmessage -bg yellow -buttons "$(gettext 'Yes'):100,$(gettext 'No'):101" -center "$(gettext 'Do you want to decompress') ${1}?
($(gettext 'it will decompress in current location
 and the original file will be deleted'))"
  [ $? -ne 100 ] && exit
  yaf-splash -bg orange -text "$(gettext 'Uncompressing, please wait...')" &
  UPID=$!
  xz -d "$1"
  sync
  kill $UPID
  exit
 fi
fi


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

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

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

if [ "$XARCHIVE" = "xarchive" ];then
 MSGE="$(gettext 'Drag and drop invocation of XArchive is achieved by dragging any file or
directory to the "pupzip" icon 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 XArchive and you will be given the opportunity to extract
it. If you drag an ordinary file to the desktop icon, such as for example
"myfile.txt", a XArchive will popup a dialog asking if you want to add
it to an existing archive or create a new one.')"
else
 MSGE="$(gettext 'There is support for drag-and-drop of archive files to the desktop icon,
however not for files and directories -- for that you need to have 
XArchive installed, the preferred archiver program for Puppy.')"
fi

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

`eval_gettext \"Note: \\\$XARCHIVE can be started in the conventional way via the menu.\"`

$MSGE

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

#120512 added .rar
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 "\\.txz" | grep -iv "\\.tbz" | grep -iv "\\.rar"`"

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" = "xarchive" ];then
   exec xarchive --add=ask "$PARAMIN"
  else
   exec xmessage -bg "orange red" -center -title "$(gettext 'PupZip: ERROR')" "$(gettext '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
   #110620 rpm2cpio does not always work, use exploderpm...
   #$RPM2CPIO "$FULLSPEC" | cpio -d -i -m
   exploderpm -x "$FULLSPEC" #a script in /usr/sbin, uses cpio.
   if [ ! $? -eq 0 ];then
    exec xmessage -bg "orange red" -center -title "$(gettext 'PupZip: ERROR')" "$(gettext 'An error has occurred opening the RPM file.')"
   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" = "xarchive" ];then
  exec xarchive --add=ask "$NORMFILE"
 else
  exec xmessage -bg "orange red" -center -title "$(gettext 'PupZip: ERROR')" "$(gettext 'Not an archive file')"
 fi
fi

###END###
