#!/bin/sh
# Copyright (c) 2003-2005 Nuxeo SARL <http://nuxeo.com>
# Authors:
# M.-A. Darche <madarche@nuxeo.com>
# Julien Anguenot <ja@nuxeo.com>
# Encolpe Degoute <edegoute@nuxeo.com>
#
# $Id: make_pot 24314 2005-06-23 11:56:01Z madarche $

prog_name=make_pot
usage="Usage:
$prog_name [options]

  -h, --help
     print help message and exit.

  -v, --verbose
     print messages and debug information during file creation.
"

help() {
echo "
$usage
This script generates a custom.pot pot file, and another file by parsing the
ZPT's as well as generating en.po and fr.po files.

This script relies on the following software, so make sure they are installed on
your system and available for $prog_name:

  * the GNU Internationalization utilities,
    be sure to have the gettext package (or equivalent)

  * the OpenPTi18n Zope product

This script relies on the PRODUCTS_DIR variable to find the OpenPTi18n product.
So for this script to work, you need to set this variable.

Example:
$ cd /var/lib/zope2.7/instance/instance1/Products/MyProduct/i18n/
$ export PRODUCTS_DIR=/var/lib/zope2.7/instance/instance1/Products/
$ make_pot --verbose
"
}

if [ ! $Z_NAME ]; then
    ECHO=echo
    PWD=pwd
    RM=rm
    CAT=cat
    GREP=grep
    MV=mv
    CP=cp
else
    # Include the Nuxeo specific script inc.sh if it exists
    . inc.sh || exit -1
fi

# If the first argument is "-h" or "--help" display help.
if [[ $1 == "-h" ]] || [[ $1 == "--help" ]]; then
    help; exit 0
fi

# If the first argument is "-v" or "--verbose" display help.
if [[ $1 == "-v" ]] || [[ $1 == "--verbose" ]]; then
    verbose='True'
else
    verbose=''
fi

if [ -x $ZHOME/$Z_NAME/bin/python ]; then
    PYTHON_BIN=$ZHOME/$Z_NAME/bin/python
else
    PYTHON_BIN=`which python`
fi

# PRODUCTS_DIR is the only necessary variable that this script needs to get
if [ -z $PRODUCTS_DIR ]; then
    if [ -d $ZHOME/$Z_NAME/Products ]; then
        PRODUCTS_DIR=$ZHOME/$Z_NAME/Products
    else
        PRODUCTS_DIR=`$PWD`/../..
    fi
    if [ -n $verbose ]; then
        $ECHO "Computed PRODUCTS_DIR = $PRODUCTS_DIR"
    fi
fi

# Query the user for the name he'd like to give to the pot file.
askProductName() {
    $ECHO     "######################################################"
    $ECHO -ne "### Enter the name of the pot file for this product: "
    read POT_FILE_NAME
}

# Query the user to confirm the request because some files may be removed.
getUserConfirmation() {
    $ECHO "###################################################################"
    $ECHO "### The pot/po files already in the directory are going to be deleted."
    $ECHO -ne "### Are you sure you want to continue ? (yes/no) "
    read USER_SURE
    $ECHO
}

# Cleaning the old pot/pos files.
# make_pot is an new proper initizalization
cleanDirectory() {
    $ECHO "######################################################"
    $ECHO "### Cleaning the pot/po files already in the directory"
    $ECHO "######################################################"
    $ECHO
    $RM -rf .config.pot 2> /dev/null
    $RM -rf .blacklist_pot 2> /dev/null
    $RM -rf *.po 2>/dev/null
    $RM -rf *.pot 2> /dev/null
}

# Remove the lines within header just generated by
# the i18n_xgettext.py
# It's compulsory since it's not working if not done
# Not neccesarly anymore since the correction I did in
# the OpenPTi18n package.
# No neccesarly anymore.
removeLines() {
   touch tmp.pot
   $CAT default.pot | grep -v "(and DELETE this one)" > tmp.pot
   $CAT tmp.pot | grep -v "Delete this line" > default.pot
   $RM tmp.pot
}

# Test of the just generated files contain errors
# At the end for the user
test_generated_files() {
    # Testing of the pot file just generated
    $ECHO "#######################################"
    $ECHO "### Checking the validy of the pot file"
    $ECHO "#######################################"
    $ECHO
    msgconv $POT_FILE_NAME.pot >> /dev/null
    if [ $? -eq 1 ] ;then
        $ECHO "###########################################"
        $ECHO "### The $POT_FILE_NAME.pot file is not correct."
        $ECHO "###########################################"
        $ECHO
        $ECHO "### Verify the parsing of the pot file or the ZPTs i18n tags!"
        $ECHO
    else
        $ECHO "#########################"
        $ECHO "### $POT_FILE_NAME [ OK ]"
        $ECHO "#########################"
        $ECHO
    fi
}

###############
#    MAIN
###############


$ECHO "##############################################################"
$ECHO "### make_pot : Parsing the ZPT's and generating the pot file"
$ECHO "##############################################################"
$ECHO

$ECHO $PRODUCTS_DIR

if [ ! -d $PRODUCTS_DIR/OpenPTi18n ]; then
    $ECHO `ls $PRODUCTS_DIR`
    $ECHO "You must install OpenPTi18n in your Products directory"
    exit 1
else
    PARSER_SCRIPT=$PRODUCTS_DIR/OpenPTi18n/i18n_xgettext.py
    # Checking if we are in the i18n directory
    $ECHO `$PWD` | grep "i18n" >> /dev/null
    if [ $? -eq 1 ] ; then
        $ECHO "make_pot needs to be executed within the i18n directory"
        $ECHO "You can also use make_pot in debug mode as stated below: "
        $ECHO "make_pot <FILE|DIRECTORY>"
        $ECHO "No pot files are going to be created but you'll know"
        $ECHO "if some of the ZPT's are not correct"
        exit 1
    else
        $ECHO `$PWD` | grep "i18n/" >> /dev/null
        if [ $? -eq 0 ] ; then
            $ECHO "make_pot needs to be executed within the i18n directory"
            $ECHO "You can also use make_pot in debug mode as stated below: "
            $ECHO "make_pot <FILE|DIRECTORY>"
            $ECHO "No pot files are going to be created but you'll know"
            $ECHO "if some of the ZPT's are not correct"
            exit 1
        else
            getUserConfirmation
            if [ $USER_SURE != "yes" ]; then
                $ECHO "Aborted by user!"
                exit 1
            else
                cleanDirectory
                SKINS_PATH="../skins/"
                $ECHO "#################################"
                $ECHO "### make_pot: Generating pot file"
                $ECHO "#################################"
                $ECHO

                # Generating the default.pot file
                $PYTHON_BIN $PARSER_SCRIPT $SKINS_PATH >> /dev/null

                # Renaming by asking the user
                while [ !$POT_FILE_NAME ]; do
                    askProductName
                    if [ $POT_FILE_NAME ]; then
                       # Renaming

                        $MV `$PWD`/default.pot `$PWD`/$POT_FILE_NAME.pot
                        touch `$PWD`/.config.pot
                        touch `$PWD`/.blacklist_pot
                        $ECHO `$PWD`/$POT_FILE_NAME.pot > `$PWD`/.config.pot

                        # Creating the en.po and fr.po for dev
                        $ECHO
                        $ECHO "#######################################"
                        $ECHO "### make_pot: Generating en/fr po files"
                        $ECHO "#######################################"
                        $ECHO
                        $CP `$PWD`/$POT_FILE_NAME.pot `$PWD`/en.po
                        $CP `$PWD`/$POT_FILE_NAME.pot `$PWD`/fr.po

                        # Generating the custom.pot file
                        touch xx.fake
                        $PYTHON_BIN $PARSER_SCRIPT xx.fake 2> /dev/null
                        $RM xx.fake
                        removeLines
                        $MV default.pot custom.pot

                        test_generated_files
                        chmod a-w $POT_FILE_NAME.pot

                        $ECHO "######################################################"
                        $ECHO "### Edit now your po files to do the translations"
                        $ECHO
                        $ECHO "### See **update_pos** to merge changes after if needed"
                        $ECHO "### DON'T USE make_pot ANYMORE AFTER YOUR TRANSLATION"
                        $ECHO
                        $ECHO "### If the parsing is not ok it can be because of :"
                        $ECHO "### -> ZPTs i18N tags are not setup properly !"
                        $ECHO "### -> The parser from OpenPTi18n did some errors"
                        $ECHO "### In both situations change the files your self (ZPT)"
                        $ECHO "### and re-launch the application 'til you get"
                        $ECHO "### a correct parsing.then transalte"
                        $ECHO "######################################################"
                        exit 0
                    else
                        continue
                    fi
                done
            fi
        fi
    fi
fi
