lang-build.sh 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. esac
  43. echo '??'
  44. }
  45. write_header()
  46. # $1 - lang
  47. # $2 - size
  48. # $3 - count
  49. # $4 - checksum
  50. # $5 - signature
  51. {
  52. /bin/echo -n -e "\xa5\x5a\xb4\x4b" |\
  53. dd of=lang_$1.bin bs=1 count=4 seek=0 conv=notrunc 2>/dev/null
  54. /bin/echo -n -e $(echo -n "$(($2))" | awk "$awk_ui16") |\
  55. dd of=lang_$1.bin bs=1 count=2 seek=4 conv=notrunc 2>/dev/null
  56. /bin/echo -n -e $(echo -n "$(($3))" | awk "$awk_ui16") |\
  57. dd of=lang_$1.bin bs=1 count=2 seek=6 conv=notrunc 2>/dev/null
  58. /bin/echo -n -e $(echo -n "$(($4))" | awk "$awk_ui16") |\
  59. dd of=lang_$1.bin bs=1 count=2 seek=8 conv=notrunc 2>/dev/null
  60. /bin/echo -n -e "$(lang_code_hex_data $1)" |\
  61. dd of=lang_$1.bin bs=1 count=2 seek=10 conv=notrunc 2>/dev/null
  62. sig_h=$(($5 / 65536))
  63. /bin/echo -n -e $(echo -n "$sig_h" | awk "$awk_ui16") |\
  64. dd of=lang_$1.bin bs=1 count=2 seek=14 conv=notrunc 2>/dev/null
  65. sig_l=$(($5 - $sig_h * 65536))
  66. /bin/echo -n -e $(echo -n "$sig_l" | awk "$awk_ui16") |\
  67. dd of=lang_$1.bin bs=1 count=2 seek=12 conv=notrunc 2>/dev/null
  68. }
  69. generate_binary()
  70. # $1 - language code ('en', 'cz'...)
  71. {
  72. echo "lang="$1 >&2
  73. #remove output and temporary files
  74. rm -f lang_$1.bin
  75. rm -f lang_$1.tmp
  76. rm -f lang_$1.dat
  77. LNG=$1
  78. #create lang_xx.tmp - different processing for 'en' language
  79. if [ "$1" = "en" ]; then
  80. #remove comments and empty lines
  81. cat lang_en.txt | sed '/^$/d;/^#/d'
  82. else
  83. #remove comments and empty lines, print lines with translated text only
  84. cat lang_en_$1.txt | sed '/^$/d;/^#/d' | sed -n 'n;p'
  85. fi | sed 's/^\"\\x00\"$/\"\"/' > lang_$1.tmp
  86. #create lang_xx.dat (binary text data file)
  87. # cat lang_$1.tmp | sed 's/^\"/\/bin\/echo -e \"/;s/"$/\\x00\"/' > lang_$1.shx
  88. cat lang_$1.tmp | sed 's/^\"/\/bin\/echo -e -n \"/;s/"$/\\x00\"/' | sh >lang_$1.dat
  89. #calculate number of strings
  90. count=$(grep -c '^"' lang_$1.tmp)
  91. echo "count="$count >&2
  92. #calculate text data offset
  93. offs=$((16 + 2 * $count))
  94. echo "offs="$offs >&2
  95. #calculate text data size
  96. size=$(($offs + $(wc -c lang_$1.dat | cut -f1 -d' ')))
  97. echo "size="$size >&2
  98. #write header with empty signature and checksum
  99. write_header $1 $size $count 0x0000 0x00000000
  100. #write offset table
  101. offs_hex=$(cat lang_$1.tmp | sed 's/^\"//;s/\"$//' |\
  102. sed 's/\\x[0-9a-f][0-9a-f]/\./g;s/\\[0-7][0-7][0-7]/\./g;s/\ /\./g' |\
  103. awk 'BEGIN { o='$offs';} { h=int(o/256); printf("\\x%02x\\x%02x",int(o-256*h), h); o+=(length($0)+1); }')
  104. /bin/echo -n -e "$offs_hex" | dd of=./lang_$1.bin bs=1 seek=16 conv=notrunc 2>/dev/null
  105. #write binary text data
  106. dd if=./lang_$1.dat of=./lang_$1.bin bs=1 seek=$offs conv=notrunc 2>/dev/null
  107. #write signature
  108. if [ "$1" != "en" ]; then
  109. dd if=lang_en.bin of=lang_$1.bin bs=1 count=4 skip=6 seek=12 conv=notrunc 2>/dev/null
  110. fi
  111. #calculate and update checksum
  112. 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); }')
  113. /bin/echo -n -e $(echo -n $((0x$chsum)) | awk "$awk_ui16") |\
  114. dd of=lang_$1.bin bs=1 count=2 seek=8 conv=notrunc 2>/dev/null
  115. #remove temporary files
  116. # rm -f lang_$1.tmp
  117. # rm -f lang_$1.dat
  118. }
  119. if [ -z "$1" ]; then set 'all'; fi
  120. if [ "$1" = "all" ]; then
  121. generate_binary 'en'
  122. generate_binary 'cz'
  123. generate_binary 'de'
  124. generate_binary 'es'
  125. generate_binary 'fr'
  126. generate_binary 'it'
  127. generate_binary 'pl'
  128. else
  129. generate_binary $1
  130. fi
  131. finish 0