#!/usr/bin/ksh 
# @(#)instdirs.sh	1.0	11/15/00
#

INSTALL=/usr/sbin/install
LOGIN=lanman
LMXADMIN=lmxadmin
DOSGRP=DOS----
NO=1
YES=0
NO_DEFAULT=2

DEFAULT_USER_COMMENT="Solaris (TM) PC NetLink user"
DEFAULT_NEW_USER_SHELL="/bin/sh"
PASSWORD="password"
force="no"
cluster="no"


#
# Function to run gettext with TEXTDOMAINDIR set.
# TEXTDOMAINDIR is not set in the exported environment so subprocesses 
# do not inadvertently use it.
#

pgettext () {
	gttext=gettext #Trick to fool shgettext
	TEXTDOMAINDIR=${textdomaindir} ${gttext} ${textdomain} "$1"
}

##
# if this ENV variable exists and is set to rmall (from the uninstall script)
# then we must return $NO to the yorn questions. this should precede every question
# in this script
##
uninstallchk()
{
    # /tmp/uninstall.tmp will be created each time the uninstall script for 
    #  Solaris (TM) PC NetLink is called. This will remove all data related to Solaris (TM) PC NetLink that was put on the system.
    if [ -f /tmp/uninstall.tmp ]
    then 
	return $NO
    else
	return $YES
    fi
}
    
# Simple yes or no prompter
#
Is_Answer_Yes()
{
        while [ /bin/true ]
        do
                read answer ignore_extra_words

                case "$answer" in

                y|Y|yes|YES|Yes)
                        true; return
                        ;;
                n|N|no|NO|No)
                        false; return
                        ;;
                *)
                        pgettext "Please answer Yes or No: "
                        ;;
                esac
        done
}

#
# build accounts database
#
build_databases ()
{

	if [ ${SERVERNAME:-""} = "" ]
	then
		SERVERNAME="`uname -n| cut -c1-13`${PCNL_INSTANCE}"
	fi
	$SRVCONFIG_PATH -s "server,listenname=$SERVERNAME"


	if [ ${DOMAIN:-""} = "" ]
	then
		DOMAIN="`uname -n | cut -c1-9`${PCNL_INSTANCE}_dom"
	fi
	$SRVCONFIG_PATH -s "workstation,domain=$DOMAIN"

	echo "\nCreating ${COMMENT} accounts database."
	$SAMCHECK_PATH -d > $DEV_NULL_PATH 2>&1
	if [ $? -ne 0 ]
	then
		exit 1
	fi

	# set the administrator password
	$CHUSER_PATH -p "${PASSWORD}" "${DOMAIN}:administrator"

	if [ $? -ne 0 ]
	then
		exit 1
	fi
	$MAKEMACH_PATH -P > $DEV_NULL_PATH
	if [ $? -ne 0 ]
	then
		exit 1
	fi
}

MakeDirs() {
$INSTALL -d -u lanman -g DOS---- -m 0755 ${IDIR}/var/opt/lanman/${PCNL_INSTANCE}/datafiles > $DEV_NULL_PATH 2>&1
$INSTALL -d -u lanman -g DOS---- -m 0777 $DEBUGDIR_PATH > $DEV_NULL_PATH 2>&1
$INSTALL -d -u lanman -g DOS---- -m 0700 $DIRSYNCDIR_PATH > $DEV_NULL_PATH 2>&1
$INSTALL -d -u lanman -g DOS---- -m 0777 $SAMDIR_PATH > $DEV_NULL_PATH 2>&1
$INSTALL -d -u lanman -g DOS---- -m 0775 $LOGS_PATH > $DEV_NULL_PATH 2>&1
$INSTALL -d -u lanman -g DOS---- -m 0775 $LMXADMIN_PATH > $DEV_NULL_PATH 2>&1
$INSTALL -d -u lanman -g DOS---- -m 0777 $MAILSLOT_PATH > $DEV_NULL_PATH 2>&1
$INSTALL -d -u lanman -g DOS---- -m 0777 ${MAILSLOT_PATH}/net > $DEV_NULL_PATH 2>&1
$INSTALL -d -u lanman -g DOS---- -m 0775 $PROFILES_PATH > $DEV_NULL_PATH 2>&1
$INSTALL -d -u lanman -g DOS---- -m 0775 $REPL_PATH > $DEV_NULL_PATH 2>&1
$INSTALL -d -u lanman -g DOS---- -m 0755 $EXPORT_PATH > $DEV_NULL_PATH 2>&1
$INSTALL -d -u lanman -g DOS---- -m 0755 $SCRIPTS_PATH > $DEV_NULL_PATH 2>&1
# check owner and mode here
$INSTALL -d -u lanman -g DOS---- -m 0755 $IMPORT_DIR_PATH > $DEV_NULL_PATH 2>&1
$INSTALL -d -u lanman -g DOS---- -m 0755 $SAVEDIR_PATH > $DEV_NULL_PATH 2>&1
$INSTALL -d -u lanman -g DOS---- -m 0755 $SAVEDIR2_PATH > $DEV_NULL_PATH 2>&1
$INSTALL -d -u lanman -g DOS---- -m 0755 ${IDIR}/var/opt/lanman/${PCNL_INSTANCE}/shares > $DEV_NULL_PATH 2>&1
$INSTALL -d -u lanman -g DOS---- -m 0755 $LMSPOOL_PATH > $DEV_NULL_PATH 2>&1
$INSTALL -d -u lanman -g DOS---- -m 0755 $TIMEXDIR_PATH > $DEV_NULL_PATH 2>&1
$INSTALL -d -u lanman -g DOS---- -m 0755 $WINSDIR_PATH > $DEV_NULL_PATH 2>&1
#$INSTALL -d -u lanman -g DOS---- -m 0755 $CONFIGDIR_PATH > $DEV_NULL_PATH 2>&1
$INSTALL -d -u lanman -g DOS---- -m 0755 $CLIPRSPOOL_PATH > $DEV_NULL_PATH 2>&1
$INSTALL -d -u lanman -g DOS---- -m 0755 $CUSTOMS_PATH > $DEV_NULL_PATH 2>&1
$INSTALL -d -u lanman -g DOS---- -m 0755 $LMREGDIR_PATH > $DEV_NULL_PATH 2>&1
$INSTALL -d -u lanman -g DOS---- -m 0775 $PRINTDRV_PATH > $DEV_NULL_PATH 2>&1

if [ "$cluster" != "no" ]
then
	uspath=${IDIR}${USER_PATH}
	USER_PATH=$uspath
fi

#
# Create the directory where lanman users live
#
if [ ! -d ${USER_PATH} ]
then
	echo "Creating Directory: ${USER_PATH}"
	mkdir -p ${USER_PATH}
fi
if [ -d ${USER_PATH} ]
then
	homedir=${USER_PATH}
	chown $LOGIN $homedir &&
	chgrp $LANMAN_GROUP $homedir &&
	chmod $LANMAN_MODE $homedir
fi

#
# Create "prntfault" if it doesn't already exist.
#
if [ ! -f $PRNTFAULT_PATH ]
then
	cp $TPRNTFAULT_PATH $PRNTFAULT_PATH
fi

cat $PRNTFAULT_PATH | \
	grep -iv "LAN Manager" | \
	grep -iv "Solaris (TM) PC NetLink" > /tmp/prtflt.$$

cp /tmp/prtflt.$$ $PRNTFAULT_PATH
rm -f /tmp/prtflt.$$

cat >> $PRNTFAULT_PATH <<!
if [ -x \$PRALERTER_PATH ]		#From Advanced Server for UNIX Systems
then					#From Advanced Server for UNIX Systems
	\$PRALERTER_PATH \$1 < \$TMPLOG	#From Advanced Server for UNIX Systems
fi					#From Advanced Server for UNIX Systems
!

chown $TPRNTFAULT_OWNER $PRNTFAULT_PATH
chgrp $TPRNTFAULT_GROUP $PRNTFAULT_PATH
chmod $TPRNTFAULT_MODE $PRNTFAULT_PATH

build_databases

#
# Add netlogon and browser services to list of services which
# will be automatically started with the server.
#

$SRVCONFIG_PATH -s "server,srvservices=alerter,netlogon,browser"

#
# setup the registry
#
echo "Configuring registry..."
${REGPRELOAD_PATH}
if [ $? -ne 0 -o ! -r ${REG_INI_PATH} ]
then
	echo "error creating ${REG_INI_NAME}"
	exit ${FATAL_EXIT_CODE}
fi
${REGUPGRADE_PATH}
if [ $? -ne 0 -o ! -r ${REG_INI_PATH} ]
then
	echo "error upgrading ${REG_INI_NAME}"
	exit ${FATAL_EXIT_CODE}
fi
${REGLOAD_PATH}
if [ $? -ne 0 -o ! -r ${REGISTRY_PATH} ]
then
	echo "error loading ${REG_INI_NAME}"
	exit ${FATAL_EXIT_CODE}
fi

#
# fixup replicator parameters in registry...
#
#
# set replicator
#
	mkdir -p $EXPORT_PATH/scripts > $DEV_NULL_PATH 2>&1
	chmod 775 $EXPORT_PATH/scripts
	chown $LMXADMIN $EXPORT_PATH/scripts
	chgrp $DOSGRP $EXPORT_PATH/scripts

	mkdir -p $IMPORT_DIR_PATH/scripts > $DEV_NULL_PATH 2>&1
	chmod 775 $IMPORT_DIR_PATH/scripts
	chown $LMXADMIN $IMPORT_DIR_PATH/scripts
	chgrp $DOSGRP $IMPORT_DIR_PATH/scripts

	$REGCONFIG_PATH SYSTEM/CurrentControlSet/Services/Netlogon/Parameters Scripts REG_EXPAND_SZ "%SystemRoot%${IMPORT_DIR_PATH}/scripts"
	$REGCONFIG_PATH SYSTEM/CurrentControlSet/Services/Replicator/Parameters Replicate REG_DWORD 3

	SCRIPT_LOC=$EXPORT_PATH/scripts

	# Install the default logon scripts on the primary
#	`echo ECHO ON
#	 echo Welcome to Solaris (TM) PC NetLink
#	 echo ECHO OFF` > $SCRIPT_LOC/netlogon.bat
#	`echo @ECHO Welcome to Solaris (TM) PC NetLink` 
#		> $SCRIPT_LOC/netlogon.cmd new @ECHO

	# script $homedir/netlogon.bat old ECHO
	# script $homedir/netlogon.cmd old @ECHO
	# script $homedir/scripts/netlogon.bat old ECHO
	# script $homedir/scripts/netlogon.cmd old @ECHO

#
# Since we set the home directory for server users, add it to the
# lanman.ini file now only if not the default of /home/lanman.
#
if [ $homedir != /home/$LOGIN ]
then
	DIR="`echo $homedir | tr '/' '\134'`"
	$REGCONFIG_PATH SYSTEM/CurrentControlSet/Services/LanmanServer/Parameters userpath REG_SZ "c:${DIR}"
fi

#
# Copy LDIF template file into place for use by sam2ldif and ldif2sam
#
cp -f /opt/lanman/templ/ldif.tmpl $LANMANETC_PATH/ldif.tmpl

}

#
# See if a directory is empty
#
isempty ()
{
	if [ "`echo $1/*`" = "$1/*" ]
	then
		return $YES
	else
		return $NO
	fi
}


RemoveDirs() {

    # First delete GUI scheduled tasks
    EDITOR=ed; export EDITOR
    filelist=`ls ${LANMANDATA_PATH}/datafiles/slstask*.sh 2>/dev/null`
    for file in ${filelist}
    do
	file=`basename ${file}`
# The sleep is required because crontab will discard the edits if no time
# elapses between the time the file is opened and we exit ed.
	    crontab -e << EOI >/dev/null 2>&1
/${file}/
d
!/usr/bin/sleep 3
w
q
EOI
    done

    rm -rf ${LANMANDATA_PATH}
    rm -rf ${LANMANETC_PATH}

    if [ "`echo ${IDIR}/var/opt/lanman/*`" = "${IDIR}/var/opt/lanman/*" ]
    then
	cd ${IDIR}/var/opt
	rmdir -p lanman
    fi
    
    if [ "`echo ${IDIR}/etc/opt/lanman/*`" = "${IDIR}/etc/opt/lanman/*" ]
    then
	cd ${IDIR}/etc/opt
	rmdir -p lanman
    fi
}

RemovePrinters() {

	if [ -x $LPSCHED_PATH ]
	then
		#
		# To ensure that LP will be in a healthy state 
		# (i.e., no jobs that are marked as "cancelled" 
		# will hang around causing LP to NOT remove 
		# client printers...), stop and restart LP.
		#
		$LPSHUT_PATH > $DEV_NULL_PATH 2>&1
		sleep 10
		$LPSCHED_PATH > $DEV_NULL_PATH 2>&1

		PRINTERS=

		for I in lmxnul cust_scr custom_script lmxnone
		do
			if lpstat -o $I > $DEV_NULL_PATH 2>&1
			then
				PRINTERS="$PRINTERS $I"
			fi
		done

		if [ -d "$CONFIGDIR_PATH" ]
		then
			prtlist=`/bin/ls $CONFIGDIR_PATH`
		fi

		if [ -d "$CONFIGDIR_PATH" -a -n "$prtlist" ]
		then
			#
			# Get the names of the client printers
			#
			if lpstat -r > $DEV_NULL_PATH 2>&1
			then
				for prt in `/bin/ls $CONFIGDIR_PATH`
				do
					dev=`$LPSTAT_PATH -v $prt 2> $DEV_NULL_PATH | cut -d' ' -f4`
					if [ "$dev" = "$DEV_NULL_PATH" ]
					then
						PRINTERS="$PRINTERS $prt"
					fi
				done
			fi
		fi

		for I in $PRINTERS
		do
			# This checks the exit status of lpstat to
			# make sure the printer names are known
			# before trying to cancel jobs, etc.
			if (lpstat -o ${I} > $DEV_NULL_PATH 2>&1 )
			then
				echo "\t\t\tCancelling jobs for printer: $I"
				$LPCANCEL_PATH ${I} > $DEV_NULL_PATH 2>&1
			fi
			$LPADMIN_PATH -x$I > $DEV_NULL_PATH 2>&1
		done
		savedprs=no
	fi


	# Delete LP printer classes

	lmshare -l | nawk -F'[:]' '{if ($2 == "p") print $6}' | while read cname
	do
	    # Must convert the name first: 14 chars, ' '->'_'
	    convname=`echo $cname | cut -c1-14 | sed -e 's/ /_/g'`
	    lpadmin -x $convname
	done

}

RemoveUnixUsers() {

#
# remove UNIX users created by AS/U
#
	USER_COMMENT=`$REGCONFIG_PATH system/currentcontrolset/services/advancedserver/userserviceparameters usercomment 2> $DEV_NULL_PATH`
	if [ $? -ne 0 ]
	then
		USER_COMMENT=$DEFAULT_USER_COMMENT
	else
		if [ "$USER_COMMENT" = "" ]
		then
			USER_COMMENT=$DEFAULT_USER_COMMENT
		fi
	fi
	NEW_USER_SHELL=`$REGCONFIG_PATH system/currentcontrolset/services/advancedserver/userserviceparameters newusershell 2> $DEV_NULL_PATH`
	if [ $? -ne 0 ]
	then
		NEW_USER_SHELL=$DEFAULT_NEW_USER_SHELL
	else
		if [ "$NEW_USER_SHELL" = "" ]
		then
			NEW_USER_SHELL=$DEFAULT_NEW_USER_SHELL
		fi
	fi
	if [ -x "$PASSMGMT_PATH" ]
	then
		for i in `grep "$USER_COMMENT" /etc/passwd | grep ":$NEW_USER_SHELL" | cut -d: -f1`
		do
			$PASSMGMT_PATH -d $i > $DEV_NULL_PATH 2>&1
		done
	elif [ -x "$USERDEL_PATH" ]
		then
		for i in `grep "$USER_COMMENT" /etc/passwd | grep ":$NEW_USER_SHELL" | cut -d: -f1`
		do
			$USERDEL_PATH -n 0 $i > $DEV_NULL_PATH 2>&1
		done
	else
		echo "\t*** Failed to remove $COMMENT users"
	fi
}




#MAIN
while [ /bin/true ]
do
	case $1 in
		-a)
			action="add"
			shift
			PCNL_INSTANCE=$1
			IDIR=$2
			if [ $# -gt 2 ]
			then 
				cluster=$3
			fi
			;;
		-d)
			action="delete"
			shift
			PCNL_INSTANCE=$1
			IDIR=$2
			if [ $# -gt 2 ]
			then 
				cluster=$3
			fi
			;;
		-f)
			force="yes"
			shift
			;;
		*)
			break
			;;
	esac
done

if [ "$IDIR" = "/" ]
then
	IDIR=""
fi

export PCNL_INSTANCE
export IDIR

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

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

if [ $action = "add" ]
then
    if [ -d ${LANMANDATA_PATH} -o -d ${LANMANETC_PATH} ]
    then 
	exit 1
    fi 
    mkdir -p ${LANMANDATA_PATH}
    mkdir -p ${LANMANETC_PATH}

    MakeDirs
elif [ $action = "delete" ]
then

    uninstallchk
    if [ $? = $YES -a "$force" = "no" ] 
    then
	pgettext "\nThis command will remove data configured in this virtual server:\n"
	cat <<!
	- customized print processor scripts
	- shared printers
	- shared directories
	- users and accounts
	- WINS database
!
	pgettext "\nDo you wish to proceed?  "
	Is_Answer_Yes
	if [ $? = $NO ]
	then
		exit 1
	fi
    fi
	
    if [ ! -d ${LANMANDATA_PATH} -a ! -d ${LANMANETC_PATH} ]
    then 
	if [ "$force" = "no" ]
	then
 	    pgettext "The data has already been removed."
	fi
	exit 1
    fi 

    if [ "$force" = "no" ]
    then
    	pgettext "\nRemoving Unix user accounts created by this virtual server.\n"
    	RemoveUnixUsers

    	pgettext "\nRemoving printers configured by this virtual server.\n"
    	RemovePrinters
    fi

    pgettext "\nRemoving contents of ${LANMANDATA_PATH} and ${LANMANETC_PATH}.\n"
    RemoveDirs
fi

exit 0
