#!/bin/env bash

#Author: Drew Anderson
#Purpose: Decompressing archives containing VMs for ReAssure
#Date: 14 July, 2006
#Version: 0.2

target="$1"

#get whatever is after the last . character
ext=`echo "$target" | sed -e 's/.*[.]//g'`


#echo "DEBUG: |$ext|"

#ext will never be empty becuase if there is no '.' the entire input string
#will be assigned to it, so in order to check for lack of an extension, I 
#just check to see if ext and target are the same
if [ "$ext" == "$target" ]
then
	echo "ERROR: Could not determine file extension from \"$target\""

elif [ "$ext" == "gz" ];
then
	tar --extract --file="$target" && rm -f "$target"

elif [ "$ext" == "tgz" ];
then
	tar --extract --file="$target" && rm -f "$target"

elif [ "$ext" == "bz2" ];
then
	tar --extract --file="$target" && rm -f "$target"

elif [ "$ext" == "tbz" ];
then
	tar --extract --file="$target" && rm -f "$target"

elif [ "$ext" == "zip" ];
then
	unzip "$target" && rm -f "$target"

#elif [ "$ext" == "new_format_that_needs_support" ]
#then
#
#	command to extract new format
#

else #defaults to an error if it doesn't catch on any of the known ones.

	echo "ERROR: \"$ext\" is an unknown file type!"

	#ADD ANY NEW TYPES ABOVE THIS DEFAULT OR THINGS WILL BREAK
	#(just fyi)
fi
