#!/bin/sh
#
#ident	"$Id: setup,v 1.7 2005/01/04 22:03:15 mikew Exp $"
#
# Run this setup script to setup the automounter testing environment.
#
# setup [-a|-c] [-f server_name] [-s server_name] [-m mntpoint]
# 	-a		perform complete setup. Need to be run as "root".
#	-c		perform partial setup. Need to complete setup manually.
#	-f server_name	server to failover to.
#	-s server_name	server to test against.
#	-m mntpoint	path to automount on locally
# 
# If no command line arguments are provided, then the environment variables 
# must be defined in "../src/tests.init"
#
# This script installs all the necessary automounter maps in
# ${AUTOMAP_DIR}. If automounter was not started automatically, you are 
# responsible for manually modifying your /etc/auto_master map with the 
# required changes (given by this script) and have your automounter mount 
# the new mountpoints (by either restarting it, or running the "automount -v" 
# command on autofs based systems.
#
# This script also creates the necessary directories on your server to
# run the tests, provided ${AUTO_SERVER_DIR} is shared.
#
# This script also installs all the necessary data files for this testing
# environment in ${AUTOMAP_DIR}/dat directory.
#

Progname=`basename $0`
NSPC=`echo $Progname | sed 's/./ /g'`

#
# maps directory
#
DIR=../maps

MKDIR="mkdir"

InitFile="../src/tests.init"
USAGE="usage: $Program [-a|-c] [-f server_name] [-s server_name] [-m mntpoint]"
SETUP=-a

if [ ! -f ${InitFile} ]; then
	echo "'tests.init' not found - cannot proceed without defaults file"
	exit 1
fi

echo "Reading default environment variables from 'tests.init'..."
. $InitFile

if [ $# -ne 0 ]; then
	set - `getopt acf:m:s: $*`
	if [ $? != 0 ]; then
		echo $USAGE
		exit 1
	fi
	for c in $*
	do
		case $c in
			-a|-c)	SETUP=$c; shift	;;
			-f)	SERVER2=$2; shift; shift	;;
			-s)	SERVER1=$2; AUTO_CLIENT_MNTPNT=/auto_test/$2; shift; shift	;;
			-m)	AUTO_CLIENT_MNTPNT=$2; shift	;;
			--)	shift; break	;;
		esac
	done
fi

if [ "x$SERVER1" = "x" ]; then
	echo "SERVER1 environment variable not set."
	echo "Please set in 'tests.init'."
	exit 1
fi
MYSERVER1=${SERVER1}

if [ "x$SERVER2" = "x" ]; then
	SERVER2=${SERVER1}
fi
MYSERVER2=${SERVER2}

if [ "x$AUTOMAP_DIR" = "x" ]; then
	echo "AUTOMAP_DIR environment variable not set."
	echo "Please set in 'tests.init'."
	exit 1
fi

if [ "x$AUTO_SERVER_DIR" = "x" ]; then
	echo "AUTO_SERVER_DIR environment variable not set."
	echo "Please set in 'tests.init'."
	exit 1
fi

if [ "x$AUTO_CLIENT_MNTPNT" = "x" ]; then
	echo "AUTO_CLIENT_MNTPNT environment variable not set."
	echo "Please set in 'tests.init'."
	exit 1
fi

#
# customize maps with specified servers
#
echo "Installing automounter Connectathon test maps in ${AUTOMAP_DIR}..."
for MAP in `cd ${DIR}; ls auto_*`
do
	if [ "x$VERBOSE" != "x" ]; then
		echo "\t${AUTOMAP_DIR}/${MAP}"
	fi
	if [ -f ${AUTOMAP_DIR}/${MAP} ]; then
		rm -f ${AUTOMAP_DIR}/${MAP}
	fi

	sed -e "s%SERVER1%${MYSERVER1}%g" \
	    -e "s%SERVER2%${MYSERVER2}%g" \
	    -e "s%AUTOMAP_DIR%${AUTOMAP_DIR}%g" \
	    -e "s%AUTO_SERVER_DIR%${AUTO_SERVER_DIR}%g" \
	    -e "s%AUTO_CLIENT_MNTPNT%${AUTO_CLIENT_MNTPNT}%g" \
		${DIR}/${MAP} > ${AUTOMAP_DIR}/${MAP}
done

#
# executable map
#
chmod +x ${AUTOMAP_DIR}/auto_ecthon

MAP=auto_master
rm -f ${AUTOMAP_DIR}/${MAP}

sed -e "s%AUTOMAP_DIR%${AUTOMAP_DIR}%g" \
    -e "s%AUTO_CLIENT_MNTPNT%${AUTO_CLIENT_MNTPNT}%g" \
	 ${DIR}/${MAP} > ${AUTOMAP_DIR}/${MAP}

echo "Setting up directories on servers..."
#
# create the directories needed for automounter tests
#
for i in ${MYSERVER1} ${MYSERVER2}
do
	# Make sure nothing is mounted on the mountpoint
	if [ ! -d ${MNTPOINT} ]; then
		echo "$Progname: mountpoint ${MNTPOINT} not found"
		echo "Terminating - setup did not complete"
		exit 1
	fi

	eval $UMOUNTCMD ${MNTPOINT} > /dev/null 2>&1

	# Create test directory on server if necessary
	if [ ! -d /net/${i}/${AUTO_SERVER_DIR} ]; then
		mkdir -p /net/${i}/${AUTO_SERVER_DIR}
		if [ $? -ne 0 ]; then
			echo "Can't create test directory on server"
			echo "exiting"
			exit 1
		fi
		chmod 777 /net/${i}/${AUTO_SERVER_DIR}
	fi

	eval $MOUNTCMD ${i}:${AUTO_SERVER_DIR} ${MNTPOINT}
	case $? in
	   0)
		;;
	   *)
		echo "Can't mount ${i}:${AUTO_SERVER_DIR} on ${MNTPOINT}"
		echo "Terminating - setup did not complete"
		exit 1
	esac

	NEW_DIR_LIST=`cat ${DIR}/server_directories`
	for NEW_DIR in ${NEW_DIR_LIST}
	do
		if [ ! -d ${MNTPOINT}/${NEW_DIR} ]; then
			${MKDIR} -p ${MNTPOINT}/${NEW_DIR}
		fi
	done

	if [ ! -d ${MNTPOINT}/space/test1/'trailing_space ' ]; then
		${MKDIR} -p ${MNTPOINT}/space/test1/'trailing_space '
	fi

	if [ ! -d ${MNTPOINT}/space/test2/'double_trailing_space  ' ]; then
		${MKDIR} -p ${MNTPOINT}/space/test2/'double_trailing_space  '
	fi

	eval $UMOUNTCMD ${MNTPOINT}
done

#
# create the data files
#
DIR="../dat"
echo "Installing data files in ${AUTOMAP_DIR}/dat..."
if [ -d ${AUTOMAP_DIR}/dat ]; then
	rm -rf ${AUTOMAP_DIR}/dat
fi

mkdir -p ${AUTOMAP_DIR}/dat

for DATFILE in autoparse autoparse-n test1 test2 test3 test4 test5_autofs test5_nfs badnames
do
	if [ "x$VERBOSE" != "x" ]; then
		echo "\t${AUTOMAP_DIR}/dat/${DATFILE}"
	fi
	sed -e "s%AUTO_CLIENT_MNTPNT%${AUTO_CLIENT_MNTPNT}%g" \
	   ${DIR}/${DATFILE} > ${AUTOMAP_DIR}/dat/${DATFILE}
done

if [ $SETUP = "-c" ]; then
	echo
	echo
	echo "Make sure your /etc/auto_master includes all of the following"
	echo "automount mountpoints. Make sure your automounter is either"
	echo "restarted, or if running autofs, the 'automount -v' command"
	echo "is run after modifying your /etc/auto_master."
	echo "You can either cut and paste the following automount mountpoints"
	echo "into your /etc/auto_master, or use the one provided at"
	echo "${AUTOMAP_DIR}/${MAP}"
	echo ""
	echo "You may also add the following line to your /etc/auto_master and"
	echo "run the 'automount -v' command afterwards."
	echo "\t+${AUTOMAP_DIR}/${MAP}"
	echo
	echo
	cat ${AUTOMAP_DIR}/${MAP}

	echo
	echo "Press <enter> to continue. \c"
	read ans
else
	if [ $SETUP = "-a" ]; then
		echo "Updating /etc/auto_master and restarting automounter..."
		check=`grep ${AUTOMAP_DIR}/${MAP} /etc/auto_master | grep -v \^\#`
		if [ -z "$check" ]; then
			grep -v auto_master /etc/auto_master >/tmp/auto_master.$$
			echo "+${AUTOMAP_DIR}/${MAP}"	>> /tmp/auto_master.$$
			mv -f /tmp/auto_master.$$ /etc/auto_master
			if [ $? -ne 0 ]; then
				echo "auto_master could not be updated."
				exit 1
			fi

		fi

		if [ "x$VERBOSE" = "x" ]; then
			eval $AUTOMOUNTCMD > /dev/null 2>&1
		else
			eval $AUTOMOUNTCMD 
		fi
                if [ $? -ne 0 ]; then
                        echo "Automount command failed."
                        exit 1
                fi
	fi
fi

echo
echo "Setup done."
echo
