#!/bin/sh

# $Id: backto40shared,v 1.1.24.1 2004/09/10 17:48:13 jp148923 Exp $

#    SAM-QFS_notice_begin
#
#	Solaris 2.x Sun Storage & Archiving Management File System
#
#	Copyright (c) 2004 Sun Microsystems, Inc.
#	All Rights Reserved.
#
#	Government Rights Notice
#	Use, duplication, or disclosure by the U.S. Government is
#	subject to restrictions set forth in the Sun Microsystems,
#	Inc. license agreements and as provided in DFARS 227.7202-1(a)
#	and 227.7202-3(a) (1995), DRAS 252.227-7013(c)(ii) (OCT 1988),
#	FAR 12.212(a)(1995), FAR 52.227-19, or FAR 52.227-14 (ALT III),
#	as applicable.  Sun Microsystems, Inc.
#
#    SAM-QFS_notice_end

# Script backto40shared
#
# Search the mcf file for any filesystems which have an additional parameters
# set to shared. The keyword shared must be present in order for this to be
# a shared filesystem.

if [ `grep -c shared /etc/opt/SUNWsamfs/mcf` -eq 0 ]; then
	echo No shared filesystems found in mcf to save for 4.0
	exit 0
fi

rm -f /etc/opt/SUNWsamfs/hosts.master.42

FOUND=0
HOSTNAME=`hostname`
OFILE=/etc/opt/SUNWsamfs/hosts.info.42
MFILE=/etc/opt/SUNWsamfs/hosts.master.42
DATE=`/usr/bin/date +%m%d%y-%H%M`

echo Output from backto40shared for server $HOSTNAME > $OFILE
echo backto40shared was run on $DATE >> $OFILE
echo " " >> $OFILE
echo Lines which will be processed from the mcf are the following: >> $OFILE
echo " " >> $OFILE
grep shared /etc/opt/SUNWsamfs/mcf >> $OFILE
echo " " >> $OFILE
echo Results from backto40shared script >> $OFILE
echo " " >> $OFILE

# The use of awk skips any preceeding white space.

for file in `grep shared /etc/opt/SUNWsamfs/mcf | /usr/bin/awk '{print $1}'`
do
	if [ `echo $file | cut -c1` = "#" ]; then
		echo Possible shared filesystem $file is commented out, skipping. >> $OFILE
	else
		echo Running samsharefs on filesystem $file >> $OFILE
		samsharefs -R $file > /tmp/hosts.$file.42
		if [ $? != 0 ]; then
			echo samsharefs command failed for filesystem $file >> $OFILE
		else
#
# If the hostname of this server does not match the server which is
# designated as the server for the shared filesystem we do not save the
# output from the samsharefs command. We only save the output when the
# server of the shared filesystem and the hostname of the server running
# this command are identical
#
			SERVER=`grep server /tmp/hosts.$file.42 | cut -f1 -d" "`
			if [ "$HOSTNAME" = "$SERVER" ]; then
				echo $HOSTNAME is the server for filesystem $file, saving hosts file >> $OFILE
				echo $file >> $MFILE
				mv /tmp/hosts.$file.42 /etc/opt/SUNWsamfs/.
				FOUND=1
			else
				echo $HOSTNAME is not the server for filesystem $file, $SERVER is, skipping >> $OFILE
    			rm /tmp/hosts.$file.42
			fi
		fi
	fi
done

echo " " >> $OFILE

if [ $FOUND -eq 1 ]; then
	cp /opt/SUNWsamfs/sbin/hosts42to40shared /etc/opt/SUNWsamfs/.
	chmod 700 /etc/opt/SUNWsamfs/hosts42to40shared

	cat <</END_OF_TEXT -s >> $OFILE
After 4.2 has been removed and 4.0 has been installed. You will need to
run the hosts42to40shared script. This script will be left in
/etc/opt/SUNWsamfs directory after the 4.2 packages have been removed
/END_OF_TEXT

else
	echo No shared filesystems were found that were owned by $HOSTNAME >> $OFILE
	echo The hosts42to40shared script does not need to be run on this server >> $OFILE
fi

cat $OFILE
