#!/bin/sh
version=0.60.6.1
patch=aspell-0.60.6.1-mingw.patch
dicts="aspell-af-0.50-0.tar.bz2 aspell6-am-0.03-1.tar.bz2 \
  aspell6-ar-1.2-0.tar.bz2 aspell6-ast-0.01.tar.bz2 aspell6-az-0.02-0.tar.bz2 \
  aspell5-be-0.01.tar.bz2 aspell6-bg-4.1-0.tar.bz2 aspell6-bn-0.01.1-1.tar.bz2 \
  aspell-br-0.50-2.tar.bz2 aspell6-ca-2.1.5-1.tar.bz2 \
  aspell6-cs-20040614-1.tar.bz2 aspell6-csb-0.02-0.tar.bz2 \
  aspell-cy-0.50-3.tar.bz2 aspell5-da-1.4.42-1.tar.bz2 \
  aspell6-de-20030222-1.tar.bz2 aspell6-de-alt-2.1-1.tar.bz2 \
  aspell-el-0.50-3.tar.bz2 aspell6-en-7.1-0.tar.bz2 \
  aspell6-eo-2.1.20000225a-2.tar.bz2 aspell6-es-1.11-2.tar.bz2 \
  aspell6-et-0.1.21-1.tar.bz2 aspell6-fa-0.11-0.tar.bz2 \
  aspell6-fi-0.7-0.tar.bz2 aspell5-fo-0.2.16-1.tar.bz2 \
  aspell-fr-0.50-3.tar.bz2 aspell6-fy-0.12-0.tar.bz2 aspell5-ga-4.5-0.tar.bz2 \
  aspell5-gd-0.1.1-1.tar.bz2 aspell6-gl-0.5a-2.tar.bz2 \
  aspell6-grc-0.02-0.tar.bz2 aspell6-gu-0.03-0.tar.bz2 \
  aspell-gv-0.50-0.tar.bz2 aspell6-he-1.0-0.tar.bz2 aspell6-hi-0.02-0.tar.bz2 \
  aspell5-hil-0.11-0.tar.bz2 aspell-hr-0.51-0.tar.bz2 \
  aspell6-hsb-0.02-0.tar.bz2 aspell6-hu-0.99.4.2-0.tar.bz2 \
  aspell6-hy-0.10.0-0.tar.bz2 aspell-ia-0.50-1.tar.bz2 aspell5-id-1.2-0.tar.bz2\
  aspell-is-0.51.1-0.tar.bz2 aspell6-it-2.2_20050523-0.tar.bz2 \
  aspell6-kn-0.01-1.tar.bz2 aspell5-ku-0.20-1.tar.bz2 aspell6-ky-0.01-0.tar.bz2\
  aspell6-la-20020503-0.tar.bz2 aspell6-lt-1.2.1-0.tar.bz2 \
  aspell6-lv-0.5.5-1.tar.bz2 aspell5-mg-0.03-0.tar.bz2 aspell-mi-0.50-0.tar.bz2\
  aspell-mk-0.50-0.tar.bz2 aspell6-ml-0.03-1.tar.bz2 aspell6-mn-0.06-2.tar.bz2 \
  aspell6-mr-0.10-0.tar.bz2 aspell-ms-0.50-0.tar.bz2 aspell-mt-0.50-0.tar.bz2 \
  aspell-nb-0.50.1-0.tar.bz2 aspell6-nds-0.01-0.tar.bz2 \
  aspell-nl-0.50-2.tar.bz2 aspell-nn-0.50.1-1.tar.bz2 aspell5-ny-0.01-0.tar.bz2\
  aspell6-or-0.03-1.tar.bz2 aspell6-pa-0.01-1.tar.bz2 \
  aspell6-pl-6.0_20061121-0.tar.bz2 aspell6-pt_BR-20090702-0.tar.bz2 \
  aspell6-pt_PT-20070510-0.tar.bz2 aspell6-qu-0.02-0.tar.bz2 \
  aspell5-ro-3.3-2.tar.bz2 aspell6-ru-0.99f7-1.tar.bz2 aspell-rw-0.50-0.tar.bz2\
  aspell5-sc-1.0.tar.bz2 aspell6-sk-2.01-2.tar.bz2 aspell-sl-0.50-0.tar.bz2 \
  aspell6-sr-0.02.tar.bz2 aspell-sv-0.51-0.tar.bz2 aspell-sw-0.50-0.tar.bz2 \
  aspell6-ta-20040424-1.tar.bz2 aspell6-te-0.01-2.tar.bz2 \
  aspell5-tet-0.1.1.tar.bz2 aspell5-tk-0.01-0.tar.bz2 aspell5-tl-0.02-1.tar.bz2\
  aspell5-tn-1.0.1-0.tar.bz2 aspell-tr-0.50-0.tar.bz2 \
  aspell6-uk-1.4.0-0.tar.bz2 aspell6-uz-0.6-0.tar.bz2 \
  aspell6-vi-0.01.1-1.tar.bz2 aspell-wa-0.50-0.tar.bz2 \
  aspell6-yi-0.01.1-1.tar.bz2 aspell-zu-0.50-0.tar.bz2"
topdir=`pwd`
rm -rf build
mkdir build build/staging build/bin build/dist
cd build
gzip -dc < ../aspell-${version}.tar.gz | tar xf -
cd aspell-${version}
patch -p0 < ../../$patch
./configure --disable-nls --enable-win32-relocatable \
  --enable-compile-in-filters --enable-32-bit-hash-fun --prefix=/usr \
  --enable-pkgdatadir=/usr/lib/aspell-0.60
make
make install DESTDIR=$topdir/build/staging
cd ..
rm -rf aspell-${version}
gzip -dc < ../aspell-${version}.tar.gz | tar xf -
cd aspell-${version}
patch -p0 < ../../$patch
./configure --host=i686-pc-mingw32 --disable-nls --enable-win32-relocatable \
  --enable-compile-in-filters --enable-32-bit-hash-fun --prefix=/usr \
  --enable-pkgdatadir=/usr/lib/aspell-0.60
make
make install DESTDIR=$topdir/build/dist
cd ..
echo "#!/bin/sh" > bin/aspell
echo "export LD_LIBRARY_PATH=$topdir/build/staging/usr/lib" >> bin/aspell
echo "$topdir/build/staging/usr/bin/aspell --prefix $topdir/build/dist/usr \"\$@\"" >> bin/aspell
chmod +x bin/aspell
echo "#!/bin/sh" > bin/prezip
echo "export LD_LIBRARY_PATH=$topdir/build/staging/usr/lib" >> bin/prezip
echo "$topdir/build/staging/usr/bin/prezip-bin \"\$@\"" >> bin/prezip
chmod +x bin/prezip
for dict in $dicts; do
    case $dict in
	*.tar.gz)
	    subdir=`echo $dict | sed 's/\.tar\.gz$//'`
	    gzip -dc < ../$dict | tar xf -
	    ;;
	*.tar.bz2)
	    subdir=`echo $dict | sed 's/\.tar\.bz2$//'`
	    bzip2 -dc < ../$dict | tar xf -
	    ;;
	*.tar.xz)
	    subdir=`echo $dict | sed 's/\.tar\.xz$//'`
	    xz -dc < ../$dict | tar xf -
	    ;;
	*)
	    echo "$dict: Unknown archive format"
	    exit 1
	    ;;
    esac
    if [ ! -d $subdir ]; then
	echo "$dict: Doesn't contain $subdir directory as expected"
	exit 1
    fi
    cd $subdir 
    ASPELL=$topdir/build/bin/aspell PREZIP=$topdir/build/bin/prezip ./configure
    make
    make install
    cd ..
done
cd dist/usr
zip -r $topdir/aspell-${version}-win32.zip .
