# Copyright Stefan Dirsch <sndirsch@suse.de> This script was 
# included with the Suse package xf86tools, and modified for 
# a Redhat Linux system with the authors permission by
# Devon <devon@tuxfan.homeip.net> 

#!/bin/sh

FONTS=" \
http://www.microsoft.com/typography/downloads/webdin32.exe \
http://www.microsoft.com/typography/downloads/andale32.exe \
http://www.microsoft.com/typography/downloads/trebuc32.exe \
http://www.microsoft.com/typography/downloads/georgi32.exe \
http://www.microsoft.com/typography/downloads/verdan32.exe \
http://www.microsoft.com/typography/downloads/comic32.exe \
http://www.microsoft.com/typography/downloads/arialb32.exe \
http://www.microsoft.com/typography/downloads/impact32.exe \
http://www.microsoft.com/typography/downloads/arial32.exe \
http://www.microsoft.com/typography/downloads/times32.exe \
http://www.microsoft.com/typography/downloads/courie32.exe \
"

WGET_OPTIONS="-q --passive --proxy=on --user-agent=\"Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0)\""
EULA="http://www.microsoft.com/typography/fontpack/eula.htm"

if [ "`id -u`" != "0" ]; then
 echo "error: You must be root to use this program!"
 exit 1
fi

if [ -z $http_proxy ]; then
  echo 
  echo "note: No proxy is used. Please set the environment variable \"http_proxy\""
  echo "note: to your favorite proxy, if you want to use a proxy for the download."
  echo "note:"
  echo "note:   bash: export http_proxy=\"http://proxy.provider.de:3128/\""
  echo "note:   tcsh: setenv http_proxy \"http://proxy.provider.de:3128/\""
fi

echo
echo "By continuing with this program you log on to the Microsoft(c)-Webserver."
echo "Accepting the following Terms and conditions results in a contract between the"
echo "user and Microsoft(c). Neither SuSE, the vendor of this installation media"
echo "nor anyone else (other than you and Microsoft) is part of this contract."
echo
echo "After viewing the EULA, press 'q' to return to the installer."
echo
echo -n "Continue? [yes/NO] "

read answer
if [ "$answer" != "yes" ]; then
  exit 0
fi

mkdir -p /usr/X11R6/lib/X11/fonts/truetype/tmp
cd /usr/X11R6/lib/X11/fonts/truetype/tmp

wget $WGET_OPTIONS $EULA
if [ $? -ne 0 ]; then
  echo "error: EULA not found. Aborting now!"
  cd .. && rm -rf tmp
  exit 1
fi

lynx -dump $EULA | less
echo -n "Do you accept all of the terms of EULA ? [yes/NO] "
read answer
if [ "$answer" != "yes" ]; then
  echo "warning: EULA not accepted. Aborting now!"
  cd .. && rm -rf tmp
  exit 0
fi

for archive in $FONTS; do
  file=`echo $archive|awk -F "/" '{print $NF}'`
  rm -f $file
  echo "$file:"
  echo -n "  Fetching   ... "
  wget $WGET_OPTIONS $archive
  if [ $? -ne 0 ]; then
    rm -f $file
    echo "failed ... deleted!"
    continue
  fi
  echo done
  echo -n "  Extracting ... "
  cabextract -v $file &> /dev/null
  if [ $? -ne 0 ]; then
    rm -f $file
    echo "failed ... deleted!"
  else
    cabextract $file &> /dev/null
    echo "done"
    for i in *.TTF *.ttf; do
      lower=`echo $i|tr [:upper:] [:lower:]`
      mv $i ../$lower &> /dev/null
    done
  fi
done

cd .. && rm -rf tmp
chmod 644 *.ttf

echo

if [ -x /usr/bin/ttmkfdir ]; then
  echo -n "Creating fonts.scale ... "
  /usr/bin/ttmkfdir|grep ttf > fonts.scale.msttfonts
  echo "done"
else
  echo "error: ttmkfdir (RedHat package 'freetype') is required to create fonts.scale!"
  exit 1
fi

chmod 644 fonts.scale.msttfonts

if [ -x /usr/X11R6/bin/mkfontdir ]; then
  echo -n "Creating fonts.dir ... "
  echo 0 > fonts.scale
  ls fonts.scale.* &> /dev/null && cat fonts.scale.* >> fonts.scale
  /usr/X11R6/bin/mkfontdir -e /usr/X11R6/lib/X11/fonts/encodings/large -e /usr/X11R6/lib/X11/fonts/encodings 
  echo "done"
else
  echo "error: mkfontdir (RedHat package 'XFree86-xfs') is required to create fonts.dir!"
  exit 1
fi

chmod 644 fonts.scale fonts.dir
rm -f encodings.dir

if [ -x /usr/sbin/chkfontpath ]; then
  echo -n "Adding true type fonts to the first position in the fontpath... " 
  /usr/sbin/chkfontpath -f -a /usr/X11R6/lib/X11/fonts/truetype
  echo "done"
else
  echo "error: chkfontpath ('RedHat package chkfontpath') is required to add truetype fonts to the fontservers path."
  exit 1
fi

if [ -f /etc/X11/XftConfig ]; then
  echo -n "Adding true type fonts to /etc/X11/XftConfig... "
  echo "dir \"/usr/X11R6/lib/X11/fonts/truetype\"" >> /etc/X11/XftConfig
  echo "done"
else
  echo "error: Failed attempting to add the font path to /etc/X11/XftConfig!"
  exit 1
fi
