#!/bin/bash
#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).
#131224 SFR: huge rewrite + added support for a few more common formats/extensions

export TEXTDOMAIN=pupzip
export TEXTDOMAINDIR=/usr/share/locale
export OUTPUT_CHARSET=UTF-8

# -----------------------------------------------------------------------------

eval_gettext () {
  local myMESSAGE=$(gettext "$1")
  eval echo \"$myMESSAGE\"
}

error_msg () {
  xmessage -bg red -center -title "$(gettext 'PupZip: ERROR')" "$1"
}

request_msg () {
  xmessage -bg yellow -buttons "$(gettext 'Yes'):100,$(gettext 'No'):101" -center -title "PupZip" "$1"
}

check_xarchive () {
if [ ! "$XARCHIVE" ]; then
  xmessage -bg "orange red" -center -title "$(gettext 'PupZip: ERROR')" "$(gettext 'No archive GUI program found.
Please install XArchive.')"
  exit 1
fi
}

add_to_xarchive () {
  check_xarchive
  if [ "$XARCHIVE" = "xarchive" ];then
    exec $XARCHIVE --add=ask "$@"
  else
    error_msg "$(gettext 'Not an archive file')"
    exit 1
  fi
}

# -----------------------------------------------------------------------------

CDIR="`pwd`"
EXTENSION="`echo "$1" | tr [:upper:] [:lower:]`"
UNPACK_CMD=

# SFR: dunno if that TkZip/guitar stuff is still really needed, but let it be, whatever...
XARCHIVE=
for i in xarchive TkZip guitar; do
  [ "`which $i 2>/dev/null`" ] && { XARCHIVE="$i"; break; }
done

# -----------------------------------------------------------------------------

if [ "$1" = "" ];then
  #just start xarchive...
  check_xarchive
  exec $XARCHIVE

elif [ "$1" = "--help" ];then

  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

  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

# -----------------------------------------------------------------------------

# if multiple input files/dirs or directory itself - always add to xarchive!
if [ "$#" -gt 1 ] || [ -d "$1" ]; then
  add_to_xarchive "$@"
fi

# -----------------------------------------------------------------------------

case "$EXTENSION" in
  *.bfe)
    exec bcrypt_gui "$1"
  ;;
  # -----------
  *.delta)
    exec xdelta_gui "$1"
  ;;
  # -----------
  *.rpm)
    #busybox applets are not adequate for handling rpm, deb...
    #get absolute path of file...
    if [ "`echo -n "$1" | cut -b 1`" = "/" ];then
      FULLSPEC="$1"
    else
      #relative path...
      if [ "$CDIR" = "/" ];then
        FULLSPEC="/$1"
      else
        FULLSPEC="$CDIR/$1"
      fi
    fi
    TMPARCH=/tmp/temprpm_${RANDOM}_${$}
    mkdir $TMPARCH
    cd $TMPARCH
    #110620 rpm2cpio does not always work, use exploderpm...geany
    #exploderpm -x "$FULLSPEC" #a script in /usr/sbin, uses cpio.
    # SFR: hack - exploderpm can handle extensions only in lower-case
    #	   so let's call appropriate function directly
    . exploderpm
    OPT='-x'; explode_rpm "$FULLSPEC"
    if [ $? -ne 0 ];then
      error_msg "$(gettext 'An error has occurred opening the RPM file.')"
      exit 1
    fi
    sync
    tar -c -f $TMPARCH.tar .
    rm -rf $TMPARCH
    cd /
    $XARCHIVE $TMPARCH.tar
    rm -f $TMPARCH.tar
    cd $CDIR
    exit
  ;;
  # -----------
  *.7z|*.arj|*.deb|*.rar|*.tar|*.tar.bz|*.tar.bz2|*.tar.gz|*.tar.lzma|*.tar.xz|*.tbz|*.tbz2|*.tgz|*.tlz|*.txz|*.zip)
    #ask if this is a slackware or arch binary pkg...
    case "$EXTENSION" in
      *.tgz|*.txz|*.pkg.tar.gz)
      request_msg "$(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 "$1"
    ;;
    esac
    check_xarchive
    exec $XARCHIVE "$1"
  ;;
  # -----------
  *.gz)			UNPACK_CMD=gzip ;;
  *.bz|*.bz2)	UNPACK_CMD=bzip2 ;;
  *.lzma|*.xz)	UNPACK_CMD=xz ;;
  # -----------
  *) add_to_xarchive "$1" ;;
esac

# handle gz|bz(2)|lzma|xz internally
[ "`which $UNPACK_CMD 2>/dev/null`" ] || { error_msg "`gettext \"ERROR: '$UNPACK_CMD' utility not installed\"`"; exit 1; }

request_msg "$(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=$!

# busybox applets, as well as full bzip2, don't support '--suffix' option
if [ "$UNPACK_CMD" = "bzip2" ] || [ "`readlink -e \`which $UNPACK_CMD\` | grep 'busybox'`" ]; then
  $UNPACK_CMD -d "$1"
else
  $UNPACK_CMD --suffix=".${1##*.}" -d "$1"	# in case if extension is in upper-case
fi
  
sync
kill $UPID
exit

###END###
