#!/usr/bin/perl -w
##
## Copyright (c) 2001, 2002, Amnon BARAK (amnon@MOSIX.org).
## All rights reserved.
## This software is subject to the MOSIX SOFTWARE LICENSE AGREEMENT.
##
## THIS SOFTWARE IS PROVIDED IN ITS "AS IS" CONDITION, WITH NO WARRANTY
## WHATSOEVER. NO LIABILITY OF ANY KIND FOR ANY DAMAGES WHATSOEVER RESULTING
## FROM THE USE OF THIS SOFTWARE WILL BE ACCEPTED.
##
##
## Author(s): Shlomo Matichin , A. Spanier
##

# This script installs the manaul pages.

# Default arguments currently compatible with all linux distributions. 

use Getopt::Long;
use FindBin;
use lib "$FindBin::Bin";
use common;

$man_path       = "/usr/local"            ;
-d "/usr/share/man" and ($man_dir = "/usr/share/man") or
-d "/usr/local/man" and ($man_dir = "/usr/local/man") or
($man_dir = "/usr/man");

GetOptions( 	"man_dir=s" ,	 	\$man_dir ,
		"help" ,		\$usage ,
		"uninstall" ,		\$uninstall );

$man_dir =~ s/\/$// ;

usage() if defined $usage ;
-e "$source_dir/manuals.tar" or dead "MOSIX Distribution is not intact: Unable to find $source_dir/manuals.tar";

if ( (-w $man_path) and (!(-e $man_dir)) ) {
	system("mkdir $man_dir") and dead "Failed creating $man_dir";
}
	
-w $man_dir or dead "You don't have permission to write to $man_dir";

uninstall() and exit 1 if defined $uninstall ;
install();
exit 1;
    
sub install
{
	system( "tar -x -f $source_dir/manuals.tar --directory $man_dir" ) and
		dead "tar reported problems while extracting $source_dir/manuals.tar into $man_dir:\n".
			"installation of manuals failed\n";

	patch_distribution("$source_dir/manuals.tar",$man_dir) and dead "Failed to patch $distribution files";
	exit ;
}

sub usage {
	print "Usage : install_manuals [OPTION]\n";
	print "--man_dir=      the manuals files directory (default: /usr/local/man)\n";
	print "--uninstall     uninstall\n";
	print "--help          this message\n";		
	exit ;
}

sub uninstall {
	chomp( @files = `tar -tf $source_dir/manuals.tar` );
	foreach $file( @files ) {
		$file =~ s/^\.\/// ;
		-e "$man_dir/$file" and 
			system( "rm -f $man_dir/$file" ) and
				print "\nFailed to delete $man_dir/$file\n";
	}
	exit;
}
