#!/bin/sh
#!/bin/sh
# @(#)lmxstart.sh	1.20	6/23/99

PCNL_INSTANCE=$1
export PCNL_INSTANCE
IDIR=$2
export IDIR

LMPATHS="`/bin/dirname $0`/../lib/lmpaths"

if [ -f ${LMPATHS} ]
then
    . ${LMPATHS}
else
    . lmpaths
fi

# INFORMATION
# This command is executed by lmx.ctrl very early during initialization.
# The role of this script is to ensure that the accounts and share list
# files are in place and nominally correct.
# The output of this command goes into $START_LOG_PATH in the lan manager
# directory.
# The server is in the LAN Manager home directory when this command is issued.

# Redirect output to the log file.
exec > $START_LOG_PATH 2>&1

SCRIPTNAME=`basename $0`
echo "$SCRIPTNAME Start: `date`\n"

# Delete any subdirectories below the mailslot directory
if [ -d $MAILSLOT_PATH ]
then
	rm -rf $MAILSLOT_PATH/*
fi

#
# import share info stored in registry (if any)
#
$REGSHAREIMPORT_PATH

#
# verify that SAM/LSA exists
#
if [ ! -f $LSA_PATH ]
then
	echo "\nCreating the Accounts files\n"
        mkdir $SAMDIR_PATH
        $SAMCHECK_PATH -d
fi

#
# verify that an ACL database is present
#
if [ ! -f $ACL_PATH ]
then
	echo "\nCreating the Access Control List file\n"
        $ACLADM_PATH -N
fi

#
# Create shares for ADMIN$, IPC$, C$, and D$ if they do not exist.
#

keepadmin=`$REGCONFIG_PATH SYSTEM/CurrentControlSet/Services/AdvancedServer/ShareParameters KeepAdministrativeShares`

# read the CRootDirectory parameter from lanman.ini and convert "\" (if any) to "/"
cshare="`$SRVCONFIG_PATH -g lmxserver,CRootDirectory | tr "\134" "\057"`"

if [ $? -ne 0 -o "${cshare}" = "" ]
then
	if [ "$IDIR" = "/" ]
	then
		IDIR=""
	fi
	cshare="${IDIR}/var/opt/lanman/${PCNL_INSTANCE}/shares"
else
	# strip off the c: if it's there 
	cshare=`echo $cshare | sed -e 's,c:,,'`
	cshare=`echo $cshare | sed -e 's,C:,,'`
fi

if [ "$keepadmin" = "1" ]
then
echo "Checking ADMIN$ share"
$LMSHARE_PATH -a -i << END
"ADMIN$" "d" "Admin Share" "rwcxda" "-1" "${LANMAN_PATH}" "" "" "0" "" "0" "" "0" "" "" "" "" ""
END
fi

echo "Checking IPC$ share"
$LMSHARE_PATH -a -i << END
"IPC$" "i" "IPC Share" "rwcxda" "-1" "" "" "" "0" "" "0" "" "0" "" "" "" "" ""
END

# Remove the C$ share in case CRootDirectory in lanman.ini has changed.
echo "Recreating C$ share"
$LMSHARE_PATH -d C$
$LMSHARE_PATH -a -i << END
"C$" "d" "Root Share" "r" "-1" "$cshare" "" "" "0" "" "0" "" "0" "" "" "" "" ""
END

echo "Checking D$ share"
$LMSHARE_PATH -a -i << END
"D$" "d" "SystemRoot Share" "r" "-1" "$LMSHARES_PATH" "" "" "0" "" "0" "" "0" "" "" "" "" ""
END

printshare_path=`echo $PRINTDRV_PATH | sed -e 's!//!/!'`

echo "Checking PRINT$ share"
$LMSHARE_PATH -a -i << END
"PRINT$" "d" "Printer Driver Share" "rwcxda" "-1" "$printshare_path" "" "" "0" "" "0" "" "0" "" "" "" "" ""
END


# INFORMATION
# This script looks in the directory $LMSHARES_PATH for subdirectories that
# should be the treetop of a share area.  For them to be automatically shared
# during server startup, there needs to be a file named 'shareinf' in the
# subdirectory that has keyword=value tokens in it to guide the automatic
# sharing process.  In particular, three keywords are recognized:
#
# .VL 10 2
# .LI "COMMENT"
#	is the comment associated with the share area.
# .LI "ACCESS"
#	is the access permissions associated with the share area.  Examples
#	are r or rw.
# .LI "SHARENAME"
#	is the share name the path will receive.  SHARENAME lines can appear
#	as many times as desired in this file to share the path multiple times.
#	Also, the last ACCESS and/or COMMENT lines are the ACCESS and COMMENT
#	values used for this particular SHARENAME entry.
# .LE

echo "Checking shares under $LMSHARES_PATH"
for I in $LMSHARES_PATH/*
do
	SHARECOMMENT=
	ACCESS=r
	if [ -d $I -a -f $I/shareinf ]
	then
		echo "\tChecking share entry for: `basename $I`"
		while read LINE
		do
			case "$LINE" in
			COMMENT*=*)
				SHARECOMMENT=`echo $LINE|cut -d= -f2`
				;;
			ACCESS*=*|PERMISSION*=*)
				set -- `echo $LINE|cut -d= -f2`
				ACCESS=$1
				;;
			SHARENAME*=*)
				set -- `echo $LINE|cut -d= -f2|tr '[a-z]' '[A-Z]'`
				$LMSHARE_PATH -a -i << END
"$1" "d" "$SHARECOMMENT" "$ACCESS" "-1" "$I" "" "" "0" "" "0" "" "0" "" "" "" "" ""
END
				;;
			esac
		done < $I/shareinf
	fi
done

# Add LMU to the listener databases for all stacks if no listenname defined
listenname=`$SRVCONFIG_PATH -g server,listenname`
ListenExtension=`$SRVCONFIG_PATH -g "lmxserver,listenextension" | \
						tr '[A-Z]' '[a-z]'`
if [ -z "$ListenExtension" ]
then
	ListenExtension=".serve"
fi
len=`echo $ListenExtension | wc -c`
len=`expr 16 - $len`
UnameDotServe="`uname -n | cut -d. -f1 | cut -c1-${len}`${ListenExtension}"

if [ "$listenname" = "" -o "$listenname" = "$UnameDotServe" ]
then
	for x in `$NLSADMIN_PATH -x | cut -f1`
	do
		$NLSADMIN_PATH -q -z 2 $x
		if [ "$?" != "0" ]
		then
			echo "Adding the server to protocol stack: $x"
			$NLSADMIN_PATH -m -a 2 -c "$PASSCLI_PATH" -y \
			"Used by Solaris (TM) PC NetLink server" -w root $x
		fi
	done
fi

echo "\n$SCRIPTNAME Complete: `date`" >> $START_LOG_PATH
