#!/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 
##

# This script creates the MFS file-system mount point at /mfs.

# Default arguments currently compatible with all Linux distributions

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

$mount_point = "/mfs";

GetOptions(	"mount_point=s" ,	\$mount_point ,
		"nodfsa" ,		\$no_dfsa ,
		"help" ,		\$usage ,
		"uninstall" ,		\$uninstall );

$mount_point =~ s/\/$// ;

usage() if defined $usage ;
-w "/etc/fstab" or
	dead "You do not have write permission on /etc/fstab";
@mount_point_dir = split /\// , $mount_point ;
pop @mount_point_dir;
$mount_point_dir = join '/' , @mount_point_dir;
$mount_point_dir .= '/' ;
-d $mount_point_dir or
	dead "Directory $mount_point_dir does not exist";
-w $mount_point_dir or
	dead "You do not have write permission on $mount_point_dir";
uninstall() and exit 1 if defined $uninstall ;
install();
exit 1;
    
sub install
{
	$mfs or $mfs = '/mfs' ;
	-e "/etc/fstab.pre_mosix" and
		dead "Please uninstall MOSIX before reinstalling";
	`grep \\\\bmfs\\\\b < /etc/fstab 2>&1` =~ /\bmfs\b/ and
		dead "Please uninstall MOSIX before reinstalling";
	-e $mount_point and
		dead "The mount point must be a new directory";
	mkdir( $mount_point , 0777 ) or
		dead "Unable to create directory $mount_point";
	system( "cp -f /etc/fstab /etc/fstab.pre_mosix" ) and
		dead "Unable to backup /etc/fstab";
	open( FSTAB , ">> /etc/fstab" ) or
		dead "Unable to open /etc/fstab";
	if( not defined $no_dfsa ) {
		print FSTAB "\nnone\t\t\t$mount_point\t\t\tmfs\tdfsa=1\t\t0 0\n";
	} else {
		print FSTAB "\nnone\t\t\t$mount_point\t\t\tmfs\tdefaults\t0 0\n";
	}
	close( FSTAB );
	exit;
}

sub usage {
	print "Usage : mfs [OPTION]\n";
	print "--mount_point=      new directory that will be the mount point (default: /mfs)\n";
	print "--nodfsa            do not enable DFSA on MFS\n";
	print "--uninstall         uninstall\n";
	print "--help              this message\n";		
	exit ;
}

sub uninstall {
	unless( `grep \\\\bmfs\\\\b < /etc/fstab 2>&1` =~ /\s*(\S+)\s+(\S+)/ and $mount_point = $2 ) {
		print "\nYou do not appear to have an mfs mount point!\n";
		system( "rm -fr /mfs >/dev/null 2>&1" );
		exit ;
	}
	if (-e "/etc/fstab.pre_mosix") {
		system( "mv -f /etc/fstab.pre_mosix /etc/fstab" ) and
			dead "Failed restoring the pre-MOSIX back-copy of /etc/fstab";
	}
	else {
		print "\nUnable to find the pre-MOSIX backup of /etc/fstab:\n";
		print "please check /etc/fstab to make sure no extra MFS entry remains there.\n";
	}
	system( "umount $mount_point >/dev/null 2>&1" );
	system( "rmdir $mount_point" ) and
		dead "Unable to remove directory $mount_point";
	exit ;
}
