#!/usr/bin/env bash
#
# usage: potgen [accepts no arguments]
# 
# generates ${PROJECT_ROOT}/po/shotwell/shotwell.pot, a POT file containing
# all translatable strings in the core Shotwell source tree, including
# translatable strings from unitized source files, ununitized source files,
# core plugin source files, and Glade XML files. Also generates
# ${PROJECT_ROOT}/po/shotwell-extras/shotwell-extras.pot, POT file
# containing all translatable strings in the Shotwell Extras Plugin Pack.
#
# note: must be run from the main project directory

CORE_PROGRAM_SRCS=src
CORE_PLUGIN_COMMON_SRCS=plugins/common
CORE_PUBLISHING_PLUGIN_SRCS=plugins/shotwell-publishing
CORE_DATA_IMPORTS_PLUGIN_SRCS=plugins/shotwell-data-imports
CORE_TRANSITIONS_PLUGIN_SRCS=plugins/shotwell-transitions

EXTRAS_PUBLISHING_PLUGIN_SRCS=plugins/shotwell-publishing-extras

CORE_PROGRAM_RSRCS=ui

EXTRAS_PUBLISHING_RSRCS=plugins/shotwell-publishing-extras

CORE_POT_FILE=po/shotwell-core/shotwell.pot
EXTRAS_POT_FILE=po/shotwell-extras/shotwell-extras.pot

#
# Ensure that all the directories we need to do our work are present -- this is essentially
# a sanity check to make sure that we are running in the root directory of a current
# Shotwell distribution
#
if [ ! "$#" == "0" ]
then
    echo "potgen: error: usage: potgen [accepts no arguments]"
    exit 127
fi

if [ ! -d "$CORE_PROGRAM_SRCS" ]
then
    echo "potgen: error: core program source directory ($CORE_PROGRAM_SRCS/) not present."
    exit 127
fi

if [ ! -d "$CORE_PLUGIN_COMMON_SRCS" ]
then
    echo "potgen: error: core plugins common directory ($CORE_PLUGIN_COMMON_SRCS/) not present."
    exit 127
fi

if [ ! -d "$CORE_PUBLISHING_PLUGIN_SRCS" ]
then
    echo "potgen: error: core publishing plugins directory ($CORE_PUBLISHING_PLUGIN_SRCS/) not present."
    exit 127
fi

if [ ! -d "$CORE_TRANSITIONS_PLUGIN_SRCS" ]
then
    echo "potgen: error: core transitions plugins directory ($CORE_TRANSITIONS_PLUGIN_SRCS/) not present."
    exit 127
fi

if [ ! -d "$EXTRAS_PUBLISHING_PLUGIN_SRCS" ]
then
    echo "potgen: error: publishing extras source directory ($EXTRAS_PUBLISHING_PLUGIN_SRCS/) not present."
    exit 127
fi

if [ ! -d "$CORE_PROGRAM_RSRCS" ]
then
    echo "potgen: error: core program resource directory ($CORE_PROGRAM_RSRCS/) not present."
    exit 127
fi

if [ ! -d "$EXTRAS_PUBLISHING_RSRCS" ]
then
    echo "potgen: error: publishing extras resource directory ($EXTRAS_PUBLISHING_RSRCS/) not present."
    exit 127
fi

#
# Extract translatable strings from all core source files (both core program and core
# plugins and write them out to ./core-sources.pot
#
find "./$CORE_PROGRAM_SRCS/" -name "*.vala" > pot_core_sources.temp
find "./$CORE_PLUGIN_COMMON_SRCS/" -name "*.vala" >> pot_core_sources.temp
find "./$CORE_PUBLISHING_PLUGIN_SRCS/" -name "*.vala" >> pot_core_sources.temp
find "./$CORE_TRANSITIONS_PLUGIN_SRCS/" -name "*.vala" >> pot_core_sources.temp
find "./$CORE_DATA_IMPORTS_PLUGIN_SRCS/" -name "*.vala" >> pot_core_sources.temp
xgettext -o core-sources.pot --language=C --keyword=_ --escape --files-from="pot_core_sources.temp"
rm pot_core_sources.temp

#
# Extract translatable strings from core program Glade XML files
#
xgettext -o core-glade.pot --language=Glade --omit-header $CORE_PROGRAM_RSRCS/*.glade $CORE_PUBLISHING_PLUGIN_SRCS/*.glade

#
# Merge the core program source and core program Glade POT files together
#
msgcat -o $CORE_POT_FILE --use-first core-sources.pot core-glade.pot

#
# Clean up the core program temp files
#
rm core-sources.pot core-glade.pot

#
# Extract translatable strings from the plugin extras source files and write them
# out to ./extras-sources.pot
#
find "./$EXTRAS_PUBLISHING_PLUGIN_SRCS/" -name "*.vala" > extras_sources.temp
xgettext -o extras-sources.pot --language=C --keyword='_t' --keyword='_tn:1,2' \
    --escape --files-from="extras_sources.temp"
rm extras_sources.temp

#
# Extract translatable strings from the plugin extras Glade XML files
#
xgettext -o extras-glade.pot --language=Glade --omit-header $EXTRAS_PUBLISHING_RSRCS/*.glade

#
# Merge the plugin extras source and Glade POT files together
#
msgcat -o $EXTRAS_POT_FILE --use-first extras-sources.pot extras-glade.pot

#
# Clean up the plugin extras temp files
#
rm extras-sources.pot extras-glade.pot

