#!/bin/sh
#
# ident "@(#)preinstall.src	1.2 02/10/22 SMI"
#
# Copyright 2002 Sun Microsystems, Inc.  All rights reserved.
#
# This script attempts to clean up the creation of a dtsession
# specific properties file that earlier versions of SUNWutps
# created. This file was created in the locale-specific "C"
# directory and was the root cause of bug: 4746096.
#
# This cleanup will fix the problem when upgrading from SRSS 1.3
# patch or SRSS 2.0 beta, both of which created this file.
#

BASENAME="/bin/basename"
     MOD="`$BASENAME $0`"
 DIRNAME="/bin/dirname"

EGREP="/bin/egrep"
   MV="/bin/mv"
   RM="/bin/rm -f"
RMDIR="/usr/bin/rmdir"
   CP="/bin/cp"
 DIFF="/bin/diff"
   TR="/bin/tr"

    set -u

    TMP_SR="/var/run/$MOD.$$"

# dtsession autounlock from PAM
USR_CDE_DEFAULTS_DIR="/usr/dt/app-defaults/C"
ETC_CDE_DEFAULTS_DIR="/etc/dt/app-defaults/C"
DTSESSION_DEFAULTSFILE="Dtsession"
PAM_PROMPT_NOLOCK_PROP="noPamPromptNoLock"

    #
    # Remove the property that tells dtsession that it's OK to
    # unlock the screen without requiring user input if the PAM
    # module has validated the user.
    #
    # Once this property is removed, compare the file in /etc/dt
    # with the one in /usr/dt, and if they are not different,
    # then remove the file in /etc/dt.
    #
    if test -f $ETC_CDE_DEFAULTS_DIR/$DTSESSION_DEFAULTSFILE ; then
        $EGREP -vi "$PAM_PROMPT_NOLOCK_PROP" $ETC_CDE_DEFAULTS_DIR/$DTSESSION_DEFAULTSFILE >$TMP_SR
        $MV $TMP_SR $ETC_CDE_DEFAULTS_DIR/$DTSESSION_DEFAULTSFILE

	#
	# Test to see if the files are different. If not, then remove
	# the version that is in /etc/dt and clean up the directories.
	# The test is done by first converting all newlines to space,
	# and then doing a diff that ignores spaces (and tabs). This
	# will prevent any wayward newlines from giving us a false
	# comparison result.
	#
	E_FILE=$TMP_SR.etc.$DTSESSION_DEFAULTSFILE
	U_FILE=$TMP_SR.usr.$DTSESSION_DEFAULTSFILE

	$TR '\n' ' ' <$ETC_CDE_DEFAULTS_DIR/$DTSESSION_DEFAULTSFILE >$E_FILE
	$TR '\n' ' ' <$USR_CDE_DEFAULTS_DIR/$DTSESSION_DEFAULTSFILE >$U_FILE

	if $DIFF -w $E_FILE $U_FILE >/dev/null 2>/dev/null ; then
	    #
	    # Files in /etc/dt and /usr/dt are the same, so first remove
	    # the file in /etc/dt and then remove any empty dirs in there.
	    #
	    $RM $ETC_CDE_DEFAULTS_DIR/$DTSESSION_DEFAULTSFILE
	    $RMDIR -p $ETC_CDE_DEFAULTS_DIR >/dev/null 2>/dev/null
	fi

	# clean up the comparison files
	$RM $E_FILE $U_FILE

    fi

    exit 0
