#!/bin/dash
# (C) James Budiono 2014
# License: GPL Version 3 or later.
#
### extract contents of a zipball to current directory
# $1 - zip file
# $2 - optional target directory

[ -z "$1" ] && echo "Usage: ${0##*/} package [targetdir]" && exit
out=$(mktemp -p /tmp extractzipball.XXXXXXXX)

(
### 1. prepare
TARGETDIR=$(readlink -f "$1")
case "$TARGETDIR" in
	*.?ip)  TARGETDIR="${TARGETDIR%.?ip}"  ;;
	*.ZIP)  TARGETDIR="${TARGETDIR%.ZIP}"  ;;
	*) TARGETDIR="${TARGETDIR}.extracted"  ;;
esac
[ "$2" ] && TARGETDIR="$2"
mkdir -p "$TARGETDIR"

### 2. extract
printf "Extracting ${1} in ${TARGETDIR}\n===\n"
if unzip -o "$1" -d "$TARGETDIR" 2> /dev/null | sed 's/[^:]*://' ; then
	printf "===\nExtraction successful."
else
	printf "===\nExtraction failed!"
fi
) > $out &

Xdialog --title "Extract Zipball $1" --no-cancel --tailbox $out 25 80
rm $out
