#!/bin/bash
#JKA - ZIP
#A script for de/compress files and folders in many formats
#Author: JKA Network - contacto@jkanetwork.com
#Function decompress(); decompress the file whith the format especified

# gettext initialization
#export TEXTDOMAIN='jkazip'
#peroexport TEXTDOMAINDIR='/usr/share/locale'

#function sacaFormato, get the file extension to zip or unzip
function sacaFormato() {
	local var=$1
	cant=$(echo $var | grep -o "\." | wc -l)
	fieldPost=$(expr $cant + 1)
	postform=$(echo $var | cut -f$fieldPost -d".")
	preform=$(echo $var | cut -f$cant -d".")
	if [[ $preform = "tar" ]];then
		formato=$(echo ${preform}.${postform})
	else
		formato=$postform
	fi
}
#Function msg, print in stdout a final message
function msg() {
	local var=$1
	printf -- "$(gettext "File/s %s has/have been compressed")" $var
}
#Function msg2, print in stdout a final message
function msg() {
	local var=$1
	printf -- "$(gettext "File has been decompressed")" $var
}

version="1.1.0"
if [ -z $1 ];then
	error "$(gettext "No option specified, use jkazip -h")"
elif [ $1 = "-h" ];then
	echo "$(gettext "	synopsis:	jkazip <options> <file or directory>")"
	echo ""
	echo "$(gettext "	-d,		decompress file")"
	echo "$(gettext "	-c <output file> <input file>,	compress file")"
	echo "$(gettext "	-h,		show help box and exit")"
	echo "$(gettext "	-v,		show version and exit")"
	echo ""
	echo "$(gettext "	The order must be \"jkazip -d <file or directory>\" or \"jkazip -c <format> <file or directory\"")"
	echo ""
	echo "$(gettext "	Examples")"
	echo "$(gettext "	jkazip -d file.7z")"
	echo "$(gettext "	jkazip -c ouputfile.tar.gz directory")"
	echo ""
	echo "$(gettext "	Supported formats: tar, gzip, bzip2, xzip, lzip, 7z, zip and rar")"
	echo "$(gettext "	Only one file or directory (some formats don't support directories), for more options use the each program individualy")"
	echo ""
elif [ $1 = "-v" ];then
	printf -- "$(gettext "jkazip by JKA Network; version %s")" "$version"
	echo
elif [ $1 = "-d" ];then
	if [ -z $2 ];then
		error "$(gettext "No file or directory specified")"
		exit
	elif [ -f $2 ];then
		printf -- "$(gettext "File %s will be decompressed")" "$2"
		echo
	else
		error "$(gettext "%s no such file or directory")" "$2"
		echo
		exit
	fi
	entrada=$2
	sacaFormato $entrada
	case $formato in
		"tar" | "tar.xz" | "tar.lz" | "tar.gz" | "tar.bz2" | "tgz" | "tbz" | "txz" | "tlz")
			tar xvf $entrada
			msg2 $entrada
			;;
		"7z")
			7z x $entrada
			msg2 $entrada
			;;
		"zip")
			unzip $entrada
			msg2 $entrada
			;;
		"gz" | "lz")
			formato="${formato}ip"
			$formato -dk $entrada
			msg2 $entrada
			;;
		"xz")
			$formato -dk $entrada
			msg $entrada
			;;
		"bz2")
			bzip2 -dk $entrada
			msg $entrada
			;;
		"rar")
			unrar x $entrada
			;;
		*)
			error "$(gettext "File extension %s are not supported")" $formato
			;;
	esac

elif [ $1 = "-c" ];then
	salida=$2
	if [ -z $3 ];then
		echo "$(gettext "No file or directory specified")"
		exit	
	else
		for i in $@
		do
			if [[ $i != $1 && $i != $2 ]];then
				if [ ! -d $i -a ! -f $i ];then
					printf -- "$(gettext "%s no such file or directory")" "$3"
					echo
					exit
				fi
			fi
		done
	fi
	files=$3
	for (( i=4; i<=$#; i++))
	do
		files="$files $(echo $* | cut -f$i -d" ")"
	done
	sacaFormato $salida
	case $formato in
		"tar" | "tar.xz" | "tar.lz" | "tar.gz" | "tar.bz2" | "tgz" | "tbz" | "txz" | "tlz")
			tar cvfa $salida $files
			msg $files
			;;
		"7z")
			7z a $salida $files
			msg $files
			;;
		"zip")
			zip $salida $files
			msg $files
			;;
		"gz" | "lz")
			formato="${formato}ip"
			$formato -9kc $3 > $salida
			msg $3
			;;
		"xz")
			$formato -9kc $3 > $salida
			msg $3
			;;
		"bz2")
			$formato -9kc $3 > $salida
			msg $3
			;;
		"rar")
			error "$(gettext "rar compression are not supported")"
			;;
		*)
			error "$(gettext "File extension %s are not supported")" $formato
			;;
	esac
else
	echo "$(gettext "Unknown option")"
fi