#!/usr/bin/perl

# Copyright 1998 by Marco Budde (Budde@tu-harburg.de)
# GNU GPL license

print "-------   dhelp processing   -------\n";

######################################################
#  copy debian/dhelp to debian/tmp/usr/doc/$package  #
######################################################

if (-f 'debian/dhelp')
{
  open (IN, '< debian/changelog');

  $package = <IN>;
  $package =~ /(.+?)\s.+/;

  if (-d "debian/tmp/usr/doc/$1")
  {
      system ("cp debian/dhelp debian/tmp/usr/doc/$1/.dhelp");
      print "- Installing dhelp file\n";
  }

  close (IN);
}


###################################
#  create postinst/prerm scripts  #
###################################

@find = split (/\n/, `find debian/tmp/usr/doc -noleaf -name .dhelp -print`);

#print "@find ";

if (not -d 'debian/tmp/DEBIAN')
{
    mkdir ('debian/tmp/DEBIAN', 0755);
}

$POSTINST_EX = -f 'debian/tmp/DEBIAN/postinst';
$PRERM_EX = -f 'debian/tmp/DEBIAN/prerm';

open (POSTINST, ">> debian/tmp/DEBIAN/postinst");
open (PRERM, ">> debian/tmp/DEBIAN/prerm");

if (not $POSTINST_EX)
{
    print POSTINST "#!/bin/sh -e\n";
    chmod (0755, 'debian/tmp/DEBIAN/postinst');
}

if (not $PRERM_EX)
{
    print PRERM "#!/bin/sh -e\n";
    chmod (0755, 'debian/tmp/DEBIAN/prerm');
}

print POSTINST "\n# dhelp processing\n";
print POSTINST "if [ -x /usr/sbin/dhelp_parse ]; then\n";
print POSTINST "       /usr/sbin/dhelp_parse -a";

print PRERM "\n# dhelp processing\n";
print PRERM "if [ -x /usr/sbin/dhelp_parse ]; then\n";
print PRERM "       /usr/sbin/dhelp_parse -d";

foreach $entry (@find)
{
    $entry =~ /debian\/tmp(.+).dhelp$/;
    print POSTINST " $1";
    print PRERM " $1";
    print "- Adding $1 to postinst/prerm script\n";
}

print POSTINST "\nfi\n\n";
print PRERM "\nfi\n\n";

close POSTINST;
close PRERM;

print "------------------------------------\n";



