helper_functions 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 "$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 "shiftctl: record to and query LCM 150 shifts database file."
  22. echo ""
  23. echo "options:"
  24. echo " -h Print this help message"
  25. echo " -v Print current version"
  26. echo " -t Print the grand total of shifts durations"
  27. echo " -r Rotate shifts file"
  28. return 0
  29. }
  30. # Inizializza il file dei turni
  31. initialize_dbfile(){
  32. echo "date;operator;host;comment" > $SHIFT_FILE 2>/dev/null ||\
  33. echoerr "Failed to create shifts file $SHIFT_FILE."
  34. chgrp "$OPGROUP" "$SHIFT_FILE" &>/dev/null ||\
  35. echoerr "Failed to change group ownership of shifts file $SHIFT_FILE to $OPGROUP."
  36. chmod g=wr,o= "$SHIFT_FILE" &>/dev/null ||\
  37. echoerr "Failed to change mode of shifts file $SHIFT_FILE."
  38. return 0
  39. }
  40. ## Funzioni di check
  41. # Check dell'utente che esegue il programma
  42. # può essere solo uno dei tre associato agli
  43. # ID sopra.
  44. check_user(){
  45. if [ "$RUNNER" = "$ID_0_150" ] || [ "$RUNNER" = "$ID_1_150" ] || [ "$RUNNER" = "$ID_2_150" ]
  46. then
  47. echo -e "\nHi, $RUNNER!"
  48. else
  49. echoerr "$RUNNER, you are not an LCM operator. Go sweep sea."
  50. fi
  51. return 0
  52. }
  53. # Controlla che l'utente che esegue il
  54. # programma sia root
  55. check_root(){
  56. if [ "$(id -u)" -ne 0 ]
  57. then
  58. echoerr "Only root can do this. You are not root, you are pathetic."
  59. fi
  60. return 0
  61. }
  62. # Controlla se il file dei turni esiste e ha la mode
  63. # corretta
  64. check_dbfile(){
  65. case $1 in
  66. exist)
  67. [ -f "$SHIFT_FILE" ]
  68. ;;
  69. mode)
  70. [ -w "$SHIFT_FILE" ] && [ -r "$SHIFT_FILE" ]
  71. ;;
  72. *)
  73. echoerr "$0: $1 is an invalid argument."
  74. esac
  75. return $?
  76. }
  77. # Controlla e inizializza in modo intelligente il file
  78. # dei turni
  79. smartinit_dbfile(){
  80. check_dbfile exist || initialize_dbfile
  81. check_dbfile mode ||\
  82. echoerr "Shifts file "$SHIFT_FILE" is not readable and/or writable by you."
  83. return 0
  84. }