#!/usr/bin/expect -f
################################################################################
# Filename: ./src/base/sbin/qrm-set-nfs-permissions
# 
# The contents of this file are subject to the Qlusters Public License
# Version 1.1 (the License); you may not use this file except in compliance
# with the License.
# You may obtain a copy of the License at
# http://www.openqrm.org/content/view/33/69/.
# 
# Software distributed under the License is distributed on an AS IS basis,
# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
# the specific language governing rights and limitations under the License.
# 
# The Original Code is openQRM.
# 
# The Initial Developer of the Original Code is Qlusters Corporation. Portions
# created by Qlusters Corporation are Copyright (C) 2003-2005. All Rights
# Reserved.
# 
# Contributor(s):
# 
# Description:
# 
# Comments:
################################################################################


# paramters 
# 1 : nfs/netapp		-> to choose if to run on a regular nfs or net-app server
# 2 : <nfs/netapp-ip-address>	-> the ip of the nfs/net-app server
# 3 : <password>		-> the root password of the nfs/net-app server
# 4 : <export-path>		-> exported path to adjust permission
# 5 : on/off			-> if the permission for the clients should be turned on or off
# 6 : <ip-address>		-> the ip-address of the client(s) to be allow rw access
# 7- 1000 : <ip-address>	-> more client ip-address separated by a whitespace

# examples :
# net-app -> turn on permission to mount /vol/vol0/myimage for client 192.168.0.1
#	./set-nfs-permissions netapp [my-net-app-server] [password] /vol/vol0/myimage on 192.168.0.1

# net-app -> turn off permission to mount /vol/vol0/myimage for client 192.168.0.1
#	./set-nfs-permissions netapp [my-net-app-server] [password] /vol/vol0/myimage off 192.168.0.1

# net-app -> turn on permission to mount /vol/vol0/myimage for client 192.168.0.1 and 192.168.0.10
#	./set-nfs-permissions netapp [my-net-app-server] [password] /vol/vol0/myimage on 192.168.0.1 192.168.0.10

# nfs -> turn on permission to mount /usr/myimage for client 192.168.0.1
#./set-nfs-permissions nfs [my-nfs-server] [password] /usr/myimage on 192.168.0.1

# nfs -> turn off permission to mount /usr/myimage for client 192.168.0.1
#./set-nfs-permissions nfs [my-nfs-server] [password] /usr/myimage off 192.168.0.1

# nfs -> turn on permission to mount /usr/myimage for client 192.168.0.1 and 192.168.0.10
#./set-nfs-permissions nfs [my-nfs-server] [password] /usr/myimage on 192.168.0.1 192.168.0.10



# correct usage
if {[llength $argv] <  6} {
	puts stderr "Usage: $argv0 nfs/netapp <nfs/netapp-ip-address> <password> <export-path> on/off <ip-address> <ip-address> <ip-address> ...\n"
	exit 1
}


set force_conservative 0  ;# set to 1 to force conservative mode even if
                          ;# script wasn't run conservatively originally
if {$force_conservative} {
        set send_slow {1 .1}
        proc send {ignore arg} {
                sleep .1
                exp_send -s -- $arg
        }
}


set nfs_type [lindex $argv 0]
set nfs_server [lindex $argv 1]
set nfs_password [lindex $argv 2]
set nfs_path [lindex $argv 3]
set nfs_permission [lindex $argv 4]
set nfs_client [lrange $argv 5 1000]

set nfs_option_netapp ""
set nfs_option_nfs ""
set loop_out "1"
set count 5
set count2 6

set timeout 1		;# needed for the sshd to answer


# prepare the nfs-options
while {$loop_out != ""} {

   if {$count == 5} {
      set nfs_option_netapp "[lrange $argv $count $count]"
      set nfs_option_nfs "[lrange $argv $count $count]:$nfs_path"

   } else {
      set nfs_option_netapp "$nfs_option_netapp:[lrange $argv $count $count]"
      set nfs_option_nfs "$nfs_option_nfs [lrange $argv $count $count]:$nfs_path"
   }

   set loop_out [lrange $argv $count2 $count2]
   set count [expr $count+1]
   set count2 [expr $count2+1]

}

set nfs_option_netapp "$nfs_option_netapp $nfs_path"

# debug
#puts "final"
#puts "nfs_option_netapp $nfs_option_netapp    nfs_option_nfs $nfs_option_nfs    loop_out $loop_out\n"

# main

# netapp
if {$nfs_type == "netapp"} {
	if {$nfs_permission == "on"} {
	        set nfs_exportfs "exportfs -p rw=$nfs_option_netapp"
	} elseif {$nfs_permission == "off"} {
		set nfs_exportfs "exportfs -u -o rw=$nfs_option_netapp"
	} else {
        	puts stderr "Error: Wrong parameter. Permissions can be turned 'on' and 'off'\n"
	        exit 1
	}

	spawn ssh $nfs_server $nfs_exportfs

# regular nfs
} elseif {$nfs_type == "nfs"} {
       if {$nfs_permission == "on"} {
                set nfs_exportfs "exportfs -o rw,insecure,no_root_squash,async $nfs_option_nfs"
        } elseif {$nfs_permission == "off"} {
                set nfs_exportfs "exportfs -u $nfs_option_nfs"
        } else {
                puts stderr "Error: Wrong parameter. Permissions can be turned 'on' and 'off'\n"
                exit 1
        }

	spawn ssh $nfs_server $nfs_exportfs

} else {
	puts stderr "Error: Wrong parameter. Only 'nfs' and 'netapp' are supported\n"
        exit 1
}



match_max 100000
expect "(yes/no)? " {
	send -- "yes\r"
}
expect "password: " {
	send -- "$nfs_password\r"
}

expect eof
















