You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

235 lines
4.8 KiB

  1. #!/usr/bin/env bash
  2. # shellcheck shell=bash
  3. ################################################################################
  4. ####################### Definitions of global functions ########################
  5. ################################################################################
  6. # ``````````````````````````````````````````````````````````````````````````````
  7. # Function name: _sprintf()
  8. #
  9. # Description:
  10. # Function designed to output to the screen in a clear format.
  11. #
  12. # Usage:
  13. # _sprintf "type" "message"
  14. #
  15. # Examples:
  16. # _sprintf "head" "correct certificate: $_ssl_cert_file"
  17. #
  18. function _sprintf() {
  19. local _FUNCTION_ID="_sprintf"
  20. local _STATE="0"
  21. local _s_type="$1"
  22. local _s_info="$2"
  23. # Determine the type of character and color for each type
  24. # of output information.
  25. if [[ "$_s_type" == "head" ]] ; then
  26. s_char="::"
  27. s_trgb="1;32"
  28. elif [[ "$_s_type" == "info" ]] ; then
  29. s_char="»"
  30. s_trgb="0;33"
  31. elif [[ "$_s_type" == "warn" ]] ; then
  32. s_char="!"
  33. s_trgb="1;37"
  34. elif [[ "$_s_type" == "stop" ]] ; then
  35. s_char="!"
  36. s_trgb="1;31"
  37. else
  38. s_char="-"
  39. s_trgb="0;37"
  40. fi
  41. # If verbose mode is enabled, display info message.
  42. # shellcheck disable=SC2154
  43. if [[ "$_s_type" == "info" ]] ; then
  44. printf ' \e['${s_trgb}'m%s\e[m %s\n\n' "$s_char" "$_s_info"
  45. elif [[ "$_s_type" == "light-red" ]] ; then
  46. s_char="»"
  47. printf ' \e[1;49;31m%s\e[m %s\n' "$s_char" "$_s_info"
  48. elif [[ "$_s_type" == "light-green" ]] ; then
  49. s_char="»"
  50. printf ' \e[1;49;32m%s\e[m %s\n' "$s_char" "$_s_info"
  51. elif [[ "$_s_type" == "light-cyan" ]] ; then
  52. s_char="»"
  53. printf ' \e[1;49;36m%s\e[m %s\n' "$s_char" "$_s_info"
  54. else
  55. # If not, just display only the head, warn or stop string.
  56. # shellcheck disable=SC2154
  57. if [[ "$_s_type" == "head" ]] ; then
  58. if [[ "$s_color" == "true" ]] ; then
  59. c_trgb="1;38"
  60. printf '\e['${s_trgb}'m%s\e[m \e['${c_trgb}'m%s\e[m\n\n' "$s_char" "$_s_info"
  61. else
  62. printf '\e['${s_trgb}'m%s\e[m %s\n\n' "$s_char" "$_s_info"
  63. fi
  64. elif [[ "$_s_type" == "warn" ]] ; then
  65. if [[ "$s_color" == "true" ]] ; then
  66. c_trgb="1;43"
  67. printf ' \e['${s_trgb}'m%s\e[m \e['${c_trgb}'m%s\e[m\n' "$s_char" "$_s_info"
  68. else
  69. printf ' \e['${s_trgb}'m%s\e[m %s\n' "$s_char" "$_s_info"
  70. fi
  71. elif [[ "$_s_type" == "stop" ]] ; then
  72. if [[ "$s_color" == "true" ]] ; then
  73. c_trgb="1;41"
  74. printf ' \e['${s_trgb}'m%s\e[m \e['${c_trgb}'m%s\e[m\n' "$s_char" "$_s_info"
  75. else
  76. printf ' \e['${s_trgb}'m%s\e[m %s\n' "$s_char" "$_s_info"
  77. fi
  78. fi
  79. fi
  80. return "$_STATE"
  81. }
  82. # ``````````````````````````````````````````````````````````````````````````````
  83. # Function name: _load()
  84. #
  85. # Description:
  86. # Responsible for loading the configuration file, $config variable
  87. # parameter is defined in the script call.
  88. #
  89. # Usage:
  90. # _load "type" "path_to_config_file"
  91. #
  92. # Examples:
  93. # _load "info" "$config"
  94. # _load "head" "/tmp/file.cfg"
  95. #
  96. function _load() {
  97. local _FUNCTION_ID="_load"
  98. local _STATE="0"
  99. local _type="$1"
  100. local _filename="$2"
  101. if [[ ! -z "$_filename" ]] && [[ -e "$_filename" ]] ; then
  102. # If we do not want to inform that the file is loaded,
  103. # the value is 'null', otherwise:
  104. if [[ "$_type" == "head" ]] ; then
  105. _sprintf "info" "load configuration: '$_filename'"
  106. elif [[ "$_type" == "info" ]] ; then
  107. _sprintf "info" "load configuration: '$_filename'"
  108. fi
  109. # shellcheck disable=SC1090
  110. # If the file exists is loaded.
  111. . "$_filename" && \
  112. _logger "info" \
  113. "${_FUNCTION_ID}()" \
  114. "configuration file: '$_filename'"
  115. elif [ -z "$_filename" ] ; then
  116. _sprintf "stop" "incorrectly loaded '$_filename' file (incorrect filename)"
  117. else
  118. _sprintf "stop" "incorrectly loaded '$_filename' file (does not exist?)"
  119. fi
  120. return "$_STATE"
  121. }
  122. # ``````````````````````````````````````````````````````````````````````````````
  123. # Function name: _init_skel()
  124. #
  125. # Description:
  126. # Init user skel:
  127. # - create directory structure
  128. #
  129. # Usage:
  130. # _init_skel
  131. #
  132. # Examples:
  133. # _init_skel
  134. #
  135. function _init_skel() {
  136. local _FUNCTION_ID="_init_skel"
  137. local _STATE="0"
  138. local _shell_config="${HOME}/.bashrc"
  139. local _ana_string="source \"${HOME}/.awesome-ninja-admins/awesome-ninja-admins\""
  140. # shellcheck disable=SC2154
  141. _sprintf "light-cyan" "init user skel"
  142. # shellcheck disable=SC2154
  143. rsync -a "${HOME}/.awesome-ninja-admins/skel/" "${HOME}" > /dev/null 2>&1
  144. # shellcheck disable=SC2154
  145. _sprintf "light-cyan" "enable awesome-ninja-admins"
  146. _if_exist=$(grep -q "${_ana_string}" "${_shell_config}" ; echo "$?")
  147. if [[ "$_if_exist" -ne 0 ]] ; then
  148. echo "source \"${HOME}/.awesome-ninja-admins/awesome-ninja-admins\"" \
  149. >> "${_shell_config}"
  150. fi
  151. return "$_STATE"
  152. }