#!/usr/bin/perl

# perl script originally by hallon@debian.org, much faster than sed & sh.
# modified by cas to pipe its output through frcode and then > to the
# db file.

use strict;
use File::Basename;

my $program = basename($0);

my $dbfile='/var/lib/dlocate/dlocatedb' ;

my $frcode='/usr/lib/locate/frcode' ;

my $infodir='/var/lib/dpkg/info';

open(FRCODE,"|$frcode >$dbfile.new") || die "$program: couldn't open pipe to $frcode: $!\n" ;

opendir(DIR, $infodir) || die "$program: can't open directory $infodir: $!\n";
my @pkgs = grep { /list$/ && s/\.list// } readdir(DIR);
closedir DIR;

chdir $infodir;
foreach my $pkg (@pkgs) {
    open(FILE, $pkg . ".list") || die "$program: can't open file $pkg: $!\n";
    foreach (<FILE>) {
        print FRCODE $pkg, ": ", $_;
    }
    close FILE;
}
close FRCODE ;

# Create a backup to the database before replacing it with the new database.
# This is effectively two rename's done atomically.
if (-e $dbfile) {
    unlink("$dbfile.old") if (-e "$dbfile.old");
    link("$dbfile", "$dbfile.old") if (-e $dbfile);
}

rename("$dbfile.new","$dbfile") ;
