#!/bin/bash

cp -f kindle_loc.sqlite kindle_loc.sqlite.old
cp -f kindle_loc.sqlite.base kindle_loc.sqlite

cd translation_3.4

VER="3.4"
cd translation_jar
find . -name '*.class' -exec sh -c "awk 'BEGIN {FS="'"'"\\t|\\r"'"'"} { print "'"'"{}\t"'"'" \$1 "'"'"\t"'"'" \$2}' < {}" \; | \
    sed "s/'/''/g" | \
    (
	printf -v IFS "\t"
	while read file src tran
	do
		[ -n "$tran" ] || continue
	    file=${file%.class}
	    file=$file.translation
	    echo "insert into trans values ('$src','$tran', '$file', '$VER');" | \
		sqlite3 ../../kindle_loc.sqlite || \
		    echo "             === insert into trans values ('$src','$tran', '$file', '$VER');"
	done
    )

find . -name '*.properties' -exec sh -c "native2ascii -reverse {} {}.native; awk 'BEGIN {FS="'"'"([[:space:]]*=[[:space:]]*)|\\r"'"'"} { print "'"'"{}\t"'"'" \$1 "'"'"\t"'"'" \$2}' < {}.native; rm -f {}.native" \; | \
    sed "s/'/''/g" | \
    (
	printf -v IFS "\t"
	while read file src tran
	do
	    [ -z "$src" ] && continue
	    [ -z "$tran" ] && continue
	    echo "insert into trans values ('$src','$tran', '$file', '$VER');" | \
		sqlite3 ../../kindle_loc.sqlite || \
		    echo "             === insert into trans values ('$src','$tran', '$file', '$VER');"
	done
    )
