#!/usr/bin/perl
#
#  Script for creating search indices for help system
#
#  This file is part of the SuSE help system.
#
#  Copyright (C) 2002  SuSE Linux AG, Nuernberg
#  Copyright (C) 2005  SUSE Linux Products GmbH, Nuernberg
#
#  Author: Cornelius Schumacher <cschum@suse.de>
#
#  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

do "/usr/share/susehelp/susehelprc";

push @INC, $susehelpcgidir;

use Getopt::Long;

require susehelp;
import susehelp;

sub dbg($) {
  $dbg && print "$_[0]\n";
}

GetOptions (
  'lang=s' => \$lang,
  'force' => \$force,
  'show' => \$show,
  'verbose' => \$verbose,
  'dryrun' => \$dryrun,
  'help' => \$help,
);

if ( $help ) { &usage(); }

my $doc = $ARGV[ 0 ];

print "DOCUMENT: $doc\n";

if ( $< != 0 ) {
  print "susehelpindex needs to be started as root for creating index files.\n";
  $show = 1;
}

if ( !$lang ) { $lang = substr ( $ENV{ LANG }, 0, 2 ); }
if ( !$lang ) { $lang = "en"; }

if ( $show ) {
  print "Show index state:\n\n";
} else {
  print "Creating index files...\n";
}

&traverseMetaDir( "", 9999, $lang, "::indexFileEntry", "::indexDirEntry",
                  "::up", "::down", 1 );

if ( $count ) {
  if ( $show ) {
    print "$count indexable documents.\n";
  } else {
    if ( $dryrun ) {
      print "Would have created index for $count documents.\n";
    } else {
      print "\nCreated index for $count documents.\n";
    }
  }
} else {
  print "No indexable documents.\n";
}

exit 0;

sub indexFileEntry($$)
{
  my $docEntry = shift;
  my $fileName = shift;
  
  my $indexer = $docEntry->indexer;
  my $testfile = $docEntry->indextestfile;
  my $name = $docEntry->name;
  my $identifier = $docEntry->identifier;
  my $docpath = $docEntry->docpath;
  my $doctype = $docEntry->doctype;
  
  if ( $doc && $doc ne $identifier ) { return; }

  if ( $doctype eq "text/docbook" ) {
    $docpath = "dummy";
  } else {
    if ( !$docpath || !-e $docpath ) { return; }
  }

  if ( $indexer ) {
    if ( $show ) {
      if ( -e $testfile ) {
        print "ok     : ";
      } else {
        print "missing: ";
      }
      print "$identifier ($name)\n";
      $count++;
    } else {
      if ( $force || !-e $testfile ) {
        $indexer .= " --indexdir $default_indexdir";
        $indexer .= " --docpath $docpath";
        $indexer .= " --identifier $identifier";
        if ( $verbose ) {
          print "Execute: $indexer\n";
        } else {
          print ".";
        }
        if ( !$dryrun ) {
          dbg( "Indexer: $indexer");
          if ( $dbg ) {
            system( "$indexer" );
          } else {
            system( "$indexer 2>/dev/null >/dev/null" );
          }
        }
        $count++;
      } else {
        if ( $verbose ) { print "Index exists: '$testfile'\n"; }
      }
    }
  }
}

sub indexDirEntry($)
{
  my $docEntry = shift;

  indexFileEntry( $docEntry, ".directory" );
}

sub up()
{
}

sub down()
{
}

sub usage()
{
  print <<EOF;
Usage: susehelpindex [options] [document-identifier]

  Options:
    --force        Recreate all index files
    --lang <lang>  Set language for which the indices are created. (Default: en)
    --show         Only show status of indices.
    --verbose      Show verbose information when creating index files.
    --dryrun       Don't actually create files.
    --help  Show this text.

  Arguments:
    document-identifier  Identifier of document to index
EOF

  exit 1;
}
