#!/bin/bash

# Free Telco Dictionary
# Copyright © 2013 František Kučera (frantovo.cz)
# 
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.


# Script for generating deliverables:
# packed dictionary data in StarDict format


JAR="dist/dictionary-generator.jar"
GENERATOR_DIR="java/dictionary-generator"
WORK_DIR="free-telco-dictionary"

function package() {
    DELIVERY_DIR="../../delivery"
    rm -rf $DELIVERY_DIR/$WORK_DIR
    java -jar $JAR $1
    pushd $DELIVERY_DIR
    (echo -n "Mercurial: "; hg identify || echo "n/a"; echo -n "Generated: "; date --rfc-3339=seconds) > $WORK_DIR/version.txt
    tar czf $2.tar.gz $WORK_DIR
    popd
}

pushd `dirname $0`/$GENERATOR_DIR

hg status ../../data/dictionary.xml | grep -q "^M" && echo -e "\033[31mWARNING: building from data file which was not commited into Mercurial!\033[0m" || (echo -e "\033[32mINFO: building from versioned data file: `hg identify` \033[0m");

# build the generator if needed
if [ ! -f $JAR ]; then
    ant;
fi

# generate packages in multiple formats
package m free-telco-dictionary.text  # plain text
package h free-telco-dictionary.xhtml # rich text

popd

