#!/bin/ksh -p
#
# ident "@(#)utX11compatlinksctl.ksh	1.4 09/06/19 SMI"
#
# Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#

set -u

########################################################################
#
# Creates compatibility library symlinks in /etc/opt/SUNWut/X11
# 
# Checks the availability of the required SecurityPolicy file.
# If the policy file is present then creates a symlink.
# If policy file is not available then it will fall back to the one
# delivered in /opt/SUNWut/lib/X11.
#
# Creates the fontpath file under /opt/SUNWut/lib/X11.  It will first
# determine the location for the font files on the server.  Then, it will
# will generate the list of font paths by prepending this location to the
# list font directories defined in the /opt/SUWNut/lib/X11/fonthpath.template
# file.  It will also verifies that the font directory actually exists
# and the fonts.dir actually contains some valid entries.
#
########################################################################


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


removeLink() {

	typeset LINKFILE=$1
	if [ -L $LINKFILE ] ; then
		rm -rf $LINKFILE
		if $VERB; then
			print "$LINKFILE link disabled"
		fi
	fi
	return 0

}

removeFile() {
	typeset FILENAME=$1
	if [ -f $FILENAME ]; then
		rm -rf $FILENAME
		if $VERB; then
			print "$FILENAME file disabled"
		fi
	fi
	return 0
}

disable() {
	removeLink ${ETC_COMPATLINKS}/${SECURITYPOLICY_FILE}
	removeLink ${ETC_COMPATLINKS}/${XKEYSYMDB_FILE}
	removeFile ${ETC_COMPATLINKS}/${FONTPATH_FILE}
}


createLink() {

	typeset SPFILE=$1
	typeset LINKFILE=$2
	
	rm -rf $LINKFILE
	ln -fs $SPFILE $LINKFILE
	if [ $? -ne 0 ]; then
		print -u2 "[${PROG}] Failed to create $LINKFILE -> $SPFILE link"
		return 1
	elif $VERB; then
		print "$LINKFILE -> $SPFILE link enabled"
	fi
	return 0
}

createFile() {
	typeset SOURCE_DIR=$1
	typeset TEMPLATE=$2
	typeset DEST_FILE=$3
	typeset VALIDATE=$4
	typeset TMPFILE=$SUNWUTVAR/tmp/${PROG}_createFile.$$

	if [ ! -f $TEMPLATE ]; then
		# this should never happen...
		print -u2 "[${PROG}] missing template file $TEMPLATE"
		return 1
	fi

	cat /dev/null > $TMPFILE
	cat $TEMPLATE | sed -e "s?@(SUBS)?${SOURCE_DIR}?g" | \
	while read ENTRY
	do
		if [ -z "${ENTRY}" -o -z "${ENTRY%%#*}" ]; then
			print $ENTRY >> $TMPFILE
			continue
		fi

		if $VERB; then
			print -n "[${PROG}] ($ENTRY): "
		fi
		if $VALIDATE ${ENTRY}; then
			if $VERB; then
				print "added into $DEST_FILE"
			fi
			print $ENTRY >> $TMPFILE
		else
			if $VERB; then
				print -- "- skipped"
			fi
		fi
	done
	if [ ! -s $TMPFILE ]; then
		# empty fille
		print -u2 "[${PROG}] failed to generate file $DEST_FILE"
		/bin/rm -rf $TMPFILE
		return 1
	fi
	mv -f $TMPFILE $DEST_FILE
}

doit() {
	typeset FILE=$1
	typeset PATHS=$2
	for P in $PATHS
	do
		SPFILE=${P}/${FILE}
		if [ -f ${SPFILE} ]; then
			createLink $SPFILE ${ETC_COMPATLINKS}/${FILE}
			return $?
		fi
	done
	# Target file not found, use the one in /opt/SUNWut/lib/X11
	createLink ${SUNWUTLIB}/X11/${FILE} ${ETC_COMPATLINKS}/${FILE}
	return $?
}

enable_securitypolicy() {
	doit ${SECURITYPOLICY_FILE} "$SECURITYPOLICY_PATHS"
	return $?
}

enable_xkeysymdb() {
	doit ${XKEYSYMDB_FILE} "$XKEYSYMDB_PATHS"
	return $?
}

# validatest the fontpath.
# returns 0 if successful, otherwise 1.
validate_font() {
	typeset FPATH=${1%%:*}

	if [ ! -d ${FPATH} ] -o [ ! -f ${FPATH}/fonts.dir ]; then
		if $VERB; then
			print -n "invalid font directory "
		fi
		return 1
	fi
	typeset SIZE=`head -1 ${FPATH}/fonts.dir`
	if [ $SIZE -eq 0 ]; then
		# emtpy font.dir
		if $VERB; then
			print -n "empty font directory "
		fi
		return 1
	fi
	return 0
}

enable_fontpath() {
	for P in $FONTPATH_PATHS
	do
		SPDIR=${P}
		if [ -d $SPDIR/fonts ]; then
			# found fontpath location on the server
			break;
		fi
		SPDIR=""
	done
	if [ -z "$SPDIR" ]; then
		print -u2 "[${PROG}] Couldn't locate the directory for the font files."
		return 1
	fi
	createFile ${SPDIR} ${FONTPATH_TEMPLATE} \
		${ETC_COMPATLINKS}/${FONTPATH_FILE} validate_font
	return $?
}

enable() {
	enable_securitypolicy
	enable_xkeysymdb
	enable_fontpath
	return $?
}


########################################################################
# Local Variables
# They must defined here to resolve the some of parameter resolution
# problem
########################################################################
local_variables() {
	ETC_COMPATLINKS=$SUNWUTETC/X11

	SECURITYPOLICY_FILE=SecurityPolicy
	SECURITYPOLICY_PATHS="/usr/X11R6/lib/X11/xserver /usr/lib/xserver"

	XKEYSYMDB_FILE=XKeysymDB
	XKEYSYMDB_PATHS="/usr/X11R6/lib/X11 /usr/share/X11"

	FONTPATH_FILE=fontpath
	FONTPATH_TEMPLATE="${SUNWUTLIB}/X11/fontpath.template"
	FONTPATH_PATHS="/usr/X11R6/lib/X11 /usr/share/X11"
}


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


########################################################################
#
# Variables used in the shared script utctl-shlib
#
########################################################################
DESCR="Sets up compatibility links for the Sun Ray Xserver program"


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