#!/bin/sh
#This is a script for Velocity by Kyle Davis
#NOTE:  we use echo -en for things that go to Velocity to avoid a blank line in our context menus

version="1.2"
name="Create compressed tarball"
authors="Kyle Davis"
about="Create a compressed tarball from a directory"
mimetypes="x-directory/normal"
dependancies="gdialog,tar,gzip,bzip2"
needterm="yes"
multiplefiles=""	#TODO:  Support multiple directories here...
localonly="Yes"


case $1 in
	--get-info)
		#DO NOT CHANGE THIS LINE, THIS IS THE VELOCITY SCRIPT CONFIG FORMAT
		echo -en "$version\n$name\n$authors\n$about\n$mimetypes\n$dependancies\n$needterm\n$multiplefiles\n$localonly"
		;;
	--execute)
		#SH doesn't seem to like you going over 9 variables...so we shift to keep things sane
		shift;windowx=$1
		shift;windowy=$1
		shift;windowwidth=$1
		shift;windowheight=$1
		shift;location=$1
		#Start deps
		shift;dgdialog=$1
		shift;dtar=$1
		shift;dgzip=$1
		shift;dbzip2=$1
		#End deps
		shift;file=$1
		shift;mimetype=$1

		gziph="gzip -9"
		gzipm="gzip -6"
		gzipl="gzip -1"

		#To avoid GTK+ warnings in gdialog's return data
		export LC_ALL="POSIX"
		gd2versions="`echo -en 'Gnome gdialog 2.2.\nGnome gdialog 2.1.'`";
		gdialog2=`$dgdialog --version 2>&1 | grep -F "$gd2versions"`;
		if [ ! "$gdialog2" ]; then
			compression=`$dgdialog --title "$name - Step 1" --radiolist "Which compression type would you like to use?" 400 300 4 "bzip2" "[highest]" 1 "$gziph" "[normal]" 0 "$gzipm" "[low]" 0 "$gzipl" "[lowest]" 0 2>&1 | tail -n 1`;
		else
			compression=`$dgdialog --title "$name - Step 1" --menu "Which compression type would you like to use?" 400 300 5 "bzip2" "[highest]" "$gziph" "[normal]" "$gzipm" "[low]" "$gzipl" "[lowest]" 2>&1 | tail -n 1`;
		fi
		case $compression in
			"")	#If they selected nothing (or hit cancel)...we leave
				exit;
				;;
			bzip2)
				compresser=$dbzip2;
				newfile="$file.tar.bz2";
				;;
			$gziph)
				compresser="\"$dgzip\" -9"
				newfile="$file.tar.gz";
				;;
			$gzipm)
				compresser="\"$dgzip\" -6"
				newfile="$file.tar.gz";
				;;
			$gzipl)
				compresser="\"$dgzip\" -1"
				newfile="$file.tar.gz";
				;;
		esac
		newfile=`$dgdialog --title "$name - Step 2" --inputbox "Name of the archive:" 100 100 "$newfile" 2>&1 | tail -n 1`;
		if [ ! "$newfile" ]; then
			exit;
		fi
		cd "$location"
		"$dtar" -vc "$file" | "$compresser" - > "$newfile"
		cd -
		echo "Keeping this window open for a few moments to allow you to review...just close it when you are done."
		sleep 600
		;;
	*)
		echo "This should only be used by Velocity."
		;;
esac
