#!/bin/sh

EULA="http://corefonts.sourceforge.net/eula.htm"

FONTS=" \
dl.sourceforge.net/sourceforge/corefonts/andale32.exe \
dl.sourceforge.net/sourceforge/corefonts/arial32.exe  \
dl.sourceforge.net/sourceforge/corefonts/arialb32.exe \
dl.sourceforge.net/sourceforge/corefonts/comic32.exe  \
dl.sourceforge.net/sourceforge/corefonts/courie32.exe \
dl.sourceforge.net/sourceforge/corefonts/georgi32.exe \
dl.sourceforge.net/sourceforge/corefonts/impact32.exe \
dl.sourceforge.net/sourceforge/corefonts/times32.exe  \
dl.sourceforge.net/sourceforge/corefonts/trebuc32.exe \
dl.sourceforge.net/sourceforge/corefonts/verdan32.exe \
dl.sourceforge.net/sourceforge/corefonts/webdin32.exe \
"

SERVER=" \
puzzle   \
ovh      \
belnet   \
switch   \
citkit   \
mesh     \
jaist    \
cogent   \
kent     \
nchc     \
heanet   \
easynews \
optusnet \
voxel    \
"

CURL_OPTIONS="-s --speed-limit 3500 --speed-time 15"

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

if [ ! -x /usr/bin/cabextract ]; then
  echo "error: cabextract missing! Please install package cabextract first."
  exit 2
fi

. /etc/sysconfig/proxy

if test "$PROXY_ENABLED" != "no"; then
  if test -n "$HTTP_PROXY" ; then
    export http_proxy="$HTTP_PROXY"
  fi
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

# for whatever reason using auth proxies does not work
if [ -r $HOME/.curlrc ]; then
  grep -q proxy-user $HOME/.curlrc 2> /dev/null 
  if [ $? -eq 0 ]; then
    echo "Using authenticated proxies seems to be broken. Aborting ..."
    exit 3
  fi
fi

echo "EULA:"
mkdir -p /usr/share/doc/corefonts
echo -n "  Fetching   ... "
curl $CURL_OPTIONS -o /usr/share/doc/corefonts/EULA.html $EULA || \
  rm -f /usr/share/doc/corefonts/EULA.html
echo "done"

tmpname=`basename $0`
tmpdir=`mktemp -d /tmp/$tmpname.XXXXXX`
trap "rm -rf $tmpdir" EXIT
if [ $? -ne 0 ]; then
  echo "$0: Can't create temp dir, exiting..."
  exit 4
fi

pushd $tmpdir &> /dev/null

echo
echo "Trying to find the fastest server:"
besttime=1000
useserver=$(echo $SERVER|awk '{print $1}')
echo " default: $useserver"

for server in $SERVER; do
  echo -n " $server ... "
  start=$SECONDS
  curl $CURL_OPTIONS --connect-timeout 10 -o cabextract.rpm \
    http://$server.dl.sourceforge.net/sourceforge/corefonts/cabextract-0.5-1.i386.rpm
  if [ $? -ne 0 ]; then
    echo "to slow (aborted)"
    continue
  fi
  stop=$SECONDS
  time=$((stop - start))
  echo "$time sec"
  if [ $time -lt $besttime ]; then 
    besttime=$time
    useserver=$server
  fi
done

rm -f cabextract.rpm
echo "The winner is: >> $useserver <<"
echo

for font in $FONTS; do
 for i in $useserver $SERVER; do
  archive=http://$i.$font
  file=`echo $archive|awk -F "/" '{print $NF}'`
  rm -f $file
  echo "$file ($archive):"
  echo -n "  Fetching   ... "
  curl $CURL_OPTIONS -o $file $archive
  if [ $? -ne 0 ]; then
    rm -f $file
    echo "failed ... deleted!"
    continue
  fi
  echo done
  echo -n "  Extracting ... "
  cabextract -l $file &> /dev/null
  if [ $? -ne 0 ]; then
    rm -f $file
    echo "failed ... deleted!"
  else
    cabextract $file &> /dev/null
    echo "done"
    success=true
    break
  fi
  rm -f $file
 done
done

if [ "x$success" != "x" ]; then 
  for i in *.[Tt][Tt][CFcf]; do
    lower=`echo $i|tr [:upper:] [:lower:]`
    test "$i" != "$lower" && mv $i $lower
  done
  chmod 644 *.tt[cf]
  mv -f *.tt[cf] /usr/X11R6/lib/X11/fonts/truetype
  /usr/sbin/fonts-config
  echo "*** Fonts installed. ***"
else
  echo "*** No Fonts installed. ***"
fi

popd &> /dev/null
