#!/bin/bash
# Copyright (c) 2008 Kanru Chen <koster@debian.org.tw>
# 
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
# 
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
# 
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.

getpot () {
    if [ "$PACKAGE" = "" ]; then
        PACKAGE=$1
    fi
    if [ "${PACKAGE:0:3}" = "lib" ]; then
        PREFIX="${PACKAGE:0:4}"
    else
        PREFIX="${PACKAGE:0:1}"
    fi
    BASEURL="http://i18n.debian.net/material/po/unstable"

    for section in main contrib non-free;
    do
        FILE=$(curl -s "$BASEURL/$section/$PREFIX/$PACKAGE/debian/po/"|egrep -o ">$PACKAGE.*\.pot\.gz")
        if [ "$?" = 0 ]; then
            if [ ! -d $PACKAGE ]; then
                mkdir $PACKAGE
            fi
            curl -s "$BASEURL/$section/$PREFIX/$PACKAGE/debian/po/${FILE:1}"|zcat > $PACKAGE/zh_TW.po
            echo "Now you can edit $PACKAGE/zh_TW.po"
            return
        fi
    done
    echo "Failed to get $PACKAGE/zh_TW.po"
}

usage () {
    echo "Usage: $0 {check|display|get|send} package"
}

PACKAGE=$2

TEMPLATE=$(cat <<TEMP
Hi,

Please include attached translation zh_TW.po to the package.

Kind regards
 $DEBFULLNAME
TEMP)

if [ "$PACKAGE" = "" ]; then
    usage
    exit 0
fi

case "$1" in
    check)
        msgfmt -c -v -o /dev/null $PACKAGE/zh_TW.po
    ;;
    display)
        podebconf-display-po -fdialog $PACKAGE/zh_TW.po
    ;;
    send)
        if [ "$DEBFULLNAME" = "" ]; then
            echo "Must set DEBFULLNAME"
            exit 1
        fi

        SUBJECT="[INTL:zh_TW] Traditional Chinese debconf templates translation update"

        reportbug -bcgxOI --attach=$PACKAGE/zh_TW.po --body="$TEMPLATE" --subject="$SUBJECT" --severity=wishlist --tag=patch --tag=l10n --mode=novice $PACKAGE

        mv $PACKAGE $PACKAGE.done
    ;;
    get)
        getpot $PACKAGE
    ;;
    *)
        usage
    ;;
esac
