#! /usr/bin/perl

# 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 3 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 GNU gv; see the file COPYING.  If not, write to
# the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.
# 
# Author:   Markus Steinborn (see CONTRIBUTORS for the e-mail address)

# This program updates ~/.gv by removing all incompatible entries.


@res = ();
%res = ();

open ($file, "<", "$ENV{HOME}/.gv") or die "Cannot open ~/.gv for reading.\n";
while (<$file>)
{
   chomp;
   $read = $_;
   $line = $read;
   while ( $read =~ /\\$/ )
   {
      $read = <$file>;
      chomp $read;
      $line .= "\n$read";
   }
   
   push (@res, $line);
   
   if ($line =~ /^([^:]+):/ )
   {
      $res{$1} = $line;
   }
}
close $file;

$VER = "gv 3.5.8";
$VER = $res{"GV.version"} if defined $res{"GV.version"};

%remove = ();


sub compare
{
   my $a1 = $_[0];
   my $a2 = $_[1];
   
   my $b1 = substr($a1,3);
   my $b2 = substr($a2,3);
   
   my @c1 = split(/\./, $b1);
   my @c2 = split(/\./, $b2);
  
   for (my $i = 0; $i < @c1; $i++)
   {
      return 1 if $c1[$i] < $c2[$i];
      return 0 if $c1[$i] > $c2[$i];
   }
   return 0;
   
   print "$b1\n$b2\n";
}

$remove{"GV.version"} = 1;
$remove{"GV.scales"} = 1 if compare($VER, "gv 3.6.4.90");

$newfile = "";

for $line (@res)
{
   $line =~ /^([^:]+):/ ;
   $name = $1;
   $newfile .= "$line\n" unless defined $remove{$name};
   print "Removing resource $name\n" if defined $remove{$name} && $name ne "GV.version";
}

$newfile .= "GV.version:\t\tgv 3.6.5\n";

open ($file, ">", "$ENV{HOME}/.gv") or die "Cannot open ~/.gv for writing.\n";
print $file $newfile;
close $file;
