make_lang.sh 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. #!/bin/sh
  2. # makelang.sh - multi-language support high-level script
  3. # for generating lang_xx.bin (secondary language binary file)
  4. #
  5. # Input files:
  6. # lang_en.txt
  7. # lang_en_$LANG.txt
  8. #
  9. # Output files:
  10. # lang_en.tmp (temporary, will be removed when finished)
  11. # lang_en_$LANG.tmp ==||==
  12. # lang_en_$LANG.dif ==||==
  13. # lang_$LANG.txt
  14. #
  15. #
  16. # Selected language:
  17. LANG=$1
  18. if [ -z "$LANG" ]; then LANG='cz'; fi
  19. #
  20. #
  21. function finish
  22. {
  23. if [ "$1" == "0" ]; then
  24. if [ -e lang_en.tmp ]; then rm lang_en.tmp; fi
  25. if [ -e lang_en_$LANG.tmp ]; then rm lang_en_$LANG.tmp; fi
  26. if [ -e lang_en_$LANG.dif ]; then rm lang_en_$LANG.dif; fi
  27. fi
  28. echo
  29. if [ "$1" == "0" ]; then
  30. echo "make_lang.sh finished with success" >&2
  31. else
  32. echo "make_lang.sh finished with errors!" >&2
  33. fi
  34. case "$-" in
  35. *i*) echo "press enter key"; read ;;
  36. esac
  37. exit $1
  38. }
  39. echo "make_lang.sh started" >&2
  40. echo "selected language=$LANG" >&2
  41. #check if input files exists
  42. echo -n " checking input files..." >&2
  43. if [ ! -e lang_en.txt ]; then echo "NG! file lang_en.txt not found!" >&2; exit 1; fi
  44. if [ ! -e lang_en_$LANG.txt ]; then echo "NG! file lang_en_$LANG.txt not found!" >&2; exit 1; fi
  45. echo "OK" >&2
  46. #filter comment and empty lines from key and dictionary files, create temporary files
  47. echo -n " creating tmp files..." >&2
  48. cat lang_en.txt | sed "/^$/d;/^#/d" > lang_en.tmp
  49. cat lang_en_$LANG.txt | sed "/^$/d;/^#/d" > lang_en_$LANG.tmp
  50. echo "OK" >&2
  51. #cat lang_en_$LANG.tmp | sed 'n;d' >test1.txt
  52. #compare files using diff and check for differences
  53. echo -n " comparing tmp files..." >&2
  54. if ! cat lang_en_$LANG.tmp | sed 'n;d' | diff lang_en.tmp - > lang_en_$LANG.dif; then
  55. echo "NG!" >&2
  56. echo "Entries in lang_en_$LANG.txt are different from lang_en.txt!" >&2
  57. echo "please check lang_en_$LANG.dif" >&2
  58. finish 1
  59. fi
  60. echo "OK" >&2
  61. #generate lang_xx.txt (secondary language text data sorted by ids)
  62. echo -n " generating lang_$LANG.txt..." >&2
  63. cat lang_en_$LANG.tmp | sed '1~2d' | sed "s/^\"\\\\x00/\"/" > lang_$LANG.txt
  64. echo "OK" >&2
  65. #generate lang_xx.dat (secondary language text data in binary form)
  66. echo -n " generating lang_$LANG.dat..." >&2
  67. cat lang_$LANG.txt | sed "s/\\\\/\\\\\\\\/g" | while read s; do
  68. s=${s#\"}
  69. s=${s%\"}
  70. echo -n -e "$s"'\x00'
  71. done >lang_$LANG.dat
  72. echo "OK" >&2
  73. #calculate variables
  74. lt_magic='\xa5\x5a\xb4\x4b'
  75. lt_count=$(grep -c '^' lang_$LANG.txt)
  76. lt_data_size=$(wc -c lang_$LANG.dat | cut -f1 -d' ')
  77. lt_offs_size=$((2 * $lt_count))
  78. lt_size=$((16 + $lt_offs_size + $lt_data_size))
  79. lt_chsum=1
  80. lt_resv0='\xff\xff'
  81. lt_resv1='\xff\xff\xff\xff'
  82. #generate lang_xx.ofs (secondary language text data offset table)
  83. echo -n " generating lang_$LANG.ofs..." >&2
  84. cat lang_$LANG.txt | sed "s/\\\\x[0-9a-f][0-9a-f]/\./g;s/\\\\[0-7][0-7][0-7]/\./g" |\
  85. awk 'BEGIN { o='$((16 + $lt_offs_size))';} { printf("%d\n",o); o+=(length($0)-1); }' > lang_$LANG.ofs
  86. echo "OK" >&2
  87. #generate lang_xx.bin (secondary language result binary file)
  88. echo " generating lang_$LANG.bin:" >&2
  89. #create empty file
  90. dd if=/dev/zero of=lang_$LANG.bin bs=1 count=$lt_size 2>/dev/null
  91. #awk code to format ui16 variables for dd
  92. awk_ui16='{ h=int($1/256); printf("\\x%02x\\x%02x\n", int($1-256*h), h); }'
  93. #write data to binary file with dd
  94. echo -n " writing header (16 bytes)..." >&2
  95. echo -n -e "$lt_magic" |\
  96. dd of=lang_$LANG.bin bs=1 count=4 seek=0 conv=notrunc 2>/dev/null
  97. echo -n -e $(echo -n "$lt_size" | awk "$awk_ui16") |\
  98. dd of=lang_$LANG.bin bs=1 count=2 seek=4 conv=notrunc 2>/dev/null
  99. echo -n -e $(echo -n "$lt_count" | awk "$awk_ui16") |\
  100. dd of=lang_$LANG.bin bs=1 count=2 seek=6 conv=notrunc 2>/dev/null
  101. echo -n -e $(echo -n "$lt_chsum" | awk "$awk_ui16") |\
  102. dd of=lang_$LANG.bin bs=1 count=2 seek=8 conv=notrunc 2>/dev/null
  103. echo -n -e "$lt_resv0" |\
  104. dd of=lang_$LANG.bin bs=1 count=2 seek=10 conv=notrunc 2>/dev/null
  105. echo -n -e "$lt_resv1" |\
  106. dd of=lang_$LANG.bin bs=1 count=4 seek=12 conv=notrunc 2>/dev/null
  107. echo "OK" >&2
  108. echo -n " writing offset table ($lt_offs_size bytes)..." >&2
  109. echo -n -e $(cat lang_$LANG.ofs | awk "$awk_ui16" | tr -d '\n'; echo) |\
  110. dd of=./lang_$LANG.bin bs=1 count=$lt_offs_size seek=16 conv=notrunc 2>/dev/null
  111. echo "OK" >&2
  112. echo -n " writing text data ($lt_data_size bytes)..." >&2
  113. dd if=./lang_$LANG.dat of=./lang_$LANG.bin bs=1 count=$lt_data_size seek=$((16 + $lt_offs_size)) conv=notrunc 2>/dev/null
  114. echo "OK" >&2
  115. echo " lang_table details:" >&2
  116. echo " lt_count = $lt_count" >&2
  117. echo " lt_size = $lt_size" >&2
  118. echo " lt_chsum = $lt_chsum" >&2
  119. finish 0