#!/bin/bash
#
# atool-compress
# Monday, 03/15/2021
# script to send a directory path to be compressed with atool
# by Paul Sherman for Absolute Linux, 2018-21- GPL3
#

if [ -z "$1" ]; then
#    echo "No file/folder supplied"
	exit 0
fi
if [ ! -f "$1" ]; then
#    echo "Not a regular file"
	exit 0
fi

rm -f /tmp/archive_contents 2>/dev/null

# tar vs compressed tar snafu avoidance...
# ends with .tar.gz
x=`echo "$1" | grep '.tar.gz$'`
if [ ! -z "$x" ];then
	# ends with .tar.gx...
	# but is it really compressed?
	y=`file "$1" | grep 'POSIX tar archive$'`
	if [ ! -z "$y" ];then
		#s this is a .tar.gz, but is NOT compressed....
		echo "This is a tar.gz, but is not compressed"
		tar -tf "$1" | awk '{print $NF}' | sed '1d' -> /tmp/achive_contents
		yad --text-info --button="gtk-close:0" --fontname='Mono 11' --width=800 --height=600 --title $(basename "$1") --window-icon="/usr/share/pixmaps/aview.svg" --filename=/tmp/achive_contents&
		rm -f /tmp/archive_contents 2>/dev/null
		exit 0
	fi
fi

# plain gzip
y=`file --mime-type "$1" | grep 'application/gzip$'`
if [ ! -z "$y" ];then
	# plain gzip
	echo "Plain gzip"
	gunzip -l "$1"  | awk '{print $NF}' | sed '1d' -> /tmp/achive_contents
	yad --text-info --button="gtk-close:0" --fontname='Mono 11' --width=800 --height=600 --title $(basename "$1") --window-icon="/usr/share/pixmaps/aview.svg" --filename=/tmp/achive_contents&
	rm -f /tmp/archive_contents 2>/dev/null
	exit 0
fi

# deal with single-file XZ compressed data
ISXZ=`file "$1" | grep 'XZ compressed dat'`
if [ ! -z "$ISXZ" ]; then
	echo "XZ compressed data"
	listres=`tar -Jtvf "$1"`
	if [ -z "$listres" ]; then
		NAM="${1##*/}"
		yad --center --fontname="sans 20" --window-icon="deb" --title="Single file XZ" --borders=36 --text="<big><b>$NAM</b></big>" --no-buttons --text-align=center --width=440 --timeout=5&
		exit 0
	fi
fi

als "$1" | awk '{print $NF}' | sed '1d' -> /tmp/achive_contents
yad --text-info --button="gtk-close:0" --fontname='Mono 11' --width=800 --height=600 --title $(basename "$1") --window-icon="/usr/share/pixmaps/aview.svg" --filename=/tmp/achive_contents&
rm -f /tmp/archive_contents 2>/dev/null

exit 0
