gen-text-file.sh 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #!/usr/bin/env bash
  2. dir="$1"
  3. shift
  4. if [[ ! -d $dir ]]
  5. then
  6. echo "Directory not found: $dir" >&2
  7. exit 1
  8. fi
  9. function gen_text()
  10. {
  11. local varname="$1"
  12. local src="$2"
  13. printf "\nconst char %s[] =\n{\n" "$varname"
  14. if [[ $varname =~ _cr$ ]]
  15. then
  16. grep -v '^~' "$src" \
  17. | sed 's/\\/\\\\/g; s/"/\\"/g; s/^/ "/; s/#FF#/#\\f/; s/$/\\r\\n"/'
  18. else
  19. grep -v '^~' "$src" \
  20. | sed 's/\\/\\\\/g; s/"/\\"/g; s/^/ "/; s/#FF#/#\\f/; s/$/\\n"/'
  21. fi
  22. printf "};\n\n"
  23. }
  24. for src in "$@"
  25. do
  26. src="${src##*/}"
  27. name="${src%.*}"
  28. name="text_${name//[-.]/_}"
  29. cname="$( echo "SZS_${name}_INC" | awk '{print toupper($0)}' )"
  30. #echo "$name : $src -> $dest"
  31. {
  32. printf "\n#ifndef %s\n#define %s 1\n" "$cname" "$cname"
  33. if [[ -f "$dir/$src" ]]
  34. then
  35. gen_text "$name" "$dir/$src"
  36. echo "$src: $dir/$src" >"$src.d"
  37. elif [[ -d "$dir/$src" ]]
  38. then
  39. printf '%s:' "$src" >"$src.d"
  40. for inc in "$dir/$src"/*.inc
  41. do
  42. name="${inc##*/}"
  43. name="${name%.*}"
  44. name="text_${name//[-.]/_}"
  45. gen_text "$name" "$inc"
  46. printf ' \\\n\t%s' "$inc" >>"$src.d"
  47. done
  48. printf '\n' >>"$src.d"
  49. else
  50. printf '\n!! Text not found: %s\n' "$dir/$src"
  51. exit 1
  52. fi
  53. printf "#endif // %s\n\n" "$cname"
  54. } >"$src"
  55. done