#!/bin/sh
#
# Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#
#ident	"@(#)preremove	1.8	07/06/04 SMI"
#
#	remove the domestic libcrypt.a symlink and, if the
# international version of the symlink is installed, restore it.

PKGCOND=/usr/bin/pkgcond
is_srngz=99

if [ -x $PKGCOND ]; then
     $PKGCOND -v is_sparse_root_nonglobal_zone > /dev/null 2>&1
     is_srngz=$?
fi

##
## 'pkgcond' does not exist when you do live upgrade from S8/S9
## so in that case, use 'touch' instead for the testing
##

test_writable() {
        if [ $is_srngz -ne 99 ]; then
                $PKGCOND is_path_writable $1 && return $?
        else
                /usr/bin/touch $1/.test.$$ > /dev/null 2>&1
                if [ $? !=  0 ]; then
                        return 1
                else
                        rm -f  $1/.test.$$ > /dev/null 2>&1
                        return 0
                fi
        fi
}

sparc_preremove () {
    # remove the domestic link
    #
test_writable $BASEDIR/usr/lib/sparcv9 > /dev/null 2>&1
if [ $? = 0 ]; then

    if [ -h $BASEDIR/usr/lib/sparcv9/libcrypt.so ]; then
	    rm -f $BASEDIR/usr/lib/sparcv9/libcrypt.so
    fi
    if [ -h $BASEDIR/usr/lib/sparcv9/libcrypt.so.1 ]; then
	    rm -f $BASEDIR/usr/lib/sparcv9/libcrypt.so.1
    fi
    
    # reinstall the international link
    if [ -h $BASEDIR/usr/lib/sparcv9/libcrypt_i.so ]; then
	    ln -s libcrypt_i.so $BASEDIR/usr/lib/sparcv9/libcrypt.so || exit 2
    fi
    if [ -f $BASEDIR/usr/lib/sparcv9/libcrypt_i.so.1 ]; then
	ln -s libcrypt_i.so.1 $BASEDIR/usr/lib/sparcv9/libcrypt.so.1 || exit 2
    fi
fi
}

# remove the link if it exists
test_writable $BASEDIR/usr/lib > /dev/null 2>&1
if [ $? = 0 ]; then

	if [ -h $BASEDIR/usr/lib/libcrypt.a ]; then
		rm -f $BASEDIR/usr/lib/libcrypt.a
	fi
	if [ -h $BASEDIR/usr/lib/libcrypt.so ]; then
		rm -f $BASEDIR/usr/lib/libcrypt.so
	fi
	if [ -h $BASEDIR/usr/lib/libcrypt.so.1 ]; then
		rm -f $BASEDIR/usr/lib/libcrypt.so.1
	fi

# put the international links back
	if [ -f $BASEDIR/usr/lib/libcrypt_i.a ]; then
		ln -s libcrypt_i.a $BASEDIR/usr/lib/libcrypt.a || exit 2
	fi
	if [ -s $BASEDIR/usr/lib/libcrypt_i.so ]; then
		ln -s libcrypt_i.so $BASEDIR/usr/lib/libcrypt.so || exit 2
	fi
	if [ -f $BASEDIR/usr/lib/libcrypt_i.so.1 ]; then
		ln -s libcrypt_i.so.1 $BASEDIR/usr/lib/libcrypt.so.1 || exit 2
	fi

	if [ "$ARCH" = "sparc" ]; then sparc_preremove; fi;
fi
exit 0
