#!/bin/zsh
# Kw’s Release Tools/Python Project Template
# Locale Generator
# Copyright © 2013-2018, Chris Warrick.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions, and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions, and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
#
# 3. Neither the name of the author of this software nor the names of
#    contributors to this software may be used to endorse or promote
#    products derived from this software without specific prior written
#    consent.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
. .pypt/config

case "$LOCALETYPE" in
    'gettext' | 'gettext-tx')
        version=$(cat setup.py | grep 'version=' | sed -e 's/.*version=.//g' -e 's/.,$//g')
        date=$(date '+%Y-%m-%d')
        datel=$(date '+%Y-%m-%d %H:%M%z')
        datep=$(date '+%Y%m%d')

        xgettext -c ./$PROJECTLC/**/*.py localeprovider.py -o ./messages.pot

        sed '1,+17d' ./messages.pot > ./messages.pot.tmp

        pot='# '$PROJECT' pot file.
# Copyright © 2018, Chris Warrick.
# This file is distributed under the same license as the '$PROJECT' package.
# Chris Warrick <chris@chriswarrick.com>, 2016.
#
msgid ""
msgstr ""
"Project-Id-Version: #version\\n"
"Report-Msgid-Bugs-To: Chris Warrick <chris@chriswarrick.com>\\n"
"POT-Creation-Date: #datel\\n"
"PO-Revision-Date: #datel\\n"
"Last-Translator: Chris Warrick <chris@chriswarrick.com>\\n"
"Language-Team: Chris Warrick <chris@chriswarrick.com>\\n"
"Language: en\\n"
"MIME-Version: 1.0\\n"
"Content-Type: text/plain; charset=UTF-8\\n"
"Content-Transfer-Encoding: 8bit\\n"'

        echo $pot > messages.pot
        cat ./messages.pot.tmp >> messages.pot
        rm ./messages.pot.tmp

        sed "s/#version/$version/g" messages.pot -i
        sed "s/#datel/$datel/g" messages.pot -i

        if [[ "$LOCALETYPE" == "gettext-tx" ]]; then
            tx push -s
            tx pull
        fi

        for i in ./locale/*; do
            language=$(basename $i)

            podir="./locale/$language/LC_MESSAGES"
            popath="./locale/$language/LC_MESSAGES/$PROJECTLC.po"
            sed 's/\"Project-Id-Version: .*/\"Project-Id-Version: '$version'\\n\"/' $popath -i
            msgmerge $popath messages.pot -o $popath
            fuzzy=$(cat $popath | grep -c '#, fuzzy')
            empty=$(cat $popath | pcregrep -cM 'msgstr ""\n$')
            if [ $fuzzy != '0' ]; then
                echo "WARNING: $fuzzy fuzzy strings in language $language."
            fi

            if [ $empty != '0' ]; then
                echo "WARNING: $empty empty strings in language $language."
            fi

            msgfmt -o $podir/$PROJECTLC.mo $popath
        done
    ;;
    'pyqt4' | 'pyqt4-tx')
        pylupdate4 -verbose $PROJECTLC.pro
        if [[ "$LOCALETYPE" == "pyqt4-tx" ]]; then
            tx push -s
            tx pull
        fi
        for i in ./locale/*.ts; do
            # pylupdate4 is dumb and mangles encodings
            sed -i -e 's|filename="|filename="../|g' -e \
            's|&#xe2;&#x80;&#x9c;|“|g' -e 's|&#xe2;&#x80;&#x9d;|”|g' -e \
            's|&#xe2;&#x80;&#x98;|‘|g' -e 's|&#xe2;&#x80;&#x99;|’|g' -e \
            's|&#xe2;&#x80;&#xa6;|…|g' -e 's|&#xe2;&#x80;&#x93;|–|g' -e \
            's|&#xe2;&#x80;&#x94;|—|g' $i
            done
            lrelease $PROJECTLC.pro
            pyrcc4 -py2 $PROJECTLC.qrc -o $PROJECTLC/ui/resources2.py
            pyrcc4 -py3 $PROJECTLC.qrc -o $PROJECTLC/ui/resources3.py
    ;;
    'pyside' | 'pyside-tx')
        pyside-lupdate -verbose $PROJECTLC.pro
        if [[ "$LOCALETYPE" == "pyside-tx" ]]; then
            tx push -s
            tx pull
        fi
        for i in ./locale/*.ts; do
            # pyside may be dumb, too
            sed -i -e 's|filename="|filename="../|g' -e \
            's|&#xe2;&#x80;&#x9c;|“|g' -e 's|&#xe2;&#x80;&#x9d;|”|g' -e \
            's|&#xe2;&#x80;&#x98;|‘|g' -e 's|&#xe2;&#x80;&#x99;|’|g' -e \
            's|&#xe2;&#x80;&#xa6;|…|g' -e 's|&#xe2;&#x80;&#x93;|–|g' -e \
            's|&#xe2;&#x80;&#x94;|—|g' $i
            done
            lrelease $PROJECTLC.pro
            pyside-rcc -py2 $PROJECTLC.qrc -o $PROJECTLC/ui/resources2.py
            pyside-rcc -py3 $PROJECTLC.qrc -o $PROJECTLC/ui/resources3.py


    ;;
    'none') true ;;
    *) echo 'ERROR: unknown locale type.'; false
esac
