lang-build.sh 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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 or lang_en_xx.txt
  8. #
  9. # Output files:
  10. # lang_xx.bin
  11. #
  12. # Temporary files:
  13. # lang_xx.tmp
  14. # lang_xx.dat
  15. #
  16. #awk code to format ui16 variables for dd
  17. awk_ui16='{ h=int($1/256); printf("\\x%02x\\x%02x\n", int($1-256*h), h); }'
  18. #startup message
  19. echo "lang-build.sh started" >&2
  20. #exiting function
  21. finish()
  22. {
  23. if [ $1 -eq 0 ]; then
  24. echo "lang-build.sh finished with success" >&2
  25. else
  26. echo "lang-build.sh finished with errors!" >&2
  27. fi
  28. exit $1
  29. }
  30. #returns hexadecial data for lang code
  31. lang_code_hex_data()
  32. # $1 - language code ('en', 'cz'...)
  33. {
  34. case "$1" in
  35. *en*) echo '\x6e\x65' ;;
  36. *cz*) echo '\x73\x63' ;;
  37. *de*) echo '\x65\x64' ;;
  38. *es*) echo '\x73\x65' ;;
  39. *fr*) echo '\x72\x66' ;;
  40. *it*) echo '\x74\x69' ;;
  41. *nl*) echo '\x6c\x6e' ;;
  42. *pl*) echo '\x6c\x70' ;;
  43. esac
  44. echo '??'
  45. }
  46. write_header()
  47. # $1 - lang
  48. # $2 - size
  49. # $3 - count
  50. # $4 - checksum
  51. # $5 - signature
  52. {
  53. /bin/echo -n -e "\xa5\x5a\xb4\x4b" |\
  54. dd of=lang_$1.bin bs=1 count=4 seek=0 conv=notrunc 2>/dev/null
  55. /bin/echo -n -e $(echo -n "$(($2))" | awk "$awk_ui16") |\
  56. dd of=lang_$1.bin bs=1 count=2 seek=4 conv=notrunc 2>/dev/null
  57. /bin/echo -n -e $(echo -n "$(($3))" | awk "$awk_ui16") |\
  58. dd of=lang_$1.bin bs=1 count=2 seek=6 conv=notrunc 2>/dev/null
  59. /bin/echo -n -e $(echo -n "$(($4))" | awk "$awk_ui16") |\
  60. dd of=lang_$1.bin bs=1 count=2 seek=8 conv=notrunc 2>/dev/null
  61. /bin/echo -n -e "$(lang_code_hex_data $1)" |\
  62. dd of=lang_$1.bin bs=1 count=2 seek=10 conv=notrunc 2>/dev/null
  63. sig_h=$(($5 / 65536))
  64. /bin/echo -n -e $(echo -n "$sig_h" | awk "$awk_ui16") |\
  65. dd of=lang_$1.bin bs=1 count=2 seek=14 conv=notrunc 2>/dev/null
  66. sig_l=$(($5 - $sig_h * 65536))
  67. /bin/echo -n -e $(echo -n "$sig_l" | awk "$awk_ui16") |\
  68. dd of=lang_$1.bin bs=1 count=2 seek=12 conv=notrunc 2>/dev/null
  69. }
  70. generate_binary()
  71. # $1 - language code ('en', 'cz'...)
  72. {
  73. echo "lang="$1 >&2
  74. #remove output and temporary files
  75. rm -f lang_$1.bin
  76. rm -f lang_$1.tmp
  77. rm -f lang_$1.dat
  78. LNG=$1
  79. #check lang dictionary
  80. /usr/bin/env python lang-check.py $1 --no-warning
  81. #create lang_xx.tmp - different processing for 'en' language
  82. if [ "$1" = "en" ]; then
  83. #remove comments and empty lines
  84. cat lang_en.txt | sed '/^$/d;/^#/d'
  85. else
  86. #remove comments and empty lines, print lines with translated text only
  87. cat lang_en_$1.txt | sed '/^$/d;/^#/d' | sed -n 'n;p'
  88. fi | sed 's/^\"\\x00\"$/\"\"/' > lang_$1.tmp
  89. #create lang_xx.dat (binary text data file)
  90. # cat lang_$1.tmp | sed 's/^\"/\/bin\/echo -e \"/;s/"$/\\x00\"/' > lang_$1.shx
  91. cat lang_$1.tmp | sed 's/^\"/\/bin\/echo -e -n \"/;s/"$/\\x00\"/' | sh >lang_$1.dat
  92. #calculate number of strings
  93. count=$(grep -c '^"' lang_$1.tmp)
  94. echo "count="$count >&2
  95. #calculate text data offset
  96. offs=$((16 + 2 * $count))
  97. echo "offs="$offs >&2
  98. #calculate text data size
  99. size=$(($offs + $(wc -c lang_$1.dat | cut -f1 -d' ')))
  100. echo "size="$size >&2
  101. #write header with empty signature and checksum
  102. write_header $1 $size $count 0x0000 0x00000000
  103. #write offset table
  104. offs_hex=$(cat lang_$1.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("\\x%02x\\x%02x",int(o-256*h), h); o+=(length($0)+1); }')
  107. /bin/echo -n -e "$offs_hex" | dd of=./lang_$1.bin bs=1 seek=16 conv=notrunc 2>/dev/null
  108. #write binary text data
  109. dd if=./lang_$1.dat of=./lang_$1.bin bs=1 seek=$offs conv=notrunc 2>/dev/null
  110. #write signature
  111. if [ "$1" != "en" ]; then
  112. dd if=lang_en.bin of=lang_$1.bin bs=1 count=4 skip=6 seek=12 conv=notrunc 2>/dev/null
  113. fi
  114. #calculate and update checksum
  115. chsum=$(cat lang_$1.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); }')
  116. /bin/echo -n -e $(echo -n $((0x$chsum)) | awk "$awk_ui16") |\
  117. dd of=lang_$1.bin bs=1 count=2 seek=8 conv=notrunc 2>/dev/null
  118. #remove temporary files
  119. # rm -f lang_$1.tmp
  120. # rm -f lang_$1.dat
  121. }
  122. if [ -z "$1" ]; then set 'all'; fi
  123. if [ "$1" = "all" ]; then
  124. generate_binary 'en'
  125. generate_binary 'cz'
  126. generate_binary 'de'
  127. generate_binary 'es'
  128. generate_binary 'fr'
  129. generate_binary 'nl'
  130. generate_binary 'it'
  131. generate_binary 'pl'
  132. else
  133. generate_binary $1
  134. fi
  135. finish 0