#!/bin/bash
POHEADER='
# Beryl-Manager intl file.
# Copyright (C) 2006
# This file is distributed under the same license as the beryl-manager package.
# cyberorg , 2006.
# Steveway (DE translation)
msgid ""
msgstr ""
"Project-Id-Version: 0.1\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2006-09-25 22:07+0530\n"
"PO-Revision-Date: 2006-09-25 17:15+0530\n"
"Last-Translator: Melvil/Steveway\n"
"Language-Team: beryl-project \n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
'
MYIFS='
'
addmsg()
{
    OSTR="`echo $1| sed -e 's/"/\\\\"/g'`"
    OSTR2="`echo $OSTR | sed -e 's/\\\\/\\\\\\\/g'`"
    if cat $OFILE | grep "^msgid \"$OSTR2\"" > /dev/null
    then
        echo -n "D" # duplicate value
    else
        echo "msgid \"$OSTR\"" >> $OFILE
        echo 'msgstr ""' >> $OFILE
        echo >> $OFILE
    fi
}
makepo()
{
    echo -n Making pofile section for `basename $1`
    SAVEIFS=$IFS
    IFS="$MYIFS"
    for each in `cat $1 | egrep '^[nl]_' | cut -d '=' -f 2 | sed -e 's/"/\\"/g'`
    do
        echo -n '.'
        IFS=$SAVEIFS
        addmsg "$each"
    done
    IFS="$MYIFS"
    for each in `cat $1 | egrep '^s_'`
    do
        echo -n '.'
        IFS="$SAVEIFS"
        #find if it is a =string, or =list
        oname="`echo $each | cut -d '_' -f 2- | cut -d '=' -f 1`"
        otype="`cat $1 | grep "o_$oname=" | cut -d '=' -f 2`"
        if [ "$otype" = "string" ]
        then
            echval="`echo $each | cut -d '=' -f 2`"
            IFS=";"
            for estr in $echval
            do
                IFS="$SAVEIFS"
                addmsg "$estr"
            done
        else
            if [ "$otype" = "list" ]
            then
                ltype="`cat $1 | grep "t_$oname=" | cut -d '=' -f 2`"
                if [ "$ltype" = "string" ]
                then
                    echval="`echo $each | cut -d '=' -f 2`"
                    IFS=";"
                    for estr in $echval
                    do
                        IFS="$SAVEIFS"
                        addmsg "$estr"
                    done
                fi
            fi
        fi
    done
    IFS="$SAVEIFS"
    echo Done
}
if [ "$1" = "" ]
then
    echo "Usage: $0 output_file [settings_dir]"
    exit
fi
if [ "$2" = "" ]
then
    SETDIR="/usr/share/beryl/"
else
    SETDIR=$2
fi
OFILE=$1
if [ -f $OFILE ]
then
    echo Append mode, not writing header.
else
    echo "$POHEADER" > $OFILE
fi
for each in $SETDIR/*settings*
do
    makepo "$each"
done
