## Funzioni di supporto # Printa errore echoerr(){ echo -e "\n\e[0;31mError:\e[0m $1" 1>&2 exit 1 } # Printa la versione print_version(){ echo "$VERSION" return 0 } # Printa il banner print_banner(){ echo "######################" echo "# LCM shiftctl v.$VERSION #" echo "######################" return 0 } # Printa l'utilizzo print_usage(){ echo "shiftctl: record to and query LCM 150 shifts database file." echo "" echo "options:" echo " -h Print this help message" echo " -v Print current version" echo " -t Print the grand total of shifts durations" echo " -r Rotate shifts file" return 0 } # Inizializza il file dei turni initialize_dbfile(){ echo "date;operator;host;comment" > $SHIFT_FILE 2>/dev/null ||\ echoerr "Failed to create shifts file $SHIFT_FILE." chgrp "$OPGROUP" "$SHIFT_FILE" &>/dev/null ||\ echoerr "Failed to change group ownership of shifts file $SHIFT_FILE to $OPGROUP." chmod g=wr,o= "$SHIFT_FILE" &>/dev/null ||\ echoerr "Failed to change mode of shifts file $SHIFT_FILE." return 0 } ## Funzioni di check # Check dell'utente che esegue il programma # può essere solo uno dei tre associato agli # ID sopra. check_user(){ if [ "$RUNNER" = "$ID_0_150" ] || [ "$RUNNER" = "$ID_1_150" ] || [ "$RUNNER" = "$ID_2_150" ] then echo -e "\nHi, $RUNNER!" else echoerr "$RUNNER, you are not an LCM operator. Go sweep sea." fi return 0 } # Controlla che l'utente che esegue il # programma sia root check_root(){ if [ "$(id -u)" -ne 0 ] then echoerr "Only root can do this. You are not root, you are pathetic." fi return 0 } # Controlla se il file dei turni esiste e ha la mode # corretta check_dbfile(){ case $1 in exist) [ -f "$SHIFT_FILE" ] ;; mode) [ -w "$SHIFT_FILE" ] && [ -r "$SHIFT_FILE" ] ;; *) echoerr "$0: $1 is an invalid argument." esac return $? } # Controlla e inizializza in modo intelligente il file # dei turni smartinit_dbfile(){ check_dbfile exist || initialize_dbfile check_dbfile mode ||\ echoerr "Shifts file "$SHIFT_FILE" is not readable and/or writable by you." return 0 }