#! /bin/sh


	#
	# script takes Sun format fonts
	# in $OPENWINHOME/lib/fonts and
	# converts them into X11 BDF format
	# These are then converted into HP Netstation SNF format
	# and placed in /usr/bin/X11/700X/fonts/Xol. 
	# Finally a fonts.dir and a fonts.alias are created 
	#
	# User dialogue is weak and
	# customization can only be done using an editor 
	#

set -a

CONVERTFONT="/usr/openwin/bin/convertfont"

BDFTOSNF=/usr/openwin/bin/bdftosnf
if [ -x $BDFTOSNF ] 
then
 BDFTOSNF="/usr/openwin/bin/bdftosnf"
else
 BDFTOSNF="/usr/bin/X11/700X/xtbdftosnf"
fi

XTMKFONTDIR=/usr/bin/X11/700X/xtmkfontdir

FONTDIR=/usr/lib/X11/700X/fonts
OUTDIR=$FONTDIR/Xol

if [ ${OPENWINHOME}x = "x" ]
then
  INDIR=/usr/openwin/lib/fonts
else
  INDIR=$OPENWINHOME/lib/fonts
fi

PSIZE=12
FILES="[a-h]*${PSIZE}*"


echo "input directory (variable INDIR) is $INDIR"
echo "output directory (variable OUTDIR) is $OUTDIR"
echo "files selected (variable FILES) are from an \"ls $FILES.fb\""
echo


echo "enter <return> to continue with these defaults or QUIT (^C) to exit"
echo -n "and (possibly) modify these variables, which are at top of script:  "
read REPLY




	#
	# Check for existence of input directory
	#

if [ ! -d $INDIR ]
then
  echo directory $INDIR does not exist.
  echo please change variable INDIR at top of this script and re-run.
  exit 1
fi


	#
	# Check for existence of fb files in input directory
	#

ls $INDIR/$FILES.fb 2>&1 | grep -s 'not found'
if [ $? -eq 0 ]
then
  echo no $FILES.fb files in directory $INDIR
  echo please change variable INDIR and/or FILES at top of this script and re-run
  exit 1
fi



	#
	# Create output directory if not there
	#

if [ ! -d $OUTDIR ]
then
  mkdir -p $OUTDIR
  if [ $? -ne 0 ]
  then
    echo could not create directory $OUTDIR
    echo please change variable OUTDIR at top of this script and re-run.
    exit 1
  fi
fi


	#
	# Create exclusion list of fonts already in other font directories
	#

FILELIST=/usr/tmp/flist

FDIR1=$FONTDIR/misc
if [ ! -d $FDIR1 ]
then
  FDIR1=""
fi

FDIR2=$FONTDIR/iso_8859.1
if [ ! -d $FDIR2 ]
then
  FDIR2=""
fi

FDIR3=$FONTDIR/r2
if [ ! -d $FDIR3 ]
then
  FDIR3=""
fi


find $FDIR1 $FDIR2 $FDIR3 -name '*.snf' -print \
	| awk -F/ '{print $NF}' > $FILELIST



	#
	# Convert to BDF one by one, echo a dot/file for visual feed-back
	# converting fonts from Sun .fb format to X11 BDF format
	#

echo
echo converting fonts from Sun .fb format to X11 BDF format
for i in `ls $INDIR/$FILES.fb`
do

	# skip if length is 0
	#

  if [ ! -s $i ]
  then
    continue
  fi
  

	# exclude if already in one of other font dirs
	#

  SNFNAME=`basename $i | sed 's/.fb$/.snf/g'`
  egrep $SNFNAME $FILELIST > /dev/null
  if [ $? -eq 0 ]
  then
    continue
  fi
   

  OPTION="-S"
  OUTNAME=$OUTDIR/`basename $i`
  $CONVERTFONT -x $OPTION -o $OUTNAME $i > /dev/null
  egrep 'SIZE 0' $OUTNAME.bdf > /dev/null
  if [ $? -eq 0 ]
  then
    echo point size forced to $PSIZE for $OUTNAME
    OPTION="-s$PSIZE"
    $CONVERTFONT -x $OPTION -o $OUTNAME $i > /dev/null
  else
    echo -n "."
  fi
done



	#
	# get rid of filename . extensions 
	#

echo
for i in `ls $OUTDIR/$FILES.fb.bdf`
do
  echo -n "."
  mv $i `echo $i | sed 's/.fb.bdf$//g'`
done



	#
	# Convert to SNF, one by one, echo a dot/file for visual feed-back
	#

echo
echo
echo converting fonts from X11 BDF format to HP Netstation SNF format
for i in `ls $OUTDIR/$FILES`
do
  echo -n "."
  $BDFTOSNF -t < $i > $i.snf  2> /dev/null
  rm $i
done



	#
	# Create fonts.dir and fonts.alias
	#
echo
echo
echo creating \"fonts.dir\" and \"fonts.alias\" in $OUTDIR 

echo "FILE_NAMES_ALIASES" > $OUTDIR/fonts.alias
$XTMKFONTDIR $OUTDIR


echo

chmod ugo+rx $OUTDIR

for i in `ls $OUTDIR`
do
  chmod ugo+r $OUTDIR/$i
done

echo "For new font directory to take effect on a particular netstation you may:"
echo " 1) Add font path to Server configuration screen, (<press F12> on unit)"
echo "    (works only if HP Netstation is running B.04 or later)"
echo " 2) or add font path to the terminalname.cfg file and reboot unit"
echo " 3) or add \"xset fp+ $OUTDIR/\" in ~.xsession"
echo "    and then restart session"
echo " 4) or type \"xset fp+ $OUTDIR/\" on command line,"
echo "    which is of temporary effect only."
echo ""
echo "done"
 
exit 0
