#!/usr/bin/perl

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



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

sub copy 
{
  if ($package eq $package_ch)
  {
    if (-f 'debian/dhelp')
    {
      if (-d "$dir/usr/share/doc/$package")
      {
          system ("cp debian/dhelp $dir/usr/share/doc/$package/.dhelp");
          print "- Installing dhelp file\n";
      }
    } 
  }
  else
  {
    if (-f 'debian/dhelp.$package')
    {
      if (-d "$dir/usr/share/doc/$package")
      {
        system ("cp debian/dhelp $dir/usr/share/doc/$package/.dhelp");
        print "- Installing dhelp file\n";
      }
    }
  }
}


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

sub scripts
{
  @find = split (/\n/, `find $dir/usr/share/doc -noleaf -name .dhelp -print`);

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

  open (POSTINST, ">> debian/postinst.debhelper");
  open (PRERM, ">> debian/prerm.debhelper");

  ### fhs ###

  print POSTINST "\n# dhelp processing (automatically added by dh_dhelp)\n";
  print POSTINST "if [ -x /usr/sbin/dhelp_parse ]; then\n";
  print POSTINST "       /usr/sbin/dhelp_parse -a";

  print PRERM "\n# dhelp processing (automatically added by dh_dhelp)\n";
  print PRERM "if [ -x /usr/sbin/dhelp_parse ]; then\n";
  print PRERM "       /usr/sbin/dhelp_parse -d";

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

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

  ### fsstnd (Debian 2.1) ###

#  print POSTINST "\n# dhelp processing (automatically added by dh_dhelp)\n";
#  print POSTINST "if [ -x /usr/sbin/dhelp_parse_fsstnd ]; then\n";
#  print POSTINST "       /usr/sbin/dhelp_parse_fsstnd -a";

#  print PRERM "\n# dhelp processing (automatically added by dh_dhelp)\n";
#  print PRERM "if [ -x /usr/sbin/dhelp_parse_fsstnd ]; then\n";
#  print PRERM "       /usr/sbin/dhelp_parse_fsstnd -d";

#  foreach $entry (@find)
#  {
#      $entry =~ /$dir\/usr\/share\/doc(.+).dhelp$/;
#      print POSTINST " /usr/doc$1";
#      print PRERM " /usr/doc$1";
#      print "- Adding /usr/doc$1 (fsstnd) to postinst/prerm script\n";
#  }

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

  close POSTINST;
  close PRERM;
}


###################################
#            main                 #
###################################

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

$dir = "debian/tmp";                # defaults
open (IN, '< debian/changelog');
$package_ch = <IN>;
$package_ch =~ /(.+?)\s.+/;
$package_ch = $1;
$package = $package_ch;
close (IN);

foreach (@ARGV)         # parse command line
{
  if (/^-P(.+)$/)       # build dir  
  {
    $dir = $1;
  }
  elsif (/^-p(.+)$/)    # package name
  {
    $package = $1;
  }
}

&copy;
&scripts;

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



