#!/bin/bash

# This script adds at translated strings in the po files links to the corresponding Launchpad entry
# licenced under GPL by Carsten Gerlach

if ! [ $# -eq 1 ]; then
	echo "Give one po file as argument like ./update_po_files_with_LP_links.sh en.po"
	exit
fi

POFILE=$1

if [ -e $POFILE-backup ]; then
	cp $POFILE-backup $POFILE
fi
cp $POFILE $POFILE-backup

msgstr_line_numbers=(`grep -n msgstr $POFILE | cut -d":" -f 1`)
count_msgstr_lines=`grep -n msgstr $POFILE | wc -l`

for i in `seq $count_msgstr_lines`
do
	echo LP string $i of $count_msgstr_lines
	sed_line="sed -e '"${msgstr_line_numbers[$i-1]}","${msgstr_line_numbers[$i-1]}"s/msgstr \"/msgstr \"\\\\\\\\lplink{$((i-1))}/' $POFILE"
	echo $sed_line | bash > out.po
	mv out.po $POFILE
done

newglossary_line_numbers=(`grep 'name={' $POFILE -B1 -n | grep msgstr | cut -d"-" -f 1`)
count_newglossary_lines=`grep 'name={' $POFILE -B1 -n | grep msgstr | wc -l`
newglossary_names=(`grep 'name={' $POFILE -B1 | grep msgstr -A1 | grep 'name={' | cut -d"=" -f 2 | cut -d"," -f 1 | sed s/\"/}/g | sed s/\ //g | sed s/\\\\\\\\/\\\\\\\\\\\\\\\\/g | sed s/^{//g | sed s/}$//g | sed s/\'//g`)
lplinks=(`grep 'name={' $POFILE -B1 -n | grep msgstr | cut -d"{" -f 2 | cut -d"}" -f 1`)

for i in `seq $count_newglossary_lines`
do
	echo Move LP link in newglossary entry $i of $count_newglossary_lines
	sed_line="sed -e '"${newglossary_line_numbers[$i-1]}","${newglossary_line_numbers[$i-1]}"s/\\\\\\\\lplink{${lplinks[$i-1]}}//' $POFILE"
	echo $sed_line | bash > out.po
	mv out.po $POFILE

	sed_line="sed -e '"$((${newglossary_line_numbers[$i-1]}+1))","$((${newglossary_line_numbers[$i-1]}+1))"s/name={/name={\\\\\\\\lplink{${lplinks[$i-1]}}/' $POFILE"
	echo $sed_line | bash > out.po
	mv out.po $POFILE

	sed_line="sed -e '"$((${newglossary_line_numbers[$i-1]}+1))","$((${newglossary_line_numbers[$i-1]}+1))"s/name={/sort={${newglossary_names[$i-1]}},\ name={/' $POFILE"
	echo $sed_line | bash > out.po
	mv out.po $POFILE
done

index_line_numbers=(`grep "#. type: index" $POFILE -A5 -n | grep msgstr | cut -d"-" -f 1`)
count_index_lines=`grep "#. type: index" $POFILE -A5 -n | grep msgstr | wc -l`
index_entries=(`grep "#. type: index" $POFILE -A5 -n | grep msgstr | sed s/\ /#/g | sed s/\\\\\\\\\\\\\\\\lplink{[0-9]*}/\</ | cut -d"\"" -f 2 | sed s/"|("// | sed s/"|)"//`)
lplinks=(`grep "#. type: index" $POFILE -A5 -n | grep msgstr | cut -d"{" -f 2 | cut -d"}" -f 1`)

# Counter for different index entries
test1=0
test2=0
test3=0
test4=0
test5=0
test6=0
test7=0

for i in `seq $count_index_lines`
do
	echo Move LP link in index entry $i of $count_index_lines
	if [ ${index_entries[$i-1]} = "<" ]; then
		sed_line="sed -e '"${index_line_numbers[$i-1]}","${index_line_numbers[$i-1]}"s/\\\\\\\\lplink{${lplinks[$i-1]}}//' $POFILE"
		echo $sed_line | bash > out.po
		mv out.po $POFILE
		index_line_numbers[$i-1]=$((${index_line_numbers[$i-1]}+1))
		index_entries[$i-1]=\<`head -${index_line_numbers[$i-1]} $POFILE | tail -1 | cut -d"\"" -f 2`
	fi
	index_entries[$i-1]=`echo ${index_entries[$i-1]} | sed s/\<// | sed s/#/\ /g`

	# remove leading lplinks and trailing ranges |( |) in indices
	sed_line="sed -e '"${index_line_numbers[$i-1]}","${index_line_numbers[$i-1]}"s/\\\\\\\\lplink{${lplinks[$i-1]}}//' -e '"${index_line_numbers[$i-1]}","${index_line_numbers[$i-1]}"s/"\|[\(\)]"//g' $POFILE"
	echo $sed_line | bash > out.po
	mv out.po $POFILE

	status=0
	# 1. test for entries without @, ! and |
	if [ `echo ${index_entries[$i-1]} | grep -v '@' | grep -v '|' | grep -v '!' | wc -l` -eq 1 ]; then
		sed_line="sed -e '"${index_line_numbers[$i-1]}","${index_line_numbers[$i-1]}"s/\"\(.*\)\"/\"\1@\\\\\\\\lplink{${lplinks[$i-1]}}\1\"/' $POFILE"
		echo $sed_line | bash > out.po
		mv out.po $POFILE
		status=1
		test1=$(($test1+1))
	fi

	# 2. test for entries with only @
	if [ $status = 0 ]; then
		if [ `echo ${index_entries[$i-1]} | grep '@' | grep -v '|' | grep -v '!' | wc -l` -eq 1 ]; then
			sed_line="sed -e '"${index_line_numbers[$i-1]}","${index_line_numbers[$i-1]}"s/@/@\\\\\\\\lplink{${lplinks[$i-1]}}/' $POFILE"
			echo $sed_line | bash > out.po
			mv out.po $POFILE
			status=1
			test2=$(($test2+1))
		fi
	fi

	# 3. test for entries with only !
	if [ $status = 0 ]; then
		if [ `echo ${index_entries[$i-1]} | grep -v '@' | grep -v '|' | grep '!' | wc -l` -eq 1 ]; then
			sed_line="sed -e '"${index_line_numbers[$i-1]}","${index_line_numbers[$i-1]}"s/!\(.[^!]*\)\"/!\1@\\\\\\\\lplink{${lplinks[$i-1]}}\1\"/' $POFILE"
			echo $sed_line | bash > out.po
			mv out.po $POFILE
			status=1
			test3=$(($test3+1))
		fi
	fi

	# 4. test for entries with only | (see and seealso)
	if [ $status = 0 ]; then
		if [ `echo ${index_entries[$i-1]} | grep -v '@' | grep '|' | grep -v '!' | wc -l` -eq 1 ]; then
			sed_line="sed -e '"${index_line_numbers[$i-1]}","${index_line_numbers[$i-1]}"s/\"\(.*\)|/\"\1@\\\\\\\\lplink{${lplinks[$i-1]}}\1|/' $POFILE"
			echo $sed_line | bash > out.po
			mv out.po $POFILE
			status=1
			test4=$(($test4+1))
		fi
	fi

	# 5. test for entries with only @ and !
	if [ $status = 0 ]; then
		if [ `echo ${index_entries[$i-1]} | grep '@' | grep -v '|' | grep '!' | wc -l` -eq 1 ]; then
			sed_line="sed -e '"${index_line_numbers[$i-1]}","${index_line_numbers[$i-1]}"s/@\(.[^!]*\)\"/@\\\\\\\\lplink{${lplinks[$i-1]}}\1\"/' -e '"${index_line_numbers[$i-1]}","${index_line_numbers[$i-1]}"s/!\(.[^!][^@]*\)\"/!\1@\\\\\\\\lplink{${lplinks[$i-1]}}\1\"/' $POFILE"
			echo $sed_line | bash > out.po
			mv out.po $POFILE
			status=1
			test5=$(($test5+1))
		fi
	fi

	# 6. test for entries with only @ and |see...
	if [ $status = 0 ]; then
		if [ `echo ${index_entries[$i-1]} | grep '@' | grep '|' | grep -v '!' | wc -l` -eq 1 ]; then
			sed_line="sed -e '"${index_line_numbers[$i-1]}","${index_line_numbers[$i-1]}"s/@\(.[^@]*\)|/@\\\\\\\\lplink{${lplinks[$i-1]}}\1|/' $POFILE"
			echo $sed_line | bash > out.po
			mv out.po $POFILE
			status=1
			test6=$(($test6+1))
		fi
	fi

	# 7. test for entries with only ! and |see...
	if [ $status = 0 ]; then
		if [ `echo ${index_entries[$i-1]} | grep -v '@' | grep '|' | grep '!' | wc -l` -eq 1 ]; then
			sed_line="sed -e '"${index_line_numbers[$i-1]}","${index_line_numbers[$i-1]}"s/!\(.[^!]*\)|/!\1@\\\\\\\\lplink{${lplinks[$i-1]}}\1|/' $POFILE"
			echo $sed_line | bash > out.po
			mv out.po $POFILE
			status=1
			test7=$(($test7+1))
		fi
	fi
done

echo "Test for index entries without '@', '!' and '|': " $test1
echo "Test for index entries with only '@':            " $test2
echo "Test for index entries with only '!':            " $test3
echo "Test for index entries with only '|see...':      " $test4
echo "Test for index entries with only '@ and !':      " $test5
echo "Test for index entries with only '@ and |see...':" $test6
echo "Test for index entries with only '! and |see...':" $test7
echo "Sum of all index tests:                          " $(($test1+$test2+$test3+$test4+$test5+$test6+$test7))
echo "All index enties in $POFILE                        " $count_index_lines
echo "If the sum of all index tests is less then all index entries, search for syntax errors like '|@'."

