#!/usr/bin/env perl
# 			GZip
#			Compress files dropped here
#			by Stephen Watson, based on Archive by Thomas Leonard
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
# 
# Please report any bugs to <stephen@kerofin.demon.co.uk>

sub zip;
sub xdie;
sub my_chdir;

#unless (@ARGV == 1) {
#  xdie "Drop a file onto me and I'll gzip it.\n";
#}

for ($i=0; $i<@ARGV; $i++) {
  $path = $ARGV[$i];

  # Convert relative paths to absolute (we may chdir() later).
  unless ($path =~ /^\//) {
    my $pwd = `pwd`;
    chop $pwd;
    $path = "$pwd/$path";
  }
  
  xdie "'$path' does not exist" unless -e $path;

  zip($path);
}

exit 0;

# Escape shell meta-chars.
sub escape {
	my $path = shift;
	$path =~ s/([^a-z.\/A-Z0-9_-])/\\\1/g;
	#print "Escaped path is '$path'\n";
	return $path;
}

sub zip {
	my $path = shift;

	chop ($dir = `dirname $path`);
	chop ($leaf = `basename $path`);

	chdir $dir;
	system "gzip $leaf";
}


# Display the error message and quit.
sub xdie {
	my $message = "GZip error:\n\n" . shift;

	exec "xmessage", ("-center", "-timeout", "10", $message);
	die "$message\n";
}

sub my_chdir {
	my $dir = shift;

	chdir $dir or xdie "Failed to chdir() to '$dir'";
}
