lang-build.sh 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. #!/bin/sh
  2. #
  3. # lang-build.sh - multi-language support script
  4. # generate lang_xx.bin (language binary file)
  5. #
  6. # Input files:
  7. # lang_en.txt
  8. # lang_en_xx.txt
  9. #
  10. # Output files:
  11. #
  12. #
  13. # Selected language:
  14. LNG=$1
  15. if [ -z "$LNG" ]; then LNG='cz'; fi
  16. #
  17. #awk code to format ui16 variables for dd
  18. awk_ui16='{ h=int($1/256); printf("\\x%02x\\x%02x\n", int($1-256*h), h); }'
  19. #exiting function
  20. finish()
  21. {
  22. if [ $1 -eq 0 ]; then
  23. if [ -e lang_en.tmp ]; then rm lang_en.tmp; fi
  24. if [ -e lang_en_$LNG.tmp ]; then rm lang_en_$LNG.tmp; fi
  25. if [ -e lang_en_$LNG.dif ]; then rm lang_en_$LNG.dif; fi
  26. fi
  27. # echo >&2
  28. if [ $1 -eq 0 ]; then
  29. echo "make_lang.sh finished with success" >&2
  30. else
  31. echo "make_lang.sh finished with errors!" >&2
  32. fi
  33. exit $1
  34. }
  35. #returns hexadecial data for lang code
  36. lang_code()
  37. # $1 - language code ('en', 'cz'...)
  38. {
  39. case "$1" in
  40. *en*) echo '\x6e\x65' ;;
  41. *cz*) echo '\x73\x63' ;;
  42. *de*) echo '\x65\x64' ;;
  43. *es*) echo '\x73\x65' ;;
  44. *fr*) echo '\x72\x66' ;;
  45. *it*) echo '\x74\x69' ;;
  46. *pl*) echo '\x6c\x70' ;;
  47. esac
  48. echo '??'
  49. }
  50. #
  51. write_header()
  52. # $1 - lang
  53. # $2 - size
  54. # $3 - count
  55. # $4 - checksum
  56. # $5 - signature
  57. {
  58. /bin/echo -n -e "\xa5\x5a\xb4\x4b" |\
  59. dd of=lang_$1.bin bs=1 count=4 seek=0 conv=notrunc 2>/dev/null
  60. /bin/echo -n -e $(echo -n "$(($2))" | awk "$awk_ui16") |\
  61. dd of=lang_$1.bin bs=1 count=2 seek=4 conv=notrunc 2>/dev/null
  62. /bin/echo -n -e $(echo -n "$(($3))" | awk "$awk_ui16") |\
  63. dd of=lang_$1.bin bs=1 count=2 seek=6 conv=notrunc 2>/dev/null
  64. /bin/echo -n -e $(echo -n "$(($4))" | awk "$awk_ui16") |\
  65. dd of=lang_$1.bin bs=1 count=2 seek=8 conv=notrunc 2>/dev/null
  66. /bin/echo -n -e "$(lang_code $1)" |\
  67. dd of=lang_$1.bin bs=1 count=2 seek=10 conv=notrunc 2>/dev/null
  68. sig_h=$(($5 / 65536))
  69. /bin/echo -n -e $(echo -n "$sig_h" | awk "$awk_ui16") |\
  70. dd of=lang_$1.bin bs=1 count=2 seek=14 conv=notrunc 2>/dev/null
  71. sig_l=$(($5 - $sig_h * 65536))
  72. /bin/echo -n -e $(echo -n "$sig_l" | awk "$awk_ui16") |\
  73. dd of=lang_$1.bin bs=1 count=2 seek=12 conv=notrunc 2>/dev/null
  74. }
  75. make_lang2()
  76. # $1 - lang ('en', 'cz'...)
  77. {
  78. rm -f lang_$LNG.tmp
  79. rm -f lang_$LNG.dat
  80. rm -f lang_$LNG.bin
  81. LNG=$1
  82. #create lang_xx.tmp - different processing for 'en' language
  83. if [ "$LNG" = "en" ]; then
  84. #remove comments and empty lines
  85. cat lang_en.txt | sed '/^$/d;/^#/d'
  86. else
  87. #remove comments and empty lines, print lines with translated text only
  88. cat lang_en_$LNG.txt | sed '/^$/d;/^#/d' | sed -n 'n;p'
  89. fi | sed 's/^\"\\x00\"$/\"\"/' > lang_$LNG.tmp
  90. #create lang_xx.dat (binary text data file)
  91. cat lang_$LNG.tmp | sed 's/^\"/printf \"/;s/"$/\\x00\"/' | sh >lang_$LNG.dat
  92. #calculate number of strings
  93. count=$(grep -c '^"' lang_$LNG.tmp)
  94. echo "count="$count
  95. #calculate text data offset
  96. offs=$((16 + 2 * $count))
  97. echo "offs="$offs
  98. #calculate text data size
  99. size=$(($offs + $(wc -c lang_$LNG.dat | cut -f1 -d' ')))
  100. echo "size="$size
  101. #write header with empty signature and checksum
  102. write_header $LNG $size $count 0x0000 0x00000000
  103. #write offset table
  104. cat lang_$LNG.tmp | sed 's/^\"//;s/\"$//' |\
  105. sed 's/\\x[0-9a-f][0-9a-f]/\./g;s/\\[0-7][0-7][0-7]/\./g;s/\ /\./g' |\
  106. awk 'BEGIN { o='$offs';} { h=int(o/256); printf("%c%c",int(o-256*h), h); o+=(length($0)+1); }' |\
  107. dd of=./lang_$LNG.bin bs=1 seek=16 conv=notrunc 2>/dev/null
  108. #write binary text data
  109. dd if=./lang_$LNG.dat of=./lang_$LNG.bin bs=1 seek=$offs conv=notrunc 2>/dev/null
  110. #calculate and update checksum
  111. chsum=$(cat lang_$LNG.bin | xxd | cut -c11-49 | tr ' ' "\n" | sed '/^$/d' | awk 'BEGIN { sum = 0; } { sum += strtonum("0x"$1); if (sum > 0xffff) sum -= 0x10000; } END { printf("%x\n", sum); }')
  112. /bin/echo -n -e $(echo -n $((0x$chsum)) | awk "$awk_ui16") |\
  113. dd of=lang_$LNG.bin bs=1 count=2 seek=8 conv=notrunc 2>/dev/null
  114. #remove temporary files
  115. rm -f lang_$LNG.tmp
  116. rm -f lang_$LNG.dat
  117. }
  118. make_lang2 $1
  119. exit
  120. make_lang()
  121. {
  122. LNG=$1
  123. echo "make_lang.sh started" >&2
  124. echo "selected language=$LNG" >&2
  125. #check if input files exists
  126. echo -n " checking input files..." >&2
  127. if [ ! -e lang_en.txt ]; then echo "NG! file lang_en.txt not found!" >&2; exit 1; fi
  128. if [ ! -e lang_en_$LNG.txt ]; then echo "NG! file lang_en_$LNG.txt not found!" >&2; exit 1; fi
  129. echo "OK" >&2
  130. #filter comment and empty lines from key and dictionary files, create temporary files
  131. echo -n " creating tmp files..." >&2
  132. cat lang_en.txt | sed "/^$/d;/^#/d" > lang_en.tmp
  133. cat lang_en_$LNG.txt | sed "/^$/d;/^#/d" > lang_en_$LNG.tmp
  134. echo "OK" >&2
  135. #cat lang_en_$LNG.tmp | sed 'n;d' >test1.txt
  136. #compare files using diff and check for differences
  137. echo -n " comparing tmp files..." >&2
  138. if ! cat lang_en_$LNG.tmp | sed 'n;d' | diff lang_en.tmp - > lang_en_$LNG.dif; then
  139. echo "NG!" >&2
  140. echo "Entries in lang_en_$LNG.txt are different from lang_en.txt!" >&2
  141. echo "please check lang_en_$LNG.dif" >&2
  142. finish 1
  143. fi
  144. echo "OK" >&2
  145. #generate lang_xx.txt (secondary language text data sorted by ids)
  146. echo -n " generating lang_$LNG.txt..." >&2
  147. cat lang_en_$LNG.tmp | sed '1~2d' | sed "s/^\"\\\\x00/\"/" > lang_$LNG.txt
  148. echo "OK" >&2
  149. #generate lang_xx.dat (secondary language text data in binary form)
  150. echo -n " generating lang_$LNG.dat..." >&2
  151. cat lang_$LNG.txt | sed "s/\\\\/\\\\\\\\/g" | while read s; do
  152. s=${s#\"}
  153. s=${s%\"}
  154. /bin/echo -e -n "$s\x00"
  155. done >lang_$LNG.dat
  156. echo "OK" >&2
  157. #calculate variables
  158. lt_magic='\xa5\x5a\xb4\x4b'
  159. lt_count=$(grep -c '^' lang_$LNG.txt)
  160. lt_data_size=$(wc -c lang_$LNG.dat | cut -f1 -d' ')
  161. lt_offs_size=$((2 * $lt_count))
  162. lt_size=$((16 + $lt_offs_size + $lt_data_size))
  163. lt_chsum=0
  164. lt_code='\xff\xff'
  165. lt_resv1='\xff\xff\xff\xff'
  166. case "$LNG" in
  167. *en*) lt_code='\x6e\x65' ;;
  168. *cz*) lt_code='\x73\x63' ;;
  169. *de*) lt_code='\x65\x64' ;;
  170. *es*) lt_code='\x73\x65' ;;
  171. *it*) lt_code='\x74\x69' ;;
  172. *pl*) lt_code='\x6c\x70' ;;
  173. esac
  174. #generate lang_xx.ofs (secondary language text data offset table)
  175. echo -n " generating lang_$LNG.ofs..." >&2
  176. cat lang_$LNG.txt | sed "s/\\\\x[0-9a-f][0-9a-f]/\./g;s/\\\\[0-7][0-7][0-7]/\./g" |\
  177. awk 'BEGIN { o='$((16 + $lt_offs_size))';} { printf("%d\n",o); o+=(length($0)-1); }' > lang_$LNG.ofs
  178. echo "OK" >&2
  179. #generate lang_xx.bin (secondary language result binary file)
  180. echo " generating lang_$LNG.bin:" >&2
  181. #create empty file
  182. dd if=/dev/zero of=lang_$LNG.bin bs=1 count=$lt_size 2>/dev/null
  183. #write data to binary file with dd
  184. echo -n " writing header (16 bytes)..." >&2
  185. /bin/echo -n -e "$lt_magic" |\
  186. dd of=lang_$LNG.bin bs=1 count=4 seek=0 conv=notrunc 2>/dev/null
  187. /bin/echo -n -e $(echo -n "$lt_size" | awk "$awk_ui16") |\
  188. dd of=lang_$LNG.bin bs=1 count=2 seek=4 conv=notrunc 2>/dev/null
  189. /bin/echo -n -e $(echo -n "$lt_count" | awk "$awk_ui16") |\
  190. dd of=lang_$LNG.bin bs=1 count=2 seek=6 conv=notrunc 2>/dev/null
  191. /bin/echo -n -e $(echo -n "$lt_chsum" | awk "$awk_ui16") |\
  192. dd of=lang_$LNG.bin bs=1 count=2 seek=8 conv=notrunc 2>/dev/null
  193. /bin/echo -n -e "$lt_code" |\
  194. dd of=lang_$LNG.bin bs=1 count=2 seek=10 conv=notrunc 2>/dev/null
  195. /bin/echo -n -e "$lt_resv1" |\
  196. dd of=lang_$LNG.bin bs=1 count=4 seek=12 conv=notrunc 2>/dev/null
  197. echo "OK" >&2
  198. echo -n " writing offset table ($lt_offs_size bytes)..." >&2
  199. /bin/echo -n -e $(cat lang_$LNG.ofs | awk "$awk_ui16" | tr -d '\n'; echo) |\
  200. dd of=./lang_$LNG.bin bs=1 count=$lt_offs_size seek=16 conv=notrunc 2>/dev/null
  201. echo "OK" >&2
  202. echo -n " writing text data ($lt_data_size bytes)..." >&2
  203. dd if=./lang_$LNG.dat of=./lang_$LNG.bin bs=1 count=$lt_data_size seek=$((16 + $lt_offs_size)) conv=notrunc 2>/dev/null
  204. echo "OK" >&2
  205. #calculate and update checksum
  206. lt_chsum=$(cat lang_$LNG.bin | xxd | cut -c11-49 | tr ' ' "\n" | sed '/^$/d' | awk 'BEGIN { sum = 0; } { sum += strtonum("0x"$1); if (sum > 0xffff) sum -= 0x10000; } END { printf("%x\n", sum); }')
  207. /bin/echo -n -e $(echo -n $((0x$lt_chsum)) | awk "$awk_ui16") |\
  208. dd of=lang_$LNG.bin bs=1 count=2 seek=8 conv=notrunc 2>/dev/null
  209. echo " lang_table details:" >&2
  210. echo " lt_count = $lt_count" >&2
  211. echo " lt_size = $lt_size" >&2
  212. echo " lt_chsum = $lt_chsum" >&2
  213. }
  214. echo $LNG
  215. if [ "$LNG" = "all" ]; then
  216. make_lang cz
  217. make_lang de
  218. make_lang es
  219. make_lang it
  220. make_lang pl
  221. exit 0
  222. else
  223. make_lang $LNG
  224. fi
  225. finish 0