lang-build.sh 4.8 KB

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