make_lang.sh 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. #!/bin/sh
  2. #
  3. # makelang.sh - multi-language support script
  4. # for generating lang_xx.bin (secondary language binary file)
  5. #
  6. # Input files:
  7. # lang_en.txt
  8. # lang_en_xx.txt
  9. #
  10. # Output files:
  11. # lang_en.tmp (temporary, will be removed when finished)
  12. # lang_en_xx.tmp ==||==
  13. # lang_en_xx.dif ==||==
  14. # lang_xx.txt
  15. #
  16. #
  17. # Selected language:
  18. LNG=$1
  19. if [ -z "$LNG" ]; then LNG='all'; fi
  20. #
  21. #
  22. finish()
  23. {
  24. if [ $1 -eq 0 ]; then
  25. if [ -e lang_en.tmp ]; then rm lang_en.tmp; fi
  26. if [ -e lang_en_$LNG.tmp ]; then rm lang_en_$LNG.tmp; fi
  27. if [ -e lang_en_$LNG.dif ]; then rm lang_en_$LNG.dif; fi
  28. fi
  29. # echo >&2
  30. if [ $1 -eq 0 ]; then
  31. echo "make_lang.sh finished with success" >&2
  32. else
  33. echo "make_lang.sh finished with errors!" >&2
  34. fi
  35. exit $1
  36. }
  37. make_lang()
  38. {
  39. LNG=$1
  40. echo "make_lang.sh started" >&2
  41. echo "selected language=$LNG" >&2
  42. #check if input files exists
  43. echo -n " checking input files..." >&2
  44. if [ ! -e lang_en.txt ]; then echo "NG! file lang_en.txt not found!" >&2; exit 1; fi
  45. if [ ! -e lang_en_$LNG.txt ]; then echo "NG! file lang_en_$LNG.txt not found!" >&2; exit 1; fi
  46. echo "OK" >&2
  47. #filter comment and empty lines from key and dictionary files, create temporary files
  48. echo -n " creating tmp files..." >&2
  49. cat lang_en.txt | sed "/^$/d;/^#/d" > lang_en.tmp
  50. cat lang_en_$LNG.txt | sed "/^$/d;/^#/d" > lang_en_$LNG.tmp
  51. echo "OK" >&2
  52. #cat lang_en_$LNG.tmp | sed 'n;d' >test1.txt
  53. #compare files using diff and check for differences
  54. echo -n " comparing tmp files..." >&2
  55. if ! cat lang_en_$LNG.tmp | sed 'n;d' | diff lang_en.tmp - > lang_en_$LNG.dif; then
  56. echo "NG!" >&2
  57. echo "Entries in lang_en_$LNG.txt are different from lang_en.txt!" >&2
  58. echo "please check lang_en_$LNG.dif" >&2
  59. finish 1
  60. fi
  61. echo "OK" >&2
  62. #generate lang_xx.txt (secondary language text data sorted by ids)
  63. echo -n " generating lang_$LNG.txt..." >&2
  64. cat lang_en_$LNG.tmp | sed '1~2d' | sed "s/^\"\\\\x00/\"/" > lang_$LNG.txt
  65. echo "OK" >&2
  66. #generate lang_xx.dat (secondary language text data in binary form)
  67. echo -n " generating lang_$LNG.dat..." >&2
  68. cat lang_$LNG.txt | sed "s/\\\\/\\\\\\\\/g" | while read s; do
  69. s=${s#\"}
  70. s=${s%\"}
  71. /bin/echo -e -n "$s\x00"
  72. done >lang_$LNG.dat
  73. echo "OK" >&2
  74. #calculate variables
  75. lt_magic='\xa5\x5a\xb4\x4b'
  76. lt_count=$(grep -c '^' lang_$LNG.txt)
  77. lt_data_size=$(wc -c lang_$LNG.dat | cut -f1 -d' ')
  78. lt_offs_size=$((2 * $lt_count))
  79. lt_size=$((16 + $lt_offs_size + $lt_data_size))
  80. lt_chsum=0
  81. lt_code='\xff\xff'
  82. lt_resv1='\xff\xff\xff\xff'
  83. case "$LNG" in
  84. *en*) lt_code='\x6e\x65' ;;
  85. *cz*) lt_code='\x73\x63' ;;
  86. *de*) lt_code='\x65\x64' ;;
  87. *es*) lt_code='\x73\x65' ;;
  88. *fr*) lt_code='\x71\x66' ;;
  89. *it*) lt_code='\x74\x69' ;;
  90. *pl*) lt_code='\x6c\x70' ;;
  91. esac
  92. #generate lang_xx.ofs (secondary language text data offset table)
  93. echo -n " generating lang_$LNG.ofs..." >&2
  94. cat lang_$LNG.txt | sed "s/\\\\x[0-9a-f][0-9a-f]/\./g;s/\\\\[0-7][0-7][0-7]/\./g" |\
  95. awk 'BEGIN { o='$((16 + $lt_offs_size))';} { printf("%d\n",o); o+=(length($0)-1); }' > lang_$LNG.ofs
  96. echo "OK" >&2
  97. #generate lang_xx.bin (secondary language result binary file)
  98. echo " generating lang_$LNG.bin:" >&2
  99. #create empty file
  100. dd if=/dev/zero of=lang_$LNG.bin bs=1 count=$lt_size 2>/dev/null
  101. #awk code to format ui16 variables for dd
  102. awk_ui16='{ h=int($1/256); printf("\\x%02x\\x%02x\n", int($1-256*h), h); }'
  103. #write data to binary file with dd
  104. echo -n " writing header (16 bytes)..." >&2
  105. /bin/echo -n -e "$lt_magic" |\
  106. dd of=lang_$LNG.bin bs=1 count=4 seek=0 conv=notrunc 2>/dev/null
  107. /bin/echo -n -e $(echo -n "$lt_size" | awk "$awk_ui16") |\
  108. dd of=lang_$LNG.bin bs=1 count=2 seek=4 conv=notrunc 2>/dev/null
  109. /bin/echo -n -e $(echo -n "$lt_count" | awk "$awk_ui16") |\
  110. dd of=lang_$LNG.bin bs=1 count=2 seek=6 conv=notrunc 2>/dev/null
  111. /bin/echo -n -e $(echo -n "$lt_chsum" | awk "$awk_ui16") |\
  112. dd of=lang_$LNG.bin bs=1 count=2 seek=8 conv=notrunc 2>/dev/null
  113. /bin/echo -n -e "$lt_code" |\
  114. dd of=lang_$LNG.bin bs=1 count=2 seek=10 conv=notrunc 2>/dev/null
  115. /bin/echo -n -e "$lt_resv1" |\
  116. dd of=lang_$LNG.bin bs=1 count=4 seek=12 conv=notrunc 2>/dev/null
  117. echo "OK" >&2
  118. echo -n " writing offset table ($lt_offs_size bytes)..." >&2
  119. /bin/echo -n -e $(cat lang_$LNG.ofs | awk "$awk_ui16" | tr -d '\n'; echo) |\
  120. dd of=./lang_$LNG.bin bs=1 count=$lt_offs_size seek=16 conv=notrunc 2>/dev/null
  121. echo "OK" >&2
  122. echo -n " writing text data ($lt_data_size bytes)..." >&2
  123. 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
  124. echo "OK" >&2
  125. #update signature
  126. echo -n " updating signature..." >&2
  127. dd if=lang_en.bin of=lang_$LNG.bin bs=1 count=4 skip=6 seek=12 conv=notrunc 2>/dev/null
  128. echo "OK" >&2
  129. #calculate and update checksum
  130. 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); }')
  131. /bin/echo -n -e $(echo -n $((0x$lt_chsum)) | awk "$awk_ui16") |\
  132. dd of=lang_$LNG.bin bs=1 count=2 seek=8 conv=notrunc 2>/dev/null
  133. echo " lang_table details:" >&2
  134. echo " lt_count = $lt_count" >&2
  135. echo " lt_size = $lt_size" >&2
  136. echo " lt_chsum = $lt_chsum" >&2
  137. }
  138. echo $LNG
  139. if [ "$LNG" = "all" ]; then
  140. ./lang-build.sh en
  141. make_lang cz
  142. make_lang de
  143. make_lang es
  144. make_lang fr
  145. make_lang it
  146. make_lang pl
  147. exit 0
  148. else
  149. make_lang $LNG
  150. fi
  151. finish 0