#!/bin/ksh -p
#
# ident "@(#)utcompatlinksctl.ksh	1.7 10/12/14 Oracle"
#
# Copyright (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved.
#

set -u

########################################################################
#
# Creates compatibility library symlinks in /opt/SUNWut/lib/compatlinks and
# Creates compatibility library symlinks in /opt/SUNWuttsc/lib
# 
# Checks the availability of the required library SRSS depends on
# If the required library is not available
# then checks the availability of the compatible library
# If the compatible library is present then creates a symlink.
#
########################################################################


########################################################################
#
# Operation entry points, one for each keyword.
# This is where the specific work for this feature happens.
#
########################################################################


disable() {
    
    for LINK in     $COMPATLINK_LIBSSL \
                    $COMPATLINK_LIBGDBM \
	            $COMPATLINK_LIBLDAP \
		    $COMPATLINK_LIBLBER \
		    $LIB_COMPATLINKS \
		    $COMPATLINK64_LIBGDBM \
		    $COMPATLINK64_LIBLDAP \
		    $COMPATLINK64_LIBLBER \
		    $LIB_COMPATLINKS64 \
		    $COMPATLINK_LIBCRYPTO
	do
		if [ -L $LINK ] ; then
			rm -rf $LINK
			if $VERB; then
				print "$LINK link disabled"
			fi
		fi
	done
	return 0

}


createLink() {

	typeset LIB=$1
	typeset COMPATLIB=$2
	typeset COMPATLINK=$3
	
	if [ ! -f $LIB ] ; then
		if [ -f $COMPATLIB ]; then
			rm -rf $COMPATLINK
			ln -fs $COMPATLIB $COMPATLINK
			if $VERB; then
				print "$COMPATLINK -> $COMPATLIB link enabled"
			fi
		else
			print -u2 "[${PROG}] Warning: Failed to create $COMPATLINK -> $COMPATLIB link"
			print -u2 "[${PROG}]          $COMPATLIB does not exist."
		fi
	fi

}


enable() {
	if [ ! -L $LIB_COMPATLINKS ] ; then
		rm -rf $LIB_COMPATLINKS
		ln -fs $ETC_COMPATLINKS $LIB_COMPATLINKS
		if [ $? -ne 0 ]; then
			print -u2 "[${PROG}] Failed to create $LIB_COMPATLINKS -> $ETC_COMPATLINKS link"
			return 1
		elif $VERB; then
			print "$LIB_COMPATLINKS -> $ETC_COMPATLINKS link enabled"
		fi
	fi


	createLink $LIBGDBM $COMPATLIB_LIBGDBM $COMPATLINK_LIBGDBM
	createLink $LIBLDAP $COMPATLIB_LIBLDAP $COMPATLINK_LIBLDAP
	createLink $LIBLBER $COMPATLIB_LIBLBER $COMPATLINK_LIBLBER
	createLink $LIBSSL $COMPATLIB_LIBSSL $COMPATLINK_LIBSSL
	createLink $LIBCRYPTO $COMPATLIB_LIBCRYPTO $COMPATLINK_LIBCRYPTO
	if [ ! -L $LIB_COMPATLINKS64 ] ; then
                rm -rf $LIB_COMPATLINKS64
                ln -fs $ETC_COMPATLINKS64 $LIB_COMPATLINKS64
                if [ $? -ne 0 ]; then
                        print -u2 "[${PROG}] Failed to create $LIB_COMPATLINKS64 -> $ETC_COMPATLINKS64 link"
                        return 1
                elif $VERB; then
                        print "$LIB_COMPATLINKS64 -> $ETC_COMPATLINKS64 link enabled"
                fi
        fi


	if [ -d /usr/lib64 ]; then
		createLink $LIBGDBM64 $COMPATLIB_LIBGDBM64 $COMPATLINK64_LIBGDBM
		createLink $LIBLDAP64 $COMPATLIB_LIBLDAP64 $COMPATLINK64_LIBLDAP
		createLink $LIBLBER64 $COMPATLIB_LIBLBER64 $COMPATLINK64_LIBLBER
	fi
}


########################################################################
# Local Variables
# They must defined here to resolve the some of parameter resolution
# problem
########################################################################
local_variables() {

 	SRWC_LIB_COMPATLINKS=/opt/SUNWuttsc/lib/
 	LIBSSL=/usr/sfw/lib/libssl.so.0.9.7
 	LIBCRYPTO=/usr/sfw//lib/libcrypto.so.0.9.7
 	COMPATLIB_LIBSSL=/lib/libssl.so.0.9.*
 	COMPATLIB_LIBCRYPTO=/lib/libcrypto.so.0.9.*
 	COMPATLINK_LIBSSL=$SRWC_LIB_COMPATLINKS/${LIBSSL##*/}
 	COMPATLINK_LIBCRYPTO=$SRWC_LIB_COMPATLINKS/${LIBCRYPTO##*/}

	LIB_COMPATLINKS=$SUNWUTLIB/compatlinks
	ETC_COMPATLINKS=$SUNWUTETC/compatlinks
	LIB_COMPATLINKS64=$SUNWUTLIB/compatlinks64
	ETC_COMPATLINKS64=$SUNWUTETC/compatlinks64

	LIBGDBM=/usr/lib/libgdbm.so.3
	LIBGDBM64=/usr/lib64/libgdbm.so.3
	COMPATLIB_LIBGDBM=/usr/lib/libgdbm.so.2
	COMPATLIB_LIBGDBM64=/usr/lib64/libgdbm.so.2
	COMPATLINK_LIBGDBM=$LIB_COMPATLINKS/${LIBGDBM##*/}
	COMPATLINK64_LIBGDBM=$LIB_COMPATLINKS64/${LIBGDBM64##*/}

	LIBLDAP=/usr/lib/libldap.so.199
	LIBLDAP64=/usr/lib64/libldap.so.199
	COMPATLIB_LIBLDAP=/usr/lib/libldap-2.3.so.0
	COMPATLIB_LIBLDAP64=/usr/lib64/libldap-2.3.so.0
	COMPATLINK_LIBLDAP=$LIB_COMPATLINKS/${LIBLDAP##*/}
	COMPATLINK64_LIBLDAP=$LIB_COMPATLINKS64/${LIBLDAP##*/}

	LIBLBER=/usr/lib/liblber.so.199
	LIBLBER64=/usr/lib64/liblber.so.199
	COMPATLIB_LIBLBER=/usr/lib/liblber-2.3.so.0
	COMPATLIB_LIBLBER64=/usr/lib64/liblber-2.3.so.0
	COMPATLINK_LIBLBER=$LIB_COMPATLINKS/${LIBLBER##*/}
	COMPATLINK64_LIBLBER=$LIB_COMPATLINKS64/${LIBLBER##*/}

 	LIBSSL=/usr/lib/libssl.so.0.9.7
 	LIBCRYPTO=/usr/lib/libcrypto.so.0.9.7
	COMPATLIB_LIBSSL=/lib/libssl.so.0.9.8e
	COMPATLIB_LIBCRYPTO=/lib/libcrypto.so.0.9.8e
}


########################################################################
#
# Configurable parameter
#
########################################################################
# NONE


########################################################################
#
# Variables used in the shared script utctl-shlib
#
########################################################################
DESCR="Sets up compatible library links for Sun Ray programs"


########################################################################
#
# Execution starts here
#
PROGPATH=`dirname $0`
. $PROGPATH/utctl-shlib
