#!/bin/sh
#
# Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#
# ident	"@(#)postremove_fonts	1.8	07/05/14 SMI"
#

EXEC_BASE=/usr
PKGCOND=/usr/bin/pkgcond
MKFONTDIR=$EXEC_BASE/openwin/bin/mkfontdir
ENV_MKFONTDIR=$MKFONTDIR
DISKLESS_SRVC=`echo $BASEDIR | /usr/bin/grep export/Solaris_[1-9]*[0-9]/usr_${ARCH}.all`

is_srngz=99
is_ngz=99

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

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
} 

if [ $is_ngz -eq 0 ]; then
	if [ "X$DISKLESS_SRVC" = "X" ]; then
		EXEC_BASE=$BASEDIR
		MKFONTDIR=$EXEC_BASE/openwin/bin/mkfontdir
	fi
	ENV_MKFONTDIR="env LD_LIBRARY_PATH=$BASEDIR/openwin/server/lib:$BASEDIR/openwin/lib $MKFONTDIR"
fi

if [ $is_srngz -ne 0 ]; then
     if [ ! -x $MKFONTDIR ]
     then
         # if mkfontdir doesn't exist we cannot proceed
         exit 2
     fi
fi

#
# Modify {75dpi,TTbitmaps}/fonts.dir
#

FONTINSTDIR=$BASEDIR/openwin/lib/locale/ja/X11/fonts

if [ -d $FONTINSTDIR/75dpi ]; then
        test_writable $FONTINSTDIR/75dpi > /dev/null 2>&1
        if [ $? = 0 ]; then
	    cd $FONTINSTDIR/75dpi
	    $ENV_MKFONTDIR . || exit 2
	fi
fi

if [ -d $FONTINSTDIR/TTbitmaps ]; then
        test_writable $FONTINSTDIR/TTbitmaps > /dev/null 2>&1
        if [ $? = 0 ]; then
	    cd $FONTINSTDIR/TTbitmaps
	    $ENV_MKFONTDIR . || exit 2

	    if [ -f /tmp/fonts.dir ]; then
		rm /tmp/fonts.dir
	    fi
	    cd $FONTINSTDIR/TTbitmaps
	    count=`sed -e '1 d' -e '/^HeiseiMin-W3H/ d' ./fonts.dir | wc -l`
	    echo $count > /tmp/fonts.dir
	    sed -e '1 d' -e '/^HeiseiMin-W3H/ d' ./fonts.dir >> /tmp/fonts.dir
	    mv /tmp/fonts.dir fonts.dir
	    chgrp bin fonts.dir
	fi
fi

if [ -d $FONTINSTDIR/TT ]; then
        test_writable $FONTINSTDIR/TT > /dev/null 2>&1
        if [ $? = 0 ]; then
	    cd $FONTINSTDIR/TT
	    $ENV_MKFONTDIR . || exit 2
	fi
fi

exit 0
