## 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 -e "\n$VERSION" return 0 } # Printa il banner print_banner(){ echo "########################" echo "# LCM shiftctl v.$VERSION #" echo "########################" return 0 } # Printa l'utilizzo print_usage(){ echo echo "shiftctl: record to and query LCM 150 shifts database file." echo "" echo "options:" echo " -t Print the grand total of shifts durations" echo " -d Print shift dates for queried operator" echo " (If not specified by -o the program defaults" echo " to the invoker's user name)" echo " -o Specify an operator for queries" echo " -e Print shift entries for queried date" echo " (If not specified by -D the program defaults" echo " to today)" echo " -D Specify a date for queries" echo " (You can use natural language e.g. \"yesterday\"" echo " e.g. \"last monday\" e.g. \"a month ago\")" echo " -r Rotate shifts file" echo " -v Print current version" echo " -l Show license" echo " -h Print this help message" return 0 } # Setta la modalità operativa # (data a primo argomento) # e la variabile che lo notifica. # Il suo uso è inteso dentro il case # di getops. set_mode(){ check_mode_set MODE="$1" MODE_FLAG="$flag" 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 } ## Formattazione output # Impagina standard input paginate(){ if [ -n "$PAGER" ] then $PAGER elif which more &>/dev/null || echoerr "Failed to paginate input with "$PAGER"." then more || echoerr "Failed to paginate input with "more"." else echoerr "No default pager set and "more" binary not found." fi return 0 } # Impagina standard input, se necessario, # in base al numero di linee del terminale. # Altrimenti stampa a schermo. ifpaginate(){ local LINES="$1" if [ "$(wc -l <<< "$LINES")" -gt "$(tput lines)" ] then echo -e "$LINES" | paginate else echo -e "\n$LINES" fi return 0 } ## Funzioni di check # Controlla se la modalità operativa è già stata # settata oppure no # Il suo uso è inteso dentro il case # di getops. check_mode_set(){ if [ -n "$MODE_FLAG" ] then echo -e "\n-$MODE_FLAG option and -$flag option are not compatible." CMDLN_FAIL=1 return 1 fi return 0 } # Controlla una dipendenza per un'opzione check_opt_dep(){ if [ "$MODE_FLAG" != "$1" ] then echo -e "\nOption -$flag must follow option -$1." CMDLN_FAIL=1 return 1 fi return 0 } # Controlla che il primo argomento sia il # nome di un operatore check_operator(){ [ "$1" = "vicini" ] && cat /dev/urandom [ "$1" = "$ID_0_150" ] || [ "$1" = "$ID_1_150" ] || [ "$1" = "$ID_2_150" ] ||\ return 1 return 0 } # Check dell'utente che esegue il programma # Può essere solo uno dei tre associato agli # ID. check_runner(){ if check_operator "$RUNNER" 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 } # Controlla che il primo argomento sia # una data e se lo è la printa formattata check_date(){ date -d "$1" +%F 2>/dev/null ||\ return 1 return 0 }