helper_functions 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. ## Funzioni di supporto
  2. # Printa errore
  3. echoerr(){
  4. echo -e "\n\e[0;31mError:\e[0m $1" 1>&2
  5. exit 1
  6. }
  7. # Printa la versione
  8. print_version(){
  9. echo -e "\n$VERSION"
  10. return 0
  11. }
  12. # Printa il banner
  13. print_banner(){
  14. echo "########################"
  15. echo "# LCM shiftctl v.$VERSION #"
  16. echo "########################"
  17. return 0
  18. }
  19. # Printa l'utilizzo
  20. print_usage(){
  21. echo
  22. echo "shiftctl: record to and query LCM 150 shifts database file."
  23. echo ""
  24. echo "options:"
  25. echo " -t Print the grand total of shifts durations"
  26. echo " -d Print shift dates for queried operator"
  27. echo " (If not specified by -o the program defaults"
  28. echo " to the invoker's user name)"
  29. echo " -o Specify an operator for queries"
  30. echo " -e Print shift entries for queried date"
  31. echo " (If not specified by -D the program defaults"
  32. echo " to today)"
  33. echo " -D Specify a date for queries"
  34. echo " (You can use natural language e.g. \"yesterday\""
  35. echo " e.g. \"last monday\" e.g. \"a month ago\")"
  36. echo " -r Rotate shifts file"
  37. echo " -v Print current version"
  38. echo " -l Show license"
  39. echo " -h Print this help message"
  40. return 0
  41. }
  42. # Setta la modalità operativa
  43. # (data a primo argomento)
  44. # e la variabile che lo notifica.
  45. # Il suo uso è inteso dentro il case
  46. # di getops.
  47. set_mode(){
  48. check_mode_set
  49. MODE="$1"
  50. MODE_FLAG="$flag"
  51. return 0
  52. }
  53. # Inizializza il file dei turni
  54. initialize_dbfile(){
  55. echo "date;operator;host;comment" > $SHIFT_FILE 2>/dev/null ||\
  56. echoerr "Failed to create shifts file $SHIFT_FILE."
  57. chgrp "$OPGROUP" "$SHIFT_FILE" &>/dev/null ||\
  58. echoerr "Failed to change group ownership of shifts file $SHIFT_FILE to $OPGROUP."
  59. chmod g=wr,o= "$SHIFT_FILE" &>/dev/null ||\
  60. echoerr "Failed to change mode of shifts file $SHIFT_FILE."
  61. return 0
  62. }
  63. ## Formattazione output
  64. # Impagina standard input
  65. paginate(){
  66. if [ -n "$PAGER" ]
  67. then
  68. $PAGER
  69. elif which more &>/dev/null || echoerr "Failed to paginate input with "$PAGER"."
  70. then
  71. more || echoerr "Failed to paginate input with "more"."
  72. else
  73. echoerr "No default pager set and "more" binary not found."
  74. fi
  75. return 0
  76. }
  77. # Impagina standard input, se necessario,
  78. # in base al numero di linee del terminale.
  79. # Altrimenti stampa a schermo.
  80. ifpaginate(){
  81. local LINES="$1"
  82. if [ "$(wc -l <<< "$LINES")" -gt "$(tput lines)" ]
  83. then
  84. echo -e "$LINES" | paginate
  85. else
  86. echo -e "\n$LINES"
  87. fi
  88. return 0
  89. }
  90. ## Funzioni di check
  91. # Controlla se la modalità operativa è già stata
  92. # settata oppure no
  93. # Il suo uso è inteso dentro il case
  94. # di getops.
  95. check_mode_set(){
  96. if [ -n "$MODE_FLAG" ]
  97. then
  98. echo -e "\n-$MODE_FLAG option and -$flag option are not compatible."
  99. CMDLN_FAIL=1
  100. return 1
  101. fi
  102. return 0
  103. }
  104. # Controlla una dipendenza per un'opzione
  105. check_opt_dep(){
  106. if [ "$MODE_FLAG" != "$1" ]
  107. then
  108. echo -e "\nOption -$flag must follow option -$1."
  109. CMDLN_FAIL=1
  110. return 1
  111. fi
  112. return 0
  113. }
  114. # Controlla che il primo argomento sia il
  115. # nome di un operatore
  116. check_operator(){
  117. [ "$1" = "vicini" ] && cat /dev/urandom
  118. [ "$1" = "$ID_0_150" ] || [ "$1" = "$ID_1_150" ] || [ "$1" = "$ID_2_150" ] ||\
  119. return 1
  120. return 0
  121. }
  122. # Check dell'utente che esegue il programma
  123. # Può essere solo uno dei tre associato agli
  124. # ID.
  125. check_runner(){
  126. if check_operator "$RUNNER"
  127. then
  128. echo -e "\nHi, $RUNNER!"
  129. else
  130. echoerr "$RUNNER, you are not an LCM operator. Go sweep sea."
  131. fi
  132. return 0
  133. }
  134. # Controlla che l'utente che esegue il
  135. # programma sia root
  136. check_root(){
  137. if [ "$(id -u)" -ne 0 ]
  138. then
  139. echoerr "Only root can do this. You are not root, you are pathetic."
  140. fi
  141. return 0
  142. }
  143. # Controlla se il file dei turni esiste e ha la mode
  144. # corretta
  145. check_dbfile(){
  146. case "$1" in
  147. exist)
  148. [ -f "$SHIFT_FILE" ]
  149. ;;
  150. mode)
  151. [ -w "$SHIFT_FILE" ] && [ -r "$SHIFT_FILE" ]
  152. ;;
  153. *)
  154. echoerr "$0: $1 is an invalid argument."
  155. esac
  156. return $?
  157. }
  158. # Controlla e inizializza in modo intelligente il file
  159. # dei turni
  160. smartinit_dbfile(){
  161. check_dbfile exist || initialize_dbfile
  162. check_dbfile mode ||\
  163. echoerr "Shifts file "$SHIFT_FILE" is not readable and/or writable by you."
  164. return 0
  165. }
  166. # Controlla che il primo argomento sia
  167. # una data e se lo è la printa formattata
  168. check_date(){
  169. date -d "$1" +%F 2>/dev/null ||\
  170. return 1
  171. return 0
  172. }