wochenbericht.sh (4574B)
1 #!/bin/bash 2 3 # Copyright © 2022 Markus Hanetzok <markus@hanetzok.net> 4 # This work is free. You can redistribute it and/or modify it under the 5 # terms of the Do What The Fuck You Want To Public License, Version 2, 6 # as published by Sam Hocevar. See the COPYING file for more details. 7 8 ### VARIABLES 9 # Determine the locations the script needs to know 10 script_dir="$(dirname $(realpath $0))" 11 tex_dir="$script_dir/tex" 12 out_dir="$script_dir/out" 13 user_data="$script_dir/user.csv" 14 15 ### FUNCTIONS 16 17 tex_cmd() { 18 pdflatex -output-directory "$out_dir" -jobname "$1" "$2" 19 rm "$out_dir"/*.aux "$out_dir"/*.log 20 } 21 22 dialog_helper() { 23 dialog --"$1" "$2" 0 0 --output-fd 1 24 } 25 26 error() { 27 clear 28 printf "ERROR: $1\n" 29 exit 1 30 } 31 32 read_data() { # Get the values from user.csv 33 while IFS=, read -r key value; do 34 case "$key" in 35 "NAME") name="$value" ;; 36 "TRAIN_YEAR") train_year="$value" ;; 37 "CAL_YEAR") cal_year="$value" ;; 38 "CAL_WEEK") cal_week="$value" ;; 39 "DEPARTMENT") department="$value" ;; 40 *) ;; 41 esac 42 done < "$user_data" 43 } 44 45 get_data() { # Get data via user input 46 name="$(dialog_helper "inputbox" "Name des Auszubildenden:")" 47 train_year="$(dialog_helper "inputbox" "Ausbildungsjahr:")" 48 cal_year="$(dialog_helper "inputbox" "Kalenderjahr des Berichts:")" 49 cal_week="$(dialog_helper "inputbox" "Kalenderwoche des Berichts:")" 50 department="$(dialog_helper "inputbox" "Abteilung:")" 51 } 52 53 # Function that determines the first and last day of the chosen week 54 get_date_range() { 55 local first_Mon 56 local date_fmt="+%d.%m.%Y" 57 local mon sun 58 59 if (($(date -d $cal_year-01-01 +%W))); then 60 first_Mon=$cal_year-01-01 61 else 62 first_Mon=$cal_year-01-$((01 + (7 - $(date -d $cal_year-01-01 +%u) + 1) )) 63 fi 64 65 mon=$(date -d "$first_Mon +$(($cal_week - 1)) week" "$date_fmt") 66 sun=$(date -d "$first_Mon +$(($cal_week - 1)) week + 6 day" "$date_fmt") 67 date_range="$mon - $sun" 68 } 69 70 vipe_cmd() { # Helper function to call vipe 71 content="$(echo "$1 (DIESE ZEILE LÖSCHEN!)" | vipe)" 72 sed -i "s/$2/$content/g" "$current_tex/wochenbericht.tex" 73 } 74 75 ### SCRIPT 76 77 dialog_helper "msgbox" "Willkommen im Wochenberichtsskript!" || error "dialog needs to be installed to run this script!" 78 79 # When tex dir is not found, script will exit, because it won't find template 80 [ -d "$tex_dir" ] || error "Cannot find given tex directory: $tex_dir" 81 82 [ -d "$out_dir" ] || mkdir -p "$out_dir" 83 84 while true; do 85 [ -f "$user_data" ] && read_data || get_data 86 get_date_range "$cal_week" "$cal_year" 87 88 # Let user validate inputs, let him redo the inputs if something is wrong 89 while true; do 90 dialog_helper "yesno" "Name: $name\nAusbildungsjahr: $train_year\nAusbildungsjahr: $train_year\nKalenderjahr: $cal_year\nAbteilung: $department\nWoche: $date_range\n\nSind die Angaben korrekt?" 91 92 case "$?" in 93 '0' ) break ;; 94 '1' ) get_data; get_date_range ;; 95 * ) error "Invalid case" ;; 96 esac 97 98 done 99 100 # Write the current data to user.csv 101 echo -e "NAME,$name\n\ 102 TRAIN_YEAR,$train_year\n\ 103 CAL_YEAR,$cal_year\n\ 104 CAL_WEEK,$cal_week"\n\ 105 DEPARTMENT,$department > "$user_data" 106 107 # Copy template and insert data into copied file 108 current_tex="$tex_dir/history/$cal_year/$cal_week" 109 mkdir -p "$current_tex" || error "Could not create directory for .tex files $current_tex" 110 111 cp -f "$tex_dir/template/wochenbericht.tex" "$current_tex/wochenbericht.tex" 112 sed -i "s/NAME/$name/" "$current_tex/wochenbericht.tex" 113 sed -i "s/WOCHE/$date_range/" "$current_tex/wochenbericht.tex" 114 sed -i "s/AUSBILDUNGSJAHR/$train_year/" "$current_tex/wochenbericht.tex" 115 sed -i "s/ABTEILUNG/$department/" "$current_tex/wochenbericht.tex" 116 117 # Use vipe to let user enter their report text 118 vipe_cmd "Betriebliche Tätigkeiten" BETRIEB 119 vipe_cmd "Außerbetriebliche Tätigkeiten" EXTERN 120 vipe_cmd "Berufsschule" SCHULE 121 122 # Compile via pdflatex, remove *.log and *.aux files 123 cd "$current_tex" 124 tex_cmd "$cal_year-$cal_week" "wochenbericht.tex" 125 126 # Increment cal_week and check if it was the last week of the year 127 if [ "$cal_week" == 52 ]; then 128 cal_week="1" 129 ((cal_year=cal_year+1)) 130 sed -i "s/CAL_WEEK.*/CAL_WEEK,$cal_week/" "$user_data" 131 sed -i "s/CAL_YEAR.*/CAL_YEAR,$cal_year/" "$user_data" 132 else 133 ((cal_week=cal_week+1)) 134 sed -i "s/CAL_WEEK.*/CAL_WEEK,$cal_week/" "$user_data" 135 fi 136 137 # Ask user if he wants to create the next report as well 138 dialog_helper "yesno" "Noch einen Bericht erstellen?" 139 case "$?" in 140 '0' ) break ;; 141 '1' ) clear; exit 0 ;; 142 * ) error "Invalid case" ;; 143 esac 144 done