#!/usr/bin/perl -w use strict; # This script restores the kde mimetypes associations that existed before # update_menu ran, where possible (i.e. if the file wasn't removed nor moved). # Input: /tmp/update-menu-<uid>-kdemimetypes generated by savekdemimetypes.sh # 06/09/2000: Initial Version. David Faure <david@mandrakesoft.com> # 05/04/2001: Rewritten in perl, with path on cmdline, and optimizations (DF) if ( $#ARGV != 0 ) { print STDERR "Usage : restorekdemimetypes.pl ....../share/applnk-mdk (either user or global)\n"; exit 1; } # Looks for $key in $file, and makes its value $value # (either replacing the current line, or appending a new one) sub add_or_replace { my ($file, $key, $value) = @_; my $data = ""; # print STDERR "add_or_replace $file $key $data\n"; open (FILEIN, "<$file") or return; # just skip this file if not there anymore # Read the contents of the file seek(FILEIN, 0, 2); my $fsize = tell(FILEIN); seek(FILEIN, 0, 0); read FILEIN, $data, $fsize; close(FILEIN); # Look for existing key my $lookup = '\n' . $key . '[ \t]*=[ \t]*([^\n]*)\n'; if ($data =~ m/$lookup/) { return if ( $1 eq $value ); # Value unchanged $data =~ s/$lookup/\n$key=$value\n/; } else { print STDERR "$file: $lookup NOT FOUND\n"; # Laurent don't readd it otherwise koffice doesn't work :( $data .= "$key=$value\n"; } open (FILEOUT, ">$file") or die "Couldn't write into $file"; print FILEOUT $data; close FILEOUT; print STDERR "Wrote $file\n"; } my $input="$ENV{HOME}/tmp/update-menu-$<-kdemimetypes"; my $l = 0; open (INPUT, "<$input") or die "No $input file. restorekdemimetypes.pl wasn't run !"; while (<INPUT>) { $l++; if ( /^(.*)::ServiceTypes[ \t]*=[ \t]*([^\n]*)/ ) { add_or_replace $1, "ServiceTypes", $2; } elsif ( /^(.*)::MimeType[ \t]*=[ \t]*([^\n]*)/ ) { add_or_replace $1, "MimeType", $2; } else { print STDERR "Malformed line $l in $input : $_\n"; } } close (INPUT); unlink ($input);