#!/bin/bash

if [ -z "$1" ]; then
	echo "Parameter missing"
	exit 2;
fi

if [ ! -d "$1" ]; then
	echo "Not a directory"
	exit 1
fi

if [ "$1" = "/" ]; then
	echo "No. Just no."
	exit 1
fi

TRIMMED=$(echo $1 | sed 's:/*$::')

#tar cvpjf "$1.tar.bz2" "$1"
BZIP=--best tar -cvpjf "$TRIMMED.tar.bz2" "$1"

RES=$?

# TODO: should we also accept exitcode "1" as success? ( https://stackoverflow.com/questions/20318852/tar-file-changed-as-we-read-it )
if [ $RES -eq 0 ]; then
	rm -R "$1"
else
	echo "Error code $RES"
	exit $RES
fi

