lang-build.sh 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. #Luxembourgish
  61. *lb*) echo '\x62\x6C' ;;
  62. #Croatian
  63. *hr*) echo '\x72\x68' ;;
  64. #Use the 2 lines below as a template and replace 'qr' and `\x71\x72`
  65. ##New language
  66. # *qr*) echo '\x71\x72' ;;
  67. esac
  68. echo '??'
  69. }
  70. write_header()
  71. # $1 - lang
  72. # $2 - size
  73. # $3 - count
  74. # $4 - checksum
  75. # $5 - signature
  76. {
  77. /bin/echo -n -e "\xa5\x5a\xb4\x4b" |\
  78. dd of=lang_$1.bin bs=1 count=4 seek=0 conv=notrunc 2>/dev/null
  79. /bin/echo -n -e $(echo -n "$(($2))" | awk "$awk_ui16") |\
  80. dd of=lang_$1.bin bs=1 count=2 seek=4 conv=notrunc 2>/dev/null
  81. /bin/echo -n -e $(echo -n "$(($3))" | awk "$awk_ui16") |\
  82. dd of=lang_$1.bin bs=1 count=2 seek=6 conv=notrunc 2>/dev/null
  83. /bin/echo -n -e $(echo -n "$(($4))" | awk "$awk_ui16") |\
  84. dd of=lang_$1.bin bs=1 count=2 seek=8 conv=notrunc 2>/dev/null
  85. /bin/echo -n -e "$(lang_code_hex_data $1)" |\
  86. dd of=lang_$1.bin bs=1 count=2 seek=10 conv=notrunc 2>/dev/null
  87. sig_h=$(($5 / 65536))
  88. /bin/echo -n -e $(echo -n "$sig_h" | awk "$awk_ui16") |\
  89. dd of=lang_$1.bin bs=1 count=2 seek=14 conv=notrunc 2>/dev/null
  90. sig_l=$(($5 - $sig_h * 65536))
  91. /bin/echo -n -e $(echo -n "$sig_l" | awk "$awk_ui16") |\
  92. dd of=lang_$1.bin bs=1 count=2 seek=12 conv=notrunc 2>/dev/null
  93. }
  94. generate_binary()
  95. # $1 - language code ('en', 'cz'...)
  96. {
  97. echo "lang="$1 >&2
  98. #remove output and temporary files
  99. rm -f lang_$1.bin
  100. rm -f lang_$1.tmp
  101. rm -f lang_$1.dat
  102. LNG=$1
  103. #check lang dictionary
  104. ./lang-check.py $1 #--no-warning
  105. #create lang_xx.tmp - different processing for 'en' language
  106. if [ "$1" = "en" ]; then
  107. #remove comments and empty lines
  108. cat lang_en.txt | sed '/^$/d;/^#/d'
  109. else
  110. #remove comments and empty lines, print lines with translated text only
  111. cat lang_en_$1.txt | sed '/^$/d;/^#/d' | sed -n 'n;p'
  112. fi | sed 's/^\"\\x00\"$/\"\"/' > lang_$1.tmp
  113. #create lang_xx.dat (binary text data file)
  114. # cat lang_$1.tmp | sed 's/^\"/\/bin\/echo -e \"/;s/"$/\\x00\"/' > lang_$1.shx
  115. cat lang_$1.tmp | sed 's/^\"/\/bin\/echo -e -n \"/;s/"$/\\x00\"/' | sh >lang_$1.dat
  116. #calculate number of strings
  117. count=$(grep -c '^"' lang_$1.tmp)
  118. echo "count="$count >&2
  119. #calculate text data offset
  120. offs=$((16 + 2 * $count))
  121. echo "offs="$offs >&2
  122. #calculate text data size
  123. size=$(($offs + $(wc -c lang_$1.dat | cut -f1 -d' ')))
  124. echo "size="$size >&2
  125. #write header with empty signature and checksum
  126. write_header $1 $size $count 0x0000 0x00000000
  127. #write offset table
  128. offs_hex=$(cat lang_$1.tmp | sed 's/^\"//;s/\"$//' |\
  129. sed 's/\\x[0-9a-f][0-9a-f]/\./g;s/\\[0-7][0-7][0-7]/\./g;s/\ /\./g' |\
  130. awk 'BEGIN { o='$offs';} { h=int(o/256); printf("\\x%02x\\x%02x",int(o-256*h), h); o+=(length($0)+1); }')
  131. /bin/echo -n -e "$offs_hex" | dd of=./lang_$1.bin bs=1 seek=16 conv=notrunc 2>/dev/null
  132. #write binary text data
  133. dd if=./lang_$1.dat of=./lang_$1.bin bs=1 seek=$offs conv=notrunc 2>/dev/null
  134. #write signature
  135. if [ "$1" != "en" ]; then
  136. dd if=lang_en.bin of=lang_$1.bin bs=1 count=4 skip=6 seek=12 conv=notrunc 2>/dev/null
  137. fi
  138. #calculate and update checksum
  139. 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); }')
  140. /bin/echo -n -e $(echo -n $((0x$chsum)) | awk "$awk_ui16") |\
  141. dd of=lang_$1.bin bs=1 count=2 seek=8 conv=notrunc 2>/dev/null
  142. }
  143. if [ -z "$1" ]; then set 'all'; fi
  144. if [ "$1" = "all" ]; then
  145. generate_binary 'en'
  146. for lang in $LANGUAGES; do
  147. echo " Running : $lang" >&2
  148. generate_binary $lang
  149. done
  150. else
  151. generate_binary $1
  152. fi
  153. finish 0