#!/bin/bash
#
   MBRZIP='mbr301h.zip'
MBROLAURL="http://tcts.fpms.ac.be/synthesis/mbrola/bin/pclinux/$MBRZIP"
   TMPDIR='/tmp'
   TMPMBR='mbrola'
MBROLABIN="$TMPDIR/$TMPMBR/mbrola-linux-i386"
   BINDIR='/usr/bin'

# Create temporary directory and download the zip file
mkdir -p $TMPDIR/$TMPMBR && (cd $TMPDIR/$TMPMBR && curl -O $MBROLAURL)

if test $? == '0' && test -r $TMPDIR/$TMPMBR/$MBRZIP ; then
	ZIPFILE=$(find $TMPDIR/$TMPMBR -name '*.zip' -printf '%f')
	(cd $TMPDIR/$TMPMBR && unzip -uo $ZIPFILE)
else
	echo 'Download error' >&2
	rm -rf $TMPDIR/$TMPMBR
	exit 1
fi

if test -x $MBROLABIN ; then
	if ! test "$EUID" == '0' ; then
		echo 'Please enter the root password to install mbrola:'
		su -c "cp $MBROLABIN $BINDIR/mbrola && chown root.root $BINDIR/mbrola && chmod 0755 $BINDIR/mbrola"
	else
		cp $MBROLABIN $BINDIR/mbrola && chown root.root $BINDIR/mbrola && chmod 0755 $BINDIR/mbrola
	fi

	if test $? == '0' ; then
		rm -rf $TMPDIR/$TMPMBR
		echo "mbrola has been installed as $BINDIR/mbrola"
		exit 0
	fi
else
	echo "ERROR: unzip failed; $MBROLABIN not found"
	echo "Check the contents of $TMPDIR/$TMPMBR/"
	exit 1
fi
	