#!/bin/bash
#
#  Name:    to_tarxz
#  Created: May 2020, Terry Neve
#
#  Program to recompress an archive to ".tar.xz" for PCLinuxOS packaging.
#  It is designed to be called via Right-Click service menu in pkgutils.
#
#  Revisions:
#       20200517.01  Initial working version

FILEXT=${1##*.}
ITEMNAME=${1%.*}

echo "Processing $1"
case $FILEXT in
    gz)
        gunzip $1
        ;;
        
    bz | bz2)
        bunzip2 $1
        ;;
        
    zip)
        unzip $1
        ;;
        
    xz)
        echo "No action required for $1"
        exit 0
        ;;
    
    tar)
        ITEMNAME=$1
        ;;
        
    *)
        ark --batch --autodestination --autosubfolder $1
        ;;
        
esac

if [ -d $ITEMNAME ]; then
    ITEMNAME=$(basename $ITEMNAME)
    if tar -cf $ITEMNAME.tar $ITEMNAME; then
        rm -fr $ITEMNAME
    fi
    ITEMNAME=$ITEMNAME.tar
fi

if xz $ITEMNAME; then
    echo "Recompressed to $ITEMNAME.xz"
else
    echo "Couldn't process $1 successfully"
fi
