#!/bin/dash
# (C) James Budiono 2014, 2015 & JakeSFR 2016, 2019, 2020
# compatible replacement of the original pupzip
#
# Note: internally uses Engrampa
# one argument: open archive, more than one argument: add create new archive 
#

# std localisation stanza
export TEXTDOMAIN=fatdog
. gettext.sh

### configuration
APPTITLE="Pupzip"
ARCHIVER=${DEFAULTARCHIVER:-engrampa}
ARCHIVER_ADD="$ARCHIVER -d" # add/create new files
TMP=/tmp/pupzip.$$

### helpers ###

# $1-path output $TMPTAR
convert_rpm() {
	file=${1##*/}; filename=${file%.*}; TMPTAR="/tmp/${filename}.tar"
	mkdir -p "$TMP/$filename" &&
	rpm2cpio "$1" | ( cd "$TMP/$filename"; cpio -d -i -m 2>/dev/null) &&
	tar -C "$TMP" -cf "$TMPTAR" "$filename" &&
	rm -rf "$TMP"
}

# $1-path output $TMPTAR
convert_deb() {
	file=${1##*/}; filename=${file%.*}; TMPTAR="/tmp/${filename}.tar"
	mkdir -p "$TMP/$filename" &&
	undeb -x "$1" "$TMP/$filename" &&
	tar -C "$TMP" -cf "$TMPTAR" "$filename" &&
	rm -rf "$TMP"
}

# $1-method $2-file
convert_to_tarball() {
	file=${2##*/}
	Xdialog --title="$APPTITLE" --no-buttons --infobox "$(eval_gettext "Converting \${file} to tarball, please wait ...")" 0 0 1000000 &
	XPID=$!

	case $1 in
		rpm) convert_rpm "$2" ;;
		deb) convert_deb "$2" ;;
	esac
	retval=$?

	kill $XPID
	[ $retval -ne 0 ] && Xdialog --title="$APPTITLE" --infobox "$(eval_gettext "\${file} cannot be opened.")" 0 0 10000
	return $retval
}


### main ###
[ -z $DISPLAY ] && echo "$(gettext "You need desktop to run this.")" && exit 1
case $# in
	0) exec $ARCHIVER ;; # no param, launch archiver directly
	1) case "$1" in 
		--help) echo "$(gettext "Pupzip-compatible universal archiver.")" ;;
		*) file=${1##*/}
		   case "$(echo "$file" | tr '[:upper:]' '[:lower:]')" in
		    *.rpm|*.7z|*.ar|*.cpio|*.rar|*.tar|*.tar.gz|*.tar.bz2|*.tar.lz|*.tar.lz4|*.tar.lzma|*.tar.xz|*.tar.z|*.tar.zst|*.tbz|*.tbz2|*.tgz|*.tlz|*.tlz4|*.txz|*.tzst|*.zip)
				exec $ARCHIVER "$1"
				;;
			*.gz|*.bz2|*.lz|*.lz4|*.lzma|*.xz|*.zst)
				if result="$(Xdialog --title="$APPTITLE" --stdout --check "$(gettext "Delete original file after successful decompression")" on \
							--yesno "$(eval_gettext "Do you want to decompress \${file}?")" 0 0)"; 
				then
					[ "$result" = "checked" ] && opt='' || opt='-k'	# BB equivalents don't support this option
					Xdialog --title="$APPTITLE" --no-buttons --infobox "$(eval_gettext "Decompressing \${file}, please wait ...")" 0 0 1000000 &
					XPID=$!
					case "$(echo "$file" | tr '[:upper:]' '[:lower:]')" in
						*.gz)	cmnd='gunzip' ;;
						*.bz2)	cmnd='bunzip2' ;;
						*.lz)	cmnd='lzip -d'	;;
						*.lz4)	cmnd='unlz4 -m --rm' ;;	# --rm to remove src file (trumped by -k opt); -m to force creating output file
						*.lzma)	cmnd='unlzma' ;;
						*.xz)	cmnd='unxz' ;;
						*.zst)	cmnd='unzstd --rm' ;;
					esac
					result="$($cmnd $opt "$1" 2>&1)"
					retval=$?
					kill -0 $XPID 2>/dev/null && kill $XPID
					if [ $retval -ne 0 ]; then
						Xdialog --title="$APPTITLE" --backtitle "$(gettext "Error")" --infobox "$result" 0 0 1000000
					fi
				fi
				;;
			#*.rpm)   convert_to_tarball rpm "$1" && $ARCHIVER "$TMPTAR"; rm -f "$TMPTAR" ;;
			*.deb)   convert_to_tarball deb "$1" && $ARCHIVER "$TMPTAR"; rm -f "$TMPTAR" ;;
			*.delta) exec xdelta_gui "$1" ;;
			*.bfe)   exec bcrypt_gui "$1" ;;
			*)
				[ -d "$1" ] && exec $ARCHIVER_ADD "$1"
				Xdialog --title="$APPTITLE" \
						--buttons-style="text" \
						--ok-label="$(gettext "Add to archive")" \
						--cancel-label="$(gettext "Open as archive")" \
						--yesno "$(gettext "Unknown extension:") ${file##*.}" 0 0
				case $? in
					0)	exec $ARCHIVER_ADD "$1"	;;
					1)	exec $ARCHIVER "$1"	;;
				esac
				;;

		   esac ;;
	   esac ;;
	*) exec $ARCHIVER_ADD "$@" ;;
esac
