#!/bin/sh
#
# This will try to do some desired host specific tweaking to various 
# configuration files. This is called only if an old quick install does
# not exist. It also keeps the old xp.cnf if it exists, and renames the
# new one to xp.cnf.[date/time].
#

DATE=`date +%m%d%y%H%M`

FONTPATHS="/usr/lib/X11/fonts/100dpi /usr/lib/X11/fonts/75dpi /usr/lib/X11/fonts/misc"

BOOT_PATH=$2/tekxp/boot
CONFIG_PATH=$BOOT_PATH/config
BOOT_FONTS=$BOOT_PATH/fonts
INSTALLDIR=$1
AUDITLOG=$3

TEE="tee -a $AUDITLOG"

if [ ! -d "$BOOT_PATH" ]
then
    echo "Error - the 'tekxp/boot' directory does not exist" | $TEE
    exit 1
fi

if [ ! -d "$CONFIG_PATH" ]
then
    echo "Error - the 'tekxp/boot/config' directory does not exist" | $TEE
    exit 1
fi

#
# Fixup permissions on the xp.cnf and table files
#
if [ -f $CONFIG_PATH/xp.cnf ]
then
    chmod 644 $CONFIG_PATH/xp.cnf
fi

for file in `find $CONFIG_PATH/* -type f -name "*.tbl" -print`
do 
    chmod 644 $file 
done

FONTSTBL=$CONFIG_PATH/fonts.tbl
VALIDFONTS=
INSTALLEDFONTS=

#
# Try to ensure that there are some valid font paths in the fonts.tbl
# the priority is paths in the existing fonts.tbl, any installed fonts
# and then any possible system fonts
#

#
# If a fonts.tbl exists, see if the paths are valid
#

if [ -f "$FONTSTBL" ]
then
    #
    # see if the existing fonts.tbl already has some installed fonts in
    # it, if not check for other fonts, if so, then the installed font
    # list will be rebuilt later.
    #
    INSTALLEDFONTS=`grep Installed $FONTSTBL`
    DEFAULTFONTS=`grep Default $FONTSTBL`

    if [ -z "$INSTALLEDFONTS" ]
    then
        for path in `cat $FONTSTBL`
        do
	    if [ -f $path/fonts.dir ]
	    then
	        VALIDFONTS=$path
	    fi
        done
    fi
fi

if [ -z "$VALIDFONTS" -o "$DEFAULTFONTS" ]
then
    
    if [ -d "$BOOT_FONTS" ]
    then
        echo "# Installed fonts" > $FONTSTBL

        for path in `find $BOOT_FONTS -type d -print`
        do
            if [ $path != "$BOOT_FONTS" ]
            then
	        if [ -f $path/fonts.dir ]
	        then
		    VALIDFONTS=$path
                    echo $path >> $FONTSTBL
		    echo "Adding installed $path to $FONTSTBL" >> $AUDITLOG
		    DEFAULTFONTS=""
	        fi
            fi
        done 
    fi
fi

if [ -z "$VALIDFONTS" -o "$DEFAULTFONTS" ]
then
    #
    # if no local fonts were installed, try to use system fonts
    #
    echo "# System fonts" > $FONTSTBL
    for path in $FONTPATHS
    do 
  	if [ -f $path/fonts.dir ]
	then
	    VALIDFONTS=$path
            echo $path >> $FONTSTBL
	    echo "Adding system $path to $FONTSTBL" >> $AUDITLOG
	fi
    done 
fi

#
# If no fonts were found then insert a message in the fonts.tbl stating
# this
#
if [ -z "$VALIDFONTS" ]
then
    echo "#No fonts were installed and no system fonts were found" >> $FONTSTBL
    echo "#Please locate paths and, if found, list them below" >> $FONTSTBL
fi
