#!/bin/sh
# postinst script for ttf-mathematica4.1
#
# see: dh_installdeb(1)

set -e

# summary of how this script can be called:
#        * <postinst> `configure' <most-recently-configured-version>
#        * <old-postinst> `abort-upgrade' <new version>
#        * <conflictor's-postinst> `abort-remove' `in-favour' <package>
#          <new-version>
#        * <postinst> `abort-remove'
#        * <deconfigured's-postinst> `abort-deconfigure' `in-favour'
#          <failed-install-package> <version> `removing'
#          <conflicting-package> <version>
# for details, see http://www.debian.org/doc/debian-policy/ or
# the debian-policy package


if [ $1 = configure ]; then

. /usr/share/debconf/confmodule

FONTDIR=/usr/share/fonts/truetype/mathml
do_download=1

MATH1MD5="54cd2ea4c9bc003d0faad36e37216d41"
MATH1bMD5="5dc28700b9ddf1fad8c030edb412f840"
MATH1mMD5="fe602ef41a6464477919a769b4022622"
MATH1mbMD5="cb77c06f43a9821a1561988f6c27e681"
MATH2MD5="11f8e28510765d3b24c4fb2ceb97f130"
MATH2bMD5="d5ca821cc0fac14701f7aad1024ce81f"
MATH2mMD5="636713af127f0f8c19fe9b80d3aa5f21"
MATH2mbMD5="a923b9f601972f4589afbdf4e791d602"
MATH4MD5="ca99310612bc75fbc45e157012140613"
MATH4bMD5="7760e87b4d1e9d25ceeb5996f96e431f"
MATH4mMD5="043c314f8ae7b36f71d6fce4a3588cf9"
MATH4mbMD5="6c752ca4ad596343ed97464cc3241ce6"

if [ -f $FONTDIR/math1___.ttf ] && [ `md5sum $FONTDIR/math1___.ttf | awk '{print $1}'` = $MATH1MD5 ] && \
    [ -f $FONTDIR/math1b__.ttf ] && [ `md5sum $FONTDIR/math1b__.ttf | awk '{print $1}'` = $MATH1bMD5 ] && \
    [ -f $FONTDIR/math1m__.ttf ] && [ `md5sum $FONTDIR/math1m__.ttf | awk '{print $1}'` = $MATH1mMD5 ] && \
    [ -f $FONTDIR/math1mb_.ttf ] && [ `md5sum $FONTDIR/math1mb_.ttf | awk '{print $1}'` = $MATH1mbMD5 ] && \
    [ -f $FONTDIR/math2___.ttf ] && [ `md5sum $FONTDIR/math2___.ttf | awk '{print $1}'` = $MATH2MD5 ] && \
    [ -f $FONTDIR/math2b__.ttf ] && [ `md5sum $FONTDIR/math2b__.ttf | awk '{print $1}'` = $MATH2bMD5 ] && \
    [ -f $FONTDIR/math2m__.ttf ] && [ `md5sum $FONTDIR/math2m__.ttf | awk '{print $1}'` = $MATH2mMD5 ] && \
    [ -f $FONTDIR/math2mb_.ttf ] && [ `md5sum $FONTDIR/math2mb_.ttf | awk '{print $1}'` = $MATH2mbMD5 ] && \
    [ -f $FONTDIR/math4___.ttf ] && [ `md5sum $FONTDIR/math4___.ttf | awk '{print $1}'` = $MATH4MD5 ] && \
    [ -f $FONTDIR/math4b__.ttf ] && [ `md5sum $FONTDIR/math4b__.ttf | awk '{print $1}'` = $MATH4bMD5 ] && \
    [ -f $FONTDIR/math4m__.ttf ] && [ `md5sum $FONTDIR/math4m__.ttf | awk '{print $1}'` = $MATH4mMD5 ] && \
    [ -f $FONTDIR/math4mb_.ttf ] && [ `md5sum $FONTDIR/math4mb_.ttf | awk '{print $1}'` = $MATH4mbMD5 ] ; then
    echo "All TTF files already exist so will not download the file."
    do_download=0
fi

if [ $do_download -eq 1 ]; then

db_get ttf-mathematica4.1/http_proxy || true
http_proxy=$RET
export http_proxy

EXEMD5="6c952203198216c53aa968d4d12b0c61  MathFonts_TrueType_41.exe"

URL="http://support.wolfram.com/mathematica/systems/windows/general/files/"
FILE="MathFonts_TrueType_41.exe"

TTF="math1___.ttf  math2___.ttf  math3___.ttf  math4___.ttf"
TTFb="math1b__.ttf  math2b__.ttf  math3b__.ttf  math4b__.ttf"
TTFm="math1m__.ttf  math2m__.ttf  math3m__.ttf  math4m__.ttf"
TTFmb="math1mb_.ttf  math2mb_.ttf  math3mb_.ttf  math4mb_.ttf"

SCRATCHDIR=`mktemp -t -d mathematica.XXXXXX`
chmod 0755 $SCRATCHDIR
cd $SCRATCHDIR

   if [ -n "$QUIET_MODE" ] ; then
       QUIET_ARG="--quiet"
   else
       QUIET_ARG=""
   fi

   if ! wget --continue --tries=1 --dns-timeout=10 --connect-timeout=5 --read-timeout=300 $QUIET_ARG --directory-prefix . --no-directories --no-background $URL$FILE ; then
       echo "Download seems to fail."
   fi

THISSUM=`echo $EXEMD5 | awk '{print $1}'`
THISFILE=`echo $EXEMD5 | awk '{print $2}'`
echo checking $THISFILE
   if [ ! -f $THISFILE ] || [ `md5sum $THISFILE | awk '{print $1}'` != $THISSUM ] ; then
       echo "Downloaded file looks corrupted!"
       exit 1
   fi

unzip $FILE 1>&2
rm $FILE
chmod 644 *

mkdir -p $FONTDIR
mv $TTF $TTFb $TTFm $TTFmb $FONTDIR
#mv *.ttf $FONTDIR

cd /
rm -rf $SCRATCHDIR

   if [ -z "$QUIETMODE" ] ; then
       if [ -e $FONTDIR/math1___.ttf -a -e $FONTDIR/math2___.ttf -a -e $FONTDIR/math4___.ttf ] && \
	   [ -e $FONTDIR/math1b__.ttf -a -e $FONTDIR/math2b__.ttf -a -e $FONTDIR/math4b__.ttf ] && \
	   [ -e $FONTDIR/math1m__.ttf -a -e $FONTDIR/math2m__.ttf -a -e $FONTDIR/math4m__.ttf ] && \
	   [ -e $FONTDIR/math1mb_.ttf -a -e $FONTDIR/math2mb_.ttf -a -e $FONTDIR/math4mb_.ttf ] ; then
           echo "All fonts are downloaded and installed successfully."
       else
           echo "Installation seems failed by unknown reason."
       fi
   fi

fi # end of "do_download=1"
fi

# dh_installdeb will replace this with shell code automatically
# generated by other debhelper scripts.

#DEBHELPER#

exit 0


