addlang 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. #!/bin/bash
  2. #
  3. # addlang
  4. #
  5. # Language addition helper for the Dragora GNU/Linux-Libre website
  6. # (https://www.dragora.org)
  7. #
  8. #
  9. # Copyright (C) 2021 Michael Siegel
  10. #
  11. # Licensed under the Apache License, Version 2.0 (the "License");
  12. # you may not use this file except in compliance with the License.
  13. # You may obtain a copy of the License at
  14. #
  15. # http://www.apache.org/licenses/LICENSE-2.0
  16. #
  17. # Unless required by applicable law or agreed to in writing, software
  18. # distributed under the License is distributed on an "AS IS" BASIS,
  19. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  20. # See the License for the specific language governing permissions and
  21. # limitations under the License.
  22. #### INCLUDES ####
  23. . include/constants || exit 1
  24. . include/subroutines || exit 1
  25. #### CONSTANTS ####
  26. declare -r ISO_LANG_CODES=(
  27. aa ab ae af ak am an ar as av ay az
  28. ba be bg bh bi bm bn bo br bs
  29. ca ce ch co cr cs cu cv cy
  30. da de dv dz
  31. ee el en eo es et eu
  32. fa ff fi fj fo fr fy
  33. ga gd gl gn gu gv
  34. ha he hi ho hr ht hu hy hz
  35. ia id ie ig ii ik io is it iu
  36. ja jv
  37. ka kg ki kj kk kl km kn ko kr ks ku kv kw ky
  38. la lb lg li ln lo lt lu lv
  39. mg mh mi mk ml mn mr ms mt my
  40. na ne nb nd ng nl nn no nr nv ny
  41. oc oj om or os
  42. pa pi pl ps pt
  43. qu
  44. rm rn ro ru rw
  45. sa sc sd se sg si sk sl sm sn so sq sr st ss su sv sw
  46. ta te tg th ti tk tl tn to tr ts tt tw ty
  47. ug uk ur uz
  48. ve vi vo
  49. wa wo
  50. xh
  51. yi yo
  52. za zh zu
  53. )
  54. #### GLOBAL PARAMETERS ####
  55. new_lang=
  56. #### FUNCTIONS ####
  57. _lang_valid() {
  58. local input="$1"
  59. local lang=
  60. for lang in "${ISO_LANG_CODES[@]}"
  61. do
  62. [[ "$lang" = "$input" ]] && return 0
  63. done
  64. unset lang
  65. return 1
  66. }
  67. _lang_exists() {
  68. local input="$1"
  69. local lang=
  70. for lang in $(_get_lang_dirs)
  71. do
  72. [[ "$lang" = "$input" ]] && return 0
  73. done
  74. unset lang
  75. return 1
  76. }
  77. _prompt_new_lang() {
  78. local input=
  79. while true
  80. do
  81. if [[ -z "$input" ]]
  82. then
  83. printf '%s\n' \
  84. 'Please enter the ISO two-letter code for the language to add:'
  85. printf '%s\n' "$HELP_TIP_INTERACTIVE"
  86. fi
  87. read -rp "$PROMPT" input
  88. if [[ -z "$input" ]]
  89. then
  90. continue
  91. elif [[ "$input" = "$HELP_COMMAND" ]]
  92. then
  93. _show_help "$FUNCNAME"
  94. elif ! _lang_valid "$input"
  95. then
  96. _perr "invalid language code -- '$input'"
  97. printf '%s\n' "$HELP_TIP_INTERACTIVE"
  98. elif _lang_exists "$input"
  99. then
  100. _perr "language already exists -- '$input'"
  101. printf '%s\n' "$HELP_TIP_INTERACTIVE"
  102. else
  103. break
  104. fi
  105. done
  106. new_lang="$input"
  107. }
  108. _show_help() {
  109. case "$1" in
  110. global)
  111. cat <<'EOF'
  112. Add a new language interactively
  113. Usage:
  114. addlang
  115. Options:
  116. --help
  117. Show this help text and exit
  118. EOF
  119. ;;
  120. # _prompt_new_lang)
  121. # Display complete list on help request?
  122. # ;;
  123. *)
  124. printf '%s\n' "Sorry, no help text available."
  125. ;;
  126. esac
  127. }
  128. #### MAIN ####
  129. if [[ "$1" = '--help' ]]
  130. then
  131. _show_help global
  132. exit
  133. fi
  134. ## Environment checks
  135. _env_checks || _abort
  136. ## Action
  137. _prompt_new_lang || _abort
  138. cp -a -- "$PAGES_DIR_MASTER" "$PAGES_DIR"/"$new_lang" || \
  139. { _perr "Could not add new language '$new_lang'."; _abort; }
  140. printf '%s\n' "Added new language '$new_lang'."