#ident	"@(#)pkg.nsu:i386/pkg/nsu/preinstall	1.23"
#ident	"$Header: $"
#
#  For cases when request script is not run, we set up for LOCALE 
#  specific menus here.
#
#  Make sure LOCALE environment variable is set.  If it's not set
#  coming in to this script, then default to the C-locale.
#
LOCALE=${LC_ALL:-${LC_MESSAGES:-${LANG:-"C"}}}

MENU_DIR=/etc/inst/locale/${LOCALE}/menus/${PKG}
[ -d ${MENU_DIR} ] || mkdir -p ${MENU_DIR}

MENUS="menu.nsu"

for i in $MENUS
do
   if [ ! -f ${MENU_DIR}/$i ]
   then
      if [ -f ${REQDIR}/inst/locale/${LOCALE}/${PKG}/$i ]
      then
           cp ${REQDIR}/inst/locale/${LOCALE}/${PKG}/$i ${MENU_DIR}
      else
           cp ${REQDIR}/${PKG}/$i ${MENU_DIR}
      fi
   fi
done
#
#  Set up to use menu_colors; default to C-locale if ${LOCALE}'s dir has
#  no menu_colors.sh
#
if [ -f /etc/inst/locale/${LOCALE}/menus/menu_colors.sh ]
then
	. /etc/inst/locale/${LOCALE}/menus/menu_colors.sh
else
	. /etc/inst/locale/C/menus/menu_colors.sh
fi

# Add "smtp" user 
grep smtp /etc/passwd >/dev/null 2>&1
if [ $? -ne 0 ]
then
	# This is a new installation, force uid to be 55 to match uid set on files.

	/usr/sbin/useradd -g mail -d /var/spool/mailq -u 55 -c "SMTP Processes" -m smtp > /dev/null 2>&1

else
	# The smtp user already exists so this is an upgrade or overlay.
	# We modify its /etc/passwd entry now to make sure the home 
	# directory, etc. are updated.  The uid remains unchanged. 
	# Later in postinstall we set the owner of all newly installed 
	# smtp files to this uid. We do this because boot-floppy 
	# installation will install these files with uid 55.

	/usr/sbin/usermod -g mail -d /var/spool/mailq -c "SMTP Processes" -m smtp > /dev/null 2>&1
fi

environ=$1
>${environ}

addenv () {
	eval echo $1 >>${environ}
}


##########
#
# ${VARSPOOLLP} and ${ETCLP} are the names of the new directories where
# things are now kept.
##########

addenv VARSPOOL=/var/spool > /dev/null
addenv VARSPOOLLP=${VARSPOOL}/lp > /dev/null
addenv ETCLP=${ETC}/lp > /dev/null
addenv USRLIBLP=${USRLIB}/lp > /dev/null
addenv OLD_VERSION=NONE > /dev/null
#
#
#	if ls ${VARSPOOLLP}/requests/*/* 1>/dev/null 2>&1
#	then
#		echo "
#There are still some print requests queued for printing.  Installation
#of the new Spooler over the old configuration will DESTROY them."
#
#		CONTINUE "
#		If you answer \"n\" (or \"no\") installation will stop
#		to give the queued print requests a chance to be printed.
#		If you answer \"y\" (or \"yes\") installation will proceed
#		WITHOUT SAVING any currently queued jobs.
#\n\n
#		If you suspect that there really are no requests still
#		queued, look for strange files in the directories under
#		${SPOOLDIR}/request."
#	fi


##########
##
## Shutdown Spooler
## 
##
##########

if [ -x "/usr/sbin/lpshut" ]
then
	/usr/sbin/lpshut >/dev/null 2>&1
fi

##########
##
## Save Non-networking Spooler and lpadmin
## 
##
##########

#echo "Saving non-networking lpsched. . ."

cp /usr/lib/lp/lpsched /usr/lib/lp/lpsched.LPM

#echo "Non-networking lpsched saved in /usr/lib/lp/lpsched.LPM"

#echo "Saving non-networking lpadmin. . ."

cp /usr/sbin/lpadmin /usr/sbin/lpadmin.LPM

#echo "Non-networking lpadmin saved in /usr/sbin/lpadmin.LPM"


############# Begin UPGRADE AND OVERLAY #######################

SCRIPTS=/usr/sbin/pkginst
. ${SCRIPTS}/updebug

[ "$UPDEBUG" = YES ] && {
	set -x
	goany
}

UPINSTALL=/etc/conf.sav
UPGRADE_STORE=/etc/inst/save.user
UPGFILE=${UPGRADE_STORE}/${PKGINST}.env
#AUTOMERGE=NULL

# If $UPGFILE exists than either this is an upgrade or overlay
# done during boot-floppy installation or the request script
# created it.  If request was run, then $UPGFILE contains NREG. 
# Otherwise, NREG must be set now. 

[ -f $UPGFILE ] && . $UPGFILE

[ "$NREG" ] || {

	# Since NREG is not set, this is either a boot-floppy 
	# installation or we are pkgadding a set and installing 
	# nsu in automatic mode. Do not determine PKGINSTALL_TYPE 
	# or AUTOMERGE if this is a boot-floppy install since these 
	# variables are in UPGFILE already.

	if [ ! -f /etc/inst/scripts/postreboot.sh ]
	then 
		# We are installing nsu by pkgadding a set.
		PKGINSTALL_TYPE=NEWINSTALL
	
		# Call chkpkgrel to determine installation type.
		${SCRIPTS}/chkpkgrel
		PKGVERSION=$?
		
		case $PKGVERSION in
			2)	PKGINSTALL_TYPE=OVERLAY ;;
			4)	PKGINSTALL_TYPE=UPGRADE ;;
			*)	;;
		esac
		rm -f $UPGFILE	
	fi

	# Since the new mtune.d/ptm is not installed yet, we get the
	# number of default pseudo-ttys from the pkginfo file.
	# The default we use depends on which set, AS or PE, is
	# installed.
	if [ -d /var/sadm/pkg/as ]
	then 
		DFLT_REG=$DFLT_REG_AS
	else
		DFLT_REG=$DFLT_REG_PE
	fi
	NREG=$DFLT_REG

	[ "$PKGINSTALL_TYPE" = UPGRADE ] && {
		if [ -f /etc/inst/scripts/postreboot.sh ]
		then
		# We are installing from the boot floppy.
			PTM=$UPINSTALL
		else
			PTM=/etc/conf
		fi	
		
		if [ -f $PTM/sdevice.d/ptm ]
		then
		# We are upgrading from a pre-UnixWare 2.0 release that used 
		# different drivers for SCO and regular ptys. The sdevice.d/ptm 
		# file will have only the number of regular ptys.
			grep "^ptm	" $PTM/sdevice.d/ptm >/tmp/ptm.$$ 2>/dev/null
			OFS=$IFS
			IFS="	"	#tab
			read Dev Conf LAST_REG x4 </tmp/ptm.$$
			[ "$Conf" = N ] && LAST_REG=$DFLT_REG
			IFS=$OFS
			rm -f /tmp/ptm.$$

			[ "$LAST_REG" -gt "$DFLT_REG" ] && NREG=$LAST_REG
			[ "$NREG" -gt "$MAX_REG_PTS" ] && NREG=$MAX_REG_PTS
		else
		# This is the odd case when we cannot find the old values. 
		# Use the defaults.
				continue
		fi
	}

	[ "$PKGINSTALL_TYPE" = OVERLAY ] && {
		if [ -f /etc/inst/scripts/postreboot.sh ]
		then
		# We are installing from the boot floppy.
			PTM=$UPINSTALL
			NREG=`cut -f2 $PTM/node.d/pts|grep ttyp|wc -l`
			[ "$NREG" -gt "$MAX_REG_PTS" ] && NREG=$MAX_REG_PTS
		else
		# We are installing using pkgadd on a running system.
			PTM=/etc/conf
			if [ -f $PTM/mtune.d/ptm ]
			then
				line=`/etc/conf/bin/idtune -g NUMREGPT`
				set - $line
				NREG=$1
				[ "$NREG" -gt "$MAX_REG_PTS" ] && NREG=$MAX_REG_PTS
			else
			# This is the odd case when we cannot find the old values.
			# Use the defaults.
				continue
			fi
		fi	
	}
}
############# End  UPGRADE AND OVERLAY #######################

	# Save volatile files for upgrades and overlays
	${SCRIPTS}/pkgsavfiles ${PKGINST}
# pkgsavfiles may change AUTOMERGE to NULL if there are no
# files to merge or if it is a new installation. Therefore,
# find out what values it set. Don't just overwrite them.
. $UPGFILE

echo "PKGINSTALL_TYPE=${PKGINSTALL_TYPE}" >$UPGFILE || exit 1
echo "AUTOMERGE=${AUTOMERGE}" >>$UPGFILE || exit 1
echo "NREG=${NREG}" >>$UPGFILE || exit 1

exit 0
