lang-build.sh 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. *pl*) echo '\x6c\x70' ;;
  42. #Community language support
  43. #Dutch
  44. *nl*) echo '\x6c\x6e' ;;
  45. #Use the 2 lines below as a template and replace 'qr' and `\x71\x72`
  46. ##New language
  47. # *qr*) echo '\x71\x72' ;;
  48. esac
  49. echo '??'
  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_hex_data $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. generate_binary()
  76. # $1 - language code ('en', 'cz'...)
  77. {
  78. echo "lang="$1 >&2
  79. #remove output and temporary files
  80. rm -f lang_$1.bin
  81. rm -f lang_$1.tmp
  82. rm -f lang_$1.dat
  83. LNG=$1
  84. #check lang dictionary
  85. ./lang-check.py $1 --no-warning
  86. #create lang_xx.tmp - different processing for 'en' language
  87. if [ "$1" = "en" ]; then
  88. #remove comments and empty lines
  89. cat lang_en.txt | sed '/^$/d;/^#/d'
  90. else
  91. #remove comments and empty lines, print lines with translated text only
  92. cat lang_en_$1.txt | sed '/^$/d;/^#/d' | sed -n 'n;p'
  93. fi | sed 's/^\"\\x00\"$/\"\"/' > lang_$1.tmp
  94. #create lang_xx.dat (binary text data file)
  95. # cat lang_$1.tmp | sed 's/^\"/\/bin\/echo -e \"/;s/"$/\\x00\"/' > lang_$1.shx
  96. cat lang_$1.tmp | sed 's/^\"/\/bin\/echo -e -n \"/;s/"$/\\x00\"/' | sh >lang_$1.dat
  97. #calculate number of strings
  98. count=$(grep -c '^"' lang_$1.tmp)
  99. echo "count="$count >&2
  100. #calculate text data offset
  101. offs=$((16 + 2 * $count))
  102. echo "offs="$offs >&2
  103. #calculate text data size
  104. size=$(($offs + $(wc -c lang_$1.dat | cut -f1 -d' ')))
  105. echo "size="$size >&2
  106. #write header with empty signature and checksum
  107. write_header $1 $size $count 0x0000 0x00000000
  108. #write offset table
  109. offs_hex=$(cat lang_$1.tmp | sed 's/^\"//;s/\"$//' |\
  110. sed 's/\\x[0-9a-f][0-9a-f]/\./g;s/\\[0-7][0-7][0-7]/\./g;s/\ /\./g' |\
  111. awk 'BEGIN { o='$offs';} { h=int(o/256); printf("\\x%02x\\x%02x",int(o-256*h), h); o+=(length($0)+1); }')
  112. /bin/echo -n -e "$offs_hex" | dd of=./lang_$1.bin bs=1 seek=16 conv=notrunc 2>/dev/null
  113. #write binary text data
  114. dd if=./lang_$1.dat of=./lang_$1.bin bs=1 seek=$offs conv=notrunc 2>/dev/null
  115. #write signature
  116. if [ "$1" != "en" ]; then
  117. dd if=lang_en.bin of=lang_$1.bin bs=1 count=4 skip=6 seek=12 conv=notrunc 2>/dev/null
  118. fi
  119. #calculate and update checksum
  120. 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); }')
  121. /bin/echo -n -e $(echo -n $((0x$chsum)) | awk "$awk_ui16") |\
  122. dd of=lang_$1.bin bs=1 count=2 seek=8 conv=notrunc 2>/dev/null
  123. #remove temporary files
  124. # rm -f lang_$1.tmp
  125. # rm -f lang_$1.dat
  126. }
  127. if [ -z "$1" ]; then set 'all'; fi
  128. if [ "$1" = "all" ]; then
  129. generate_binary 'en'
  130. generate_binary 'cz'
  131. generate_binary 'de'
  132. generate_binary 'es'
  133. generate_binary 'fr'
  134. generate_binary 'it'
  135. generate_binary 'pl'
  136. #DO NOT add Community languages here !!!
  137. else
  138. generate_binary $1
  139. fi
  140. finish 0