#!/bin/sh
#
# Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#
# SUNWkxfnt (Korean X Windows Platform Required Fonts) postinstall script.
#

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

if [ $is_ngz -eq 0 ]; then
    if [  "$DISKLESS_SRVC" ]; then
        EXEC_BASE=/usr
    else
        EXEC_BASE=$BASEDIR
    fi

    MKFONTDIR=$EXEC_BASE/openwin/bin/mkfontdir
    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

##
## '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
}

FONTINSTDIR=$BASEDIR/openwin/lib/locale/ko/X11/fonts
for i in cidfonts cidfonts/fonts/F3 75dpi F3bitmaps; do
    if [ -d  $FONTINSTDIR/$i ]; then
            if test_writable $FONTINSTDIR/$i; then
                cd $FONTINSTDIR/$i
                $ENV_MKFONTDIR .
            fi
    fi
done

FONTINSTDIR=$BASEDIR/openwin/lib/locale/ko/X11/fonts/75dpi
if [ -d  $FONTINSTDIR ]; then
        if test_writable $FONTINSTDIR; then
            cd $FONTINSTDIR
            [ -f fonts.alias.base ] && cp fonts.alias.base fonts.alias
        fi
fi
		
FONTINSTDIR=$BASEDIR/openwin/lib/X11/fonts/CSL
if [ -d  $FONTINSTDIR ]; then
        if test_writable $FONTINSTDIR; then
            cd $FONTINSTDIR
            $ENV_MKFONTDIR .
        fi
fi

FONTINSTDIR=$BASEDIR/openwin/lib/X11/fonts/encodings
if [ -d  $FONTINSTDIR ]; then
        if test_writable $FONTINSTDIR; then
            cd $FONTINSTDIR
            # Remove ksc5601.enc from encodings.dir if exist
            tmp_dest=/tmp/encodings.dir
            
            if [ -f encodings.dir ] ; then
                read number < encodings.dir
                CMD=""
                for line in `grep -n ksc5601.enc encodings.dir|cut -d: -f1`; do
                    # for each ksc5601.enc remove line above and
                    # this line itself
                    # Search for ksc5601.enc
                    number=`expr $number - 1`
                    echo number > $tmp_dest
                    line_up=`expr $line - 1`
                    CMD="-e '$line_up,$line d' $CMD"
                done
                echo $number > $tmp_dest
                echo "cat encodings.dir | sed $CMD | sed -e '1,1 d'" > /tmp/.$$cmd
                
                if [ "y$CMD" != "y" ]; then
                    sh /tmp/.$$cmd >> $tmp_dest
                    cp $tmp_dest encodings.dir
                fi
                
                rm $tmp_dest $chg_sh /tmp/.$$cmd
            fi
        fi
fi

installf -f $PKGINST || exit 2

exit 0
