#!/usr/bin/perl  

# chmanconfig
# == updates /etc/manpath.config with the <lang> directories ==
#
# Tipical Debian usage in installation scripts of manpages translations:
# in postinst:
#	chmanconfig --add --lang=de_DE
# in prerm:
#	chmanconfig --remove --lang=de_DE
#
# This script scans the file argument of the -f option 
# (default /etc/manpath.config) searching for rows beginning 
# with the keyword given as argument of the -k option
# (default MANDB_MAP) which has the manpath field "/usr/man" 
#
# It copies the row adding the subdir given as argument of the 
# -l option (mandatory) to both fields manpath and catpath.
# This action is performed only when the -a (add) flag is given.
#
# After that it outputs the original row and all the rest.
#
# if it founds a row listing /usr/man/<lang> this is commented out.
# This action is performed anyway.
#
# when the -a flag is not present the flag -r flag must be present.
#
# chmanconfig - (c) 1996 by Fabrizio Polacco <fpolacco@debian.org>
# This program is under GPL License.
#

my $usage = "Usage:\nchmanconfig --{add,remove} \n".
		"\t -l --lang=<langpath> lang value\n".
		"\t -f --file=<filename> default /etc/manpath.config\n".
		"\t -k --keyword=<key> default MANDB_MAP\n".
		"\t --help --VERSION --quiet --verbose\n";

use Getopt::Long;
$Getopt::Long::ignorecase = 0;	# distinct Upper 

$base = "/usr/man";
die "$base not found or not readable\n" if ! -r $base;
# initialising options
$opt_file = "/etc/manpath.config";	# default value
$opt_keyword = "MANDB_MAP";		# default value
local $opt_help;	# used only once

die $usage if !		# in case of error
&GetOptions 
	('add'		# -a		--add
	,'remove'	# -r		--remove
	,'lang=s'	# -l <string>	--lang <string>
	# optional flags
	,'file=s'	# -f <string>	--file <string>
	,'keyword=s'	# -k <string>	--keyword <string>
	#
	,'help'		# -h		--help
	,'VERSION'	# -V		--VERSION
	,'quiet'	# -q		--quiet
	,'verbose'	# -v		--verbose
	);
die $usage if $opt_help;

die "lang name missing\n" . $usage if ! $opt_lang;
die "action flag missing (--add, --remove).\n" . $usage
	if ((! $opt_add) and (! $opt_remove));
die "too much action flags (--add and --remove).\n" . $usage
	if ( $opt_add and $opt_remove);

$OUT = " $opt_file";
open( OUT) or die "cannot copy file $OUT\n";
$IN = " $opt_file" . ".bak";
open( IN, "> $IN") or die "cannot copy to $IN\n";
print IN <OUT>;		# copy original file
close( IN); close( OUT);

open( IN,  "< $IN ") or die "cannot open input file $IN\n";
open( OUT, "> $OUT") or die "cannot open output file $OUT\n";
while( <IN>) {
	print OUT $_ and next if ! m|$opt_keyword| ;

	# process lines containing the keyword
	print OUT $_ and next if m|^[\t ]*#| ;
	#print OUT "# ", $_ and next if m|$base/$opt_lang| ;
	next if m|$base/$opt_lang| ;
	print OUT $_ and next if ! m|[\t ]*$base[\t ]*| ;
	print OUT $_ and next if m|[\t ]*$base/| ;

	# process only lines declaring $base as global_manpath
	($key, $global, $relative) = split;
	print OUT "$key\t$global/$opt_lang\t\t$relative/$opt_lang\n" if $opt_add;
	print OUT ;
	print OUT <IN>;
}
close( IN); close( OUT);

# catman dirs MUST be owned by man
if ( $opt_add ) {
	# create the cat directories only if the man directories exist
	$res=`mkcatdirs man root 0755`;
	print $res if $opt_verbose;
} else {
	system "su man -c \"rm -rf $relative/$opt_lang \"";
}

