#!/bin/bash
#written by David Hobach around 2010
#convention: file suffix = pack call parameter
#version: 0.8
#hackingthe.net/coding/misc.html

#NOTE: some strange bugs (recursion) will occur, if you use e.g. "gz" as function name
if [ $# -ge 1 ] 
then
file="$(basename "$1")"
dir="$(dirname "$1")"
cd "$dir"
fi

function tarFunc {
tar -cvf "$file.tar" "$file"
}

function gzFunc {
tar -zcvf "$file.tar.gz" "$file"
}

function bzFunc {
tar -jcvf "$file.tar.bz2" "$file"
}

function rarFunc {
rar a -r "$file.rar" "$file"
}

function SzFunc {
7z a "$file.7z" "$file"
}

function zipFunc {
7z a -tzip "$file.zip" "$file"
}

function SzencFunc {
7z a -p -mhe=on "$file.7z" "$file"
}

function zencFunc {
7z a -tzip -p "$file.zip" "$file"
}

case "$2" in
	tar)
	tarFunc
	;;

	gz)
	gzFunc
	;;
	
	bz2)
	bzFunc
	;;
	
	rar)
	rarFunc
	;;
	
	7z)
	SzFunc
	;;

	zip)
	zipFunc
	;;

	7zenc)
	SzencFunc
	;;

	zenc)
	zencFunc
	;;

	*)
		if [ $# -eq 1 ]
		then
		gzFunc
		else
		echo "Usage: $0 <directory> tar|gz|bz2|rar|7z|zip|7zenc|zenc"
		fi
	;;
esac
