#!/bin/bash

#
# Shell script to install the APLX fonts in X-windows
#
# This script and the other files in this directory are copied
# by the installer to the "fonts" directory in the chosen installation
# path (e.g. /usr/local/aplx/fonts/) where they can be marked executable
#
# This script is then executed by the installer to copy
# the fonts to the appropriate X-windows font directories and
# update the relevant font configuration files.
#
# If the installation succeeds, the "installed_path" file
# is written, listing the paths where the fonts were installed
#

# For non-standard X-windows installations, give values
# for PCF_DIR, PT1_DIR, TTF_DIR

PCF_DIR=""      # Directory for screen font files (*.pcf)
                # (normally /usr/lib/X11/fonts/misc)
PT1_DIR=""      # Directory for printing font file (*.pfb)
                # (normally /usr/lib/X11/fonts/Type1)
TTF_DIR=""      # Directory for TrueType font (*.ttf)
                # (normally /usr/share/fonts/truetype)


SRC_DIR=$(dirname "$0")
HELP_FILE=$SRC_DIR/README.txt
INSTALLED_PATH_FILE=$SRC_DIR/installed_path


rm -f $INSTALLED_PATH_FILE


install_failed() {
	echo "*** Fonts have not been installed"
	echo "*** Please see $HELP_FILE"
	exit 1
}


# Must be root to install font files !
userid=$(id -u)
if [ "$userid" != "0" ]; then
	echo "*** Must be root to install X-windows fonts"
	install_failed
fi


# Look for default font locations if hard coded values
# have not been specified above

# --- Screen font:
if [ -z "$PCF_DIR" -a -d /usr/lib/X11/fonts/misc ]; then
	PCF_DIR=/usr/lib/X11/fonts/misc
fi

if [ -z "$PCF_DIR" -a -d /usr/X11R6/lib/X11/fonts/misc ]; then
	PCF_DIR=/usr/X11R6/lib/X11/fonts/misc
fi

if [ -z "$PCF_DIR" -a -d /usr/share/fonts/X11/misc ]; then
	PCF_DIR=/usr/share/fonts/X11/misc
fi

if [ -z "$PCF_DIR" -a -d /usr/share/X11/fonts/misc ]; then
        PCF_DIR=/usr/share/X11/fonts/misc
fi 

if [ -z "$PCF_DIR" -a -d /usr/share/fonts/misc ]; then
        PCF_DIR=/usr/share/fonts/misc
fi 

if [ -z "$PCF_DIR" -a -d /usr/share/fonts/local ]; then
        PCF_DIR=/usr/share/fonts/local
fi 

# Fall back on top-level
if [ -z "$PCF_DIR" -a -d /usr/share/fonts ]; then
        PCF_DIR=/usr/share/fonts
fi 


# --- Printing font:
if [ -z "$PT1_DIR" -a -d /usr/lib/X11/fonts/Type1 ]; then
	PT1_DIR=/usr/lib/X11/fonts/Type1
fi

if [ -z "$PT1_DIR" -a -d /usr/X11R6/lib/X11/fonts/Type1 ]; then
	PT1_DIR=/usr/X11R6/lib/X11/fonts/Type1
fi

if [ -z "$PT1_DIR" -a -d /usr/share/fonts/X11/Type1 ]; then
        PT1_DIR=/usr/share/fonts/X11/Type1
fi 

if [ -z "$PT1_DIR" -a -d /usr/share/X11/fonts/Type1 ]; then
        PT1_DIR=/usr/share/X11/fonts/Type1
fi 

if [ -z "$PT1_DIR" -a -d /usr/share/fonts/Type1 ]; then
        PT1_DIR=/usr/share/fonts/Type1
fi 

if [ -z "$PT1_DIR" -a -d /usr/share/fonts/default/Type1 ]; then
        PT1_DIR=/usr/share/fonts/default/Type1
fi 

# Fall back on top-level
if [ -z "$PT1_DIR" -a -d /usr/share/fonts ]; then
        PT1_DIR=/usr/share/fonts
fi 


# --- TrueType font [optional]:
if [ -z "$TTF_DIR" -a -d /usr/share/fonts/truetype ]; then
	TTF_DIR=/usr/share/fonts/truetype
fi

if [ -z "$TTF_DIR" -a -d /usr/share/fonts/ttf ]; then
	TTF_DIR=/usr/share/fonts/ttf
fi


# Found screen font directory?
if [ -z "$PCF_DIR" ]; then
	echo "*** Could not find X-windows font directory for APLX screen fonts"
	install_failed
fi

### Install the screen fonts (*.pcf)
cp -f $SRC_DIR/aplx*.pcf $PCF_DIR
$SRC_DIR/add_aliases.sh  $SRC_DIR/fonts.alias.aplx $PCF_DIR/fonts.alias
retcode=$?

# Quick sanity check
check=$(ls $PCF_DIR/aplx*.pcf)
if [ $retcode != 0 -o -z "$check" ]; then
	echo "*** An error occurred while installing APLX screen fonts"
	install_failed
fi


# Found printer font directory?
if [ -z "$PT1_DIR" ]; then
	echo "*** Could not find X-windows font directory for APLX printer font"
	install_failed
fi

### Install the printer font (*.pfb)
cp -f $SRC_DIR/aplx*.pfb $PT1_DIR
$SRC_DIR/add_scales.sh  $SRC_DIR/fonts.scale.aplx $PT1_DIR/fonts.scale
retcode=$?

# Quick sanity check
check=$(ls $PT1_DIR/aplx*.pfb)
if [ $retcode != 0 -o -z "$check" ]; then
	echo "*** An error occurred while installing APLX printer font"
	install_failed
fi

if [ -z "$TTF_DIR" ]; then
    echo "*** Warning: APLX TrueType font not installed"
else
    ### Install the TrueType font (aplx_unicode.ttf)
    cp -f $SRC_DIR/aplx*.ttf $TTF_DIR
fi


# Save the font directories we're using as these are used
# by the APLX startup script ../bin/startaplx 
echo "$PCF_DIR,$PT1_DIR" > $INSTALLED_PATH_FILE


# Now have a go at prodding the X font server and/or the X server
# to pick up the new fonts

# Unfortunately this step is very dependent on system configuration
# and may not work for all distributions and versions

mkfontdir $PCF_DIR
mkfontdir $PT1_DIR

if [ -x /etc/rc.d/init.d/xfs ]; then
	/etc/rc.d/init.d/xfs restart > /dev/null
fi

if [ -n "$DISPLAY" ]; then
	xset fp rehash
fi
