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

if [ -z "$1" ]; then
    echo "No file/folder supplied"
	exit 0
fi
XXX=`file --mime-type "$1" | awk '{print $2}'`
if echo "$XXX" | egrep 'zip|x-tar|compressed|x-lha|x-tar|x-rar|debian.binary-package'; then
	zenity --no-wrap  --window-icon="/usr/share/pixmaps/compress.svg" --info --text="\nAlready compressed...\n\nextract, then compress in the format of your choice.\n" --timeout 5 2> /dev/null
	exit 0
fi
FPATH=$(dirname "$1")
if [ -d "$1" ]; then
	cd "$1"
	ARCHIVE=${PWD##*/}
else
	ARCHIVE=$(basename "$1")
	EXTENSION="${1##*.}"
fi

cd "$FPATH"
TYPE=$(yad --list --no-headers --width=290 --height=300 --grid-lines=hor --center --text-align=center --window-icon="/usr/share/pixmaps/compress.svg" --title="Create Archive" --text="\n<span font='14'> Select compression type </span>\n" --column=  tar.xz tar.gz tar.bz2 tar 7z rar zip )
 if [[ $? == 1 || $? == 252 ]]; then
 exit
fi


# get rid of trailing pipe *where did this co,e from?)
MYTYPE=`echo $TYPE | sed 's/|$//'`


RET=$(zenity --file-selection --save --title="Choose name for package" --confirm-overwrite --filename="$ARCHIVE.$MYTYPE");
if [ $? == 1 ]; then
	exit
 else
	rm -f "$ARCHIVE.$MYTYPE" 2>/dev/null
fi

apack -f $RET $ARCHIVE | zenity --width=400 --progress --pulsate --window-icon="/usr/share/pixmaps/compress.svg" --auto-close --text="Compressing..."  2> /dev/null&
exit 0
