#!/bin/sh

set -e

. /usr/share/debconf/confmodule

set -u

PKG=bgoffice-dict-downloader

DEST="/usr/share/bgoffice"

extract_dict() {
    FILE=$1
    DICT=$2

    tar -C $TMP -xjf $FILE

    chown -R root:root $TMP/$DICT
    chmod 0644 $TMP/$DICT/data/*
    cp $TMP/$DICT/data/* $DEST/

    rm -rf $TMP/$DICT
}

download() {
    RESULT=$1
    DICT=$2
    FILE=$3

    if ! echo $RESULT | grep -Eq "(^|, )$DICT(, |\$)"; then
        return
    fi

    MIRRORS="dfn belnet switch puzzle mesh ovh heanet surfnet kent"
    MIRRORS=`perl -e 'push @r, splice(@ARGV, int(rand(@ARGV)), 1) while @ARGV; $,=" ", print @r' $MIRRORS`

    for MIRROR in $MIRRORS; do
        URL=http://$MIRROR.dl.sourceforge.net/sourceforge/bgoffice/$FILE.tar.bz2

        echo "Downloading $DICT from $MIRROR"

        if curl --connect-timeout 20 -o $TMP/$FILE.tar.bz2 $URL; then
            echo "Download complete."
            # check if the downloaded file is a bzip2-compressed tar
            # containing $FILE root dir with a data/ subdir
            if tar tjf $TMP/$FILE.tar.bz2 $FILE/data > /dev/null; then
                extract_dict $TMP/$FILE.tar.bz2 $FILE
            else
                echo "Error: downloaded file either is not a bzip2-compressed"
                echo "       tar arcive, or does not contain $FILE/data member"
                echo "*** File not extracted"
            fi
            break
        else
            echo "*** Failed to download $URL"
        fi
    done
}

D_BE="Bulgarian-English dual dictionary"
F_BE="bg-en_dual"
D_NW_DIAL="Dictionary of north-western dialect"
F_NW_DIAL="dialect"
D_POLYTECH="Polytechnical dictionary"
F_POLYTECH="polytechnical"
D_THES="Thesaurus"
F_THES="thesaurus"

if [ "$1" = "configure" ]; then
    db_get $PKG/dict-list
    LIST=$RET
    db_go || true

    db_fset $PKG/dict-list seen false
    db_stop || true

    TMP=`mktemp -d`
    trap "rm -rf $TMP" INT QUIT TERM
    download "$LIST" "$D_BE" $F_BE
    download "$LIST" "$D_NW_DIAL" $F_NW_DIAL
    download "$LIST" "$D_POLYTECH" $F_POLYTECH
    download "$LIST" "$D_THES" $F_THES
fi

#DEBHELPER#

exit 0
