diff --git a/README.md b/README.md index 43f7aa9..9ce4094 100644 --- a/README.md +++ b/README.md @@ -3,21 +3,31 @@ A simple bash script that generates a monthly printout calendar template to be used between an employer and employee. -## Dependencies +## Table of contents -All of them should be already installed in your system: + -- [GNU Bash](http://www.gnu.org/software/bash/bash.html) -- [Coreutils](https://www.gnu.org/software/coreutils/) -- [Gawk](http://www.gnu.org/software/gawk/) -- [util-linux](https://www.kernel.org/pub/linux/utils/util-linux/) +- [monthly-attendance-paper](#monthly-attendance-paper) + - [Table of contents](#table-of-contents) + - [Dependencies](#dependencies) + - [Example](#example) + - [Configuration](#configuration) + - [Running](#running) + - [Printing](#printing) + - [Warning](#warning) + - [License](#license) -Optionally, to print the template: + -- [CUPS](http://www.cups.org/) +## Dependencies -or whatever printing system is able to print from the -standard input. +| Package name | Version | Required | +|--------------|---------|----------| +| [GNU Bash](http://www.gnu.org/software/bash/bash.html) | 5.0.011(1) | yes | +| [Coreutils](https://www.gnu.org/software/coreutils/) | 8.31 | yes | +| [Gawk](http://www.gnu.org/software/gawk/) | 5.0.1 | yes | +| [util-linux](https://www.kernel.org/pub/linux/utils/util-linux/) | 2.34 | yes | +| [CUPS](http://www.cups.org/) | 2.3.0 | no | ## Example @@ -26,18 +36,8 @@ that the example was made to test a leap year. ## Configuration -Edit `configrc` based on your needs. -variable names are self-explanatory. - -Important variables are: - - this_month - this_year - employer - employee - -as well as the days of the week, which you can translate in -your language. +Edit the `./configrc` file based on your needs. +Variable names are self-explanatory. You can also edit the date formats in the following script functions: @@ -46,11 +46,11 @@ You can also edit the date formats in the following script functions: ## Running - $ ./monthly_attendace_paper.sh + $ ./monthly_attendace_paper.sh ./configrc ## Printing - $ ./monthly_attendace_paper.sh | lpr + $ ./monthly_attendace_paper.sh ./configrc | lpr ## Warning @@ -58,11 +58,11 @@ This script heavily depends on tabs for formatting. What you see in a printout may be different to what you see on the screen; what you see on an editor after executing: - $ ./monthly_attendace_paper.sh > printout.txt + $ ./monthly_attendace_paper.sh ./configrc > printout.txt might be different than: - $ ./monthly_attendace_paper.sh + $ ./monthly_attendace_paper.sh ./configrc This script does not check any input nor output for errors. That's up to you. diff --git a/configrc b/configrc index 8eb78ed..2f58acb 100644 --- a/configrc +++ b/configrc @@ -1,7 +1,7 @@ # monthly-attendace-paper - A simple bash script that generates a monthly # printout template to be used between an employer and employee. # -# Written in 2017 by Franco Masotti/frnmst +# Written in 2017-2020 by Franco Masotti/frnmst # # To the extent possible under law, the author(s) have dedicated all # copyright and related and neighboring rights to this software to the public @@ -11,26 +11,37 @@ # with this software. If not, see # . -this_month="02" -this_year="2017" +# Days of week. +MON="Monday" +TUE="Tuesday" +WED="Wednesday" +THU="Thursday" +FRI="Friday" +SAT="Saturday" +SUN="Sunday" -title="Monthly attendance paper printout" -employer="Employer: THE EMPLOYER" -employee="Employee: THE EMPLOYEE" -date_id="Month and Year: $this_month $this_year" +# Month and year currently considered. +THIS_MONTH=02 +THIS_YEAR=2020 -day_column_header="Date/Month Day" -args_column_header="Worked Hours / Entry time - Exit time" -format="Format: \"$day_column_header $args_column_header\"" +# Header. +TITLE='Monthly attendance paper printout' +EMPLOYER="Employer: THE EMPLOYER" +EMPLOYEE="Employee: THE EMPLOYEE" +DATE_ID="Month and Year: "${THIS_MONTH}" ${THIS_YEAR}" -highlited_day="Sun" +DAY_COLUMN_HEADER="Date/Month Day" +ARGS_COLUMN_HEADER="Worked Hours / Entry time - Exit time" -Mon="Monday" -Tue="Tuesday" -Wed="Wednesday" -Thu="Thursday" -Fri="Friday" -Sat="Saturday" -Sun="Sunday" +FORMAT="Format: \"${DAY_COLUMN_HEADER} ${ARGS_COLUMN_HEADER}\"" +HIGHLITED_DAY='SUN' +HIGHLITED_DAY_DELIMITER='||' +NON_HIGHLITED_DAY_DELIMITER=' ' +# Formatting. +## Tabulations and newlines. +TABULATION_FOR_STANDARD_DAYS='printf \t\t\t' +# Irregular days are 31th and 29th Februaries. +TABULATION_FOR_IRREGULAR_DAYS='printf \t\t' +NEWLINE='printf \n\n\n' diff --git a/example.txt b/example.txt index c536c76..106ff92 100644 --- a/example.txt +++ b/example.txt @@ -5,52 +5,52 @@ Employer: THE EMPLOYER Employee: THE EMPLOYEE -Month and Year: 02 2012 +Month and Year: 02 2020 - 1/02 Wednesday 15/02 Wednesday + 1/02 Saturday 15/02 Saturday - 2/02 Thursday 16/02 Thursday +||2/02 Sunday|| ||16/02 Sunday|| - 3/02 Friday 17/02 Friday + 3/02 Monday 17/02 Monday - 4/02 Saturday 18/02 Saturday + 4/02 Tuesday 18/02 Tuesday -||5/02 Sunday|| ||19/02 Sunday|| + 5/02 Wednesday 19/02 Wednesday - 6/02 Monday 20/02 Monday + 6/02 Thursday 20/02 Thursday - 7/02 Tuesday 21/02 Tuesday + 7/02 Friday 21/02 Friday - 8/02 Wednesday 22/02 Wednesday + 8/02 Saturday 22/02 Saturday - 9/02 Thursday 23/02 Thursday +||9/02 Sunday|| ||23/02 Sunday|| - 10/02 Friday 24/02 Friday + 10/02 Monday 24/02 Monday - 11/02 Saturday 25/02 Saturday + 11/02 Tuesday 25/02 Tuesday -||12/02 Sunday|| ||26/02 Sunday|| + 12/02 Wednesday 26/02 Wednesday - 13/02 Monday 27/02 Monday + 13/02 Thursday 27/02 Thursday - 14/02 Tuesday 28/02 Tuesday + 14/02 Friday 28/02 Friday - 29/02 Wednesday + 29/02 Saturday Format: "Date/Month Day Worked Hours / Entry time - Exit time" diff --git a/monthly_attendace_paper.sh b/monthly_attendace_paper.sh index f77f0da..3cec7cc 100755 --- a/monthly_attendace_paper.sh +++ b/monthly_attendace_paper.sh @@ -1,9 +1,9 @@ #!/usr/bin/env bash - +# # monthly-attendace-paper - A simple bash script that generates a monthly # printout template to be used between an employer and employee. # -# Written in 2017 by Franco Masotti/frnmst +# Written in 2017-2020 by Franco Masotti/frnmst # # To the extent possible under law, the author(s) have dedicated all # copyright and related and neighboring rights to this software to the public @@ -13,143 +13,133 @@ # with this software. If not, see # . -. ./configrc +set -euo pipefail + +CONFIG="${1}" +. "${CONFIG}" declare -A days_string -days_string[Mon]="$Mon" -days_string[Tue]="$Tue" -days_string[Wed]="$Wed" -days_string[Thu]="$Thu" -days_string[Fri]="$Fri" -days_string[Sat]="$Sat" -days_string[Sun]="$Sun" +days_string[MON]="${MON}" +days_string[TUE]="${TUE}" +days_string[WED]="${WED}" +days_string[THU]="${THU}" +days_string[FRI]="${FRI}" +days_string[SAT]="${SAT}" +days_string[SUN]="${SUN}" declare -A days_id -days_id[0]=Mon -days_id[1]=Tue -days_id[2]=Wed -days_id[3]=Thu -days_id[4]=Fri -days_id[5]=Sat -days_id[6]=Sun - -### - -printf "%s\n\n\n" "$title" -printf "%s\n\n" "$employer" -printf "%s\n\n" "$employee" -printf "%s\n" "$date_id" -printf "\n\n" - -### - -is_febuary="false" -if [ "$this_month" = "02" ]; then - is_febuary="true" -fi - -# Algorithm got from Wikipedia. -is_leap_year="false" -if [ $(($this_year % 4)) -ne 0 ]; then - is_leap_year="false" -elif [ $(($this_year % 100)) -ne 0 ]; then - is_leap_year="true" -elif [ $(($this_year % 400)) -ne 0 ]; then - is_leap_year="false" -else - is_leap_year="true" -fi - -get_cal="$(cal -m $this_month $this_year | awk 'NF > 0' | tail -n +3)" - -# Starting from monday (1) get the first and last day id of the month. -first_cal_line="$(printf "%s" "$get_cal" | head -n1)" -last_cal_line="$(printf "%s" "$get_cal" | tail -n1)" - -first_day_id=$(printf "$first_cal_line" | wc -w) -first_day_id=$((7-$first_day_id+1)) -first_day_id=$(($first_day_id-1)) - -last_day_id=$(printf "%s" "$last_cal_line" | wc -w) -last_day_number=$(echo $last_cal_line | awk "{print \$$last_day_id}") -last_day_id=$(($last_day_id-1)) - -day_offset=15 -if [ "$is_febuary" = "true" ]; then - day_offset=14 -fi -month="$(seq 1 $day_offset)" - -tabulation() -{ - printf "\t\t\t" -} - -newline() -{ - printf "\n\n\n" -} +days_id[0]=MON +days_id[1]=TUE +days_id[2]=WED +days_id[3]=THU +days_id[4]=FRI +days_id[5]=SAT +days_id[6]=SUN get_full_string() { - local day=$1 - local ds="$2" + local day=${1} + local ds="${2}" - printf "$day/$this_month\t$ds" + printf "%s\t%s" ""${day}"/"${THIS_MONTH}"" "${ds}" } print_day() { - local day=$1 - local day_id=$2 + local day=${1} + local day_id=${2} local dd="${days_id[$day_id]}" local ds="${days_string[$dd]}" - # If sunday - if [ $dd = $highlited_day ]; then - printf "||" - get_full_string "$day" "$ds" - printf "||" + # By default: SUN. + if [ "${dd}" = "${HIGHLITED_DAY}" ]; then + printf "%s" "${HIGHLITED_DAY_DELIMITER}" + get_full_string "${day}" "${ds}" + printf "%s" "${HIGHLITED_DAY_DELIMITER}" else - printf " " - get_full_string "$day" "$ds" - printf " " + printf "%s" "${NON_HIGHLITED_DAY_DELIMITER}" + get_full_string "${day}" "${ds}" + printf "%s" "${NON_HIGHLITED_DAY_DELIMITER}" fi } print_day_left() { - print_day $1 $2 - tabulation + print_day ${1} ${2} + ${TABULATION_FOR_STANDARD_DAYS} } print_day_right() { - print_day $1 $2 - newline + print_day ${1} ${2} + ${NEWLINE} } -day_id=$first_day_id +# Header. +printf "%s\n\n\n" "${TITLE}" +printf "%s\n\n" "${EMPLOYER}" +printf "%s\n\n" "${EMPLOYEE}" +printf "%s\n" "${DATE_ID}" +printf "\n\n" + +is_febuary='false' +if [ "${THIS_MONTH}" = '2' ] || [ "${THIS_MONTH}" = '02' ]; then + is_febuary='true' +fi + +# Algorithm got from Wikipedia. +is_leap_year='false' +if [ $((${THIS_YEAR} % 4)) -ne 0 ]; then + is_leap_year='false' +elif [ $((${THIS_YEAR} % 100)) -ne 0 ]; then + is_leap_year='true' +elif [ $((${THIS_YEAR} % 400)) -ne 0 ]; then + is_leap_year='false' +else + is_leap_year='true' +fi + +get_cal="$(cal --monday ${THIS_MONTH} ${THIS_YEAR} | awk 'NF > 0' | tail --lines=+3)" + +# Starting from monday (1) get the first and last day id of the month. +first_cal_line="$(printf "%s" "${get_cal}" | head --lines=1)" +last_cal_line="$(printf "%s" "${get_cal}" | tail --lines=1)" + +first_day_in_month_id_reverse=$(printf "${first_cal_line}" | wc --words) +first_day_in_month_id=$((7 - ${first_day_in_month_id_reverse} + 1)) +first_day_in_month_id=$((${first_day_in_month_id} - 1)) + +last_day_in_month_id=$(printf "%s" "${last_cal_line}" | wc --words) +last_day_in_month_number=$(echo ${last_cal_line} | awk "{print \$$last_day_in_month_id}") +last_day_in_month_id=$((${last_day_in_month_id} - 1)) + +day_offset=15 +if [ "${is_febuary}" = 'true' ]; then + day_offset=14 +fi -for day in $month; do - print_day_left $day $(($day_id % 7)) - print_day_right $(($day + $day_offset)) $((($day_id + $day_offset) % 7)) +half_month="$(seq 1 ${day_offset})" +day_id=${first_day_in_month_id} +for day in ${half_month}; do + print_day_left ${day} $((${day_id} % 7)) + print_day_right $((${day} + ${day_offset})) $(((${day_id} + ${day_offset}) % 7)) - day_id=$(($day_id + 1)) + day_id=$((${day_id} + 1)) done # Months with 31 days. -if [ $last_day_number -eq 31 ]; then - tabulation - printf "\t\t" - print_day_right 31 $((($day_id + $day_offset) % 7)) +if [ ${last_day_in_month_number} -eq 31 ]; then + ${TABULATION_FOR_STANDARD_DAYS} + ${TABULATION_FOR_IRREGULAR_DAYS} + print_day_right 31 $(((${day_id} + ${day_offset}) % 7)) fi -if [ "$is_leap_year" = "true" ]; then - tabulation - printf "\t\t" - print_day_right 29 $((($day_id + $day_offset) % 7)) +# February in a leap year. +if [ "${is_leap_year}" = 'true' ]; then + ${TABULATION_FOR_STANDARD_DAYS} + ${TABULATION_FOR_IRREGULAR_DAYS} + print_day_right 29 $(((${day_id} + ${day_offset}) % 7)) fi +printf "%s\n" "${FORMAT}" -printf "%s\n" "$format"