#!/usr/bin/perl -w
# -*- coding: utf-8 -*-
# Convert GNOME 1 .desktop files (e.g gnome-help-browser → yelp)
# Copyright © 2002 Colin Walters <walters@verbum.org>
# $Id: gnome-launchers-1-to-2,v 1.8 2002/10/23 18:12:58 walters Exp $

# 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

###### #### ### ## # # #
# This script should be run in the user's home directory,
# AFTER the gnome-panel-1-to-2 script has run.
###### #### ### ## # # #

my $no_act = 0;
foreach my $arg (@ARGV) {
  if ($arg eq '-n') {
    $no_act = 1;
  }
}

sub log {
  print "gnome-launchers-1-to-2: " . join(' ', @_) . "\n";
}

# stuff copied from gnome-panel-1-to-2
sub system_internal {
  my @cmd = @_;
  if (not $no_act) {
    system(@cmd) == 0 or die "Command \"" . join(' ', @cmd) . "\" exited with an error: $?";
  }
}

sub mysystem {
  my @cmd = @_;
  &log("running: " . join(' ', @_));
  system_internal(@_);
}

my %launcher_exec_map = ('gnomecc' => "/usr/share/gnome-session/debian/nautilus-666666.desktop",
			 'gnome-help-browser' => "/usr/share/applications/yelp.desktop");

foreach my $key (keys(%launcher_exec_map)) {
  if (! -f $launcher_exec_map{$key}) {
    &log("$key does not exist, not transitioning");
    delete $launcher_exec_map{$key};
  }
}
my @desktopfiles = ();

foreach my $source (".gnome2/panel2.d/default/launchers/", "Desktop/") {
  if (-d $source) {
    &log("Reading $source for .desktop files");
    opendir (DIR, $source) or die "Couldn't open $source: $!";
    push @desktopfiles, map { $source . $_ } (grep { /.desktop$/ } readdir (DIR));
    closedir (DIR);
  }
}

foreach my $file (@desktopfiles) {
  &log("Scanning $file");
  open(LAUNCHER, $file) or die "Couldn't open $file\n: $!";
  my @lines = <LAUNCHER>;
  close(LAUNCHER);
  my $replace = undef;
  foreach my $line (@lines) {
    if ($line =~ /^Exec=(.+?)\s*$/i) {
      my $exec = $1;
      &log("$file" . ": exec=" . $exec);
      if (defined $launcher_exec_map{$exec}) {
	$replace = $launcher_exec_map{$exec};
      }
    }
  }
  if ($replace) {
    &mysystem("cp", $replace, "$file")
  }
}

# Local Variables:
# compile-command: "perl -c gnome-launchers-1-to-2"
# End:
