feat: add generate-week script

This commit is contained in:
Viktor Varland 2020-12-07 09:45:10 +01:00
commit d3ff11220b
2 changed files with 118 additions and 0 deletions

12
README Normal file
View file

@ -0,0 +1,12 @@
generate-week.sh
----------------
cron usage:
1 0 * * 1 TEMPLATE="dhis2 7.5" TIME_DIR=~/plan/time /home/varl/bin/generate-week.sh write
user alias:
function te () {
$EDITOR ~/plan/time/current.timedot
}

106
generate-week.sh Executable file
View file

@ -0,0 +1,106 @@
#!/usr/bin/env bash
set -euo pipefail
#
## environment variables
#
time_dir=${TIME_DIR:-.}
montofri_template=${TEMPLATE:-}
#
## script arguments
#
command=${1:-nonegiven}
in_date=${2:-$(date +%Y-%m-%d)}
weekday=$(date -d "$in_date" +%u)
if [[ $weekday -eq 1 ]]; then
base_monday=$in_date
else
overshoot=$(($weekday - 1))
base_monday=$(date -d "$in_date -${overshoot}days" +%Y-%m-%d)
fi
year="$(date +%Y)"
week="$(date -d "$base_monday" +%V)"
fname="${time_dir}/${year}-${week}".timedot
current="${time_dir}/current.timedot"
#
## support functions
#
function weekdays () {
local monday="$1"
days_in_week=(
"$monday"
$(date -d "$monday +1day" +%Y-%m-%d)
$(date -d "$monday +2day" +%Y-%m-%d)
$(date -d "$monday +3day" +%Y-%m-%d)
$(date -d "$monday +4day" +%Y-%m-%d)
$(date -d "$monday +5day" +%Y-%m-%d)
$(date -d "$monday +6day" +%Y-%m-%d)
)
for day in "${days_in_week[@]}"; do
local day_name="$(date -d $day +%A)"
echo "# $day_name"
echo "$day"
if [[ ! -z $montofri_template && "$day_name" != "Saturday" && "$day_name" != "Sunday" ]]; then
echo "$montofri_template"
fi
echo ""
done
}
function write () {
local fname="$1"
local content="$2"
tee $fname <<EOF
$content
EOF
}
#
## main script
#
case "$command" in
print)
weekdays "$base_monday"
;;
write)
if [[ ! -f $fname ]]; then
echo "Writing to $fname"
write "$fname" "$(weekdays $base_monday)"
else
echo "File exists: $fname"
fi
;;
*)
echo "Usage: generate-week.sh <command> [<args>]"
echo ""
echo "Commands: print, write"
echo "Args: datestring in format: YYYY-MM-DD (+%Y-%m-%d)"
echo ""
echo "Generate a weekly timedot file. Week starts on Monday."
echo "Does not print the TEMPLATE string on Sat/Sun."
echo "Updates symlink to current.timedot in the TIME_DIR."
echo ""
echo "Environment variables:"
echo "TIME_DIR Output directory, defaults to current directory."
echo " E.g. TIME_DIR=~/timedots"
echo "TEMPLATE The template string to use, defaults to empty."
echo " E.g. TEMPLATE='myproject 8'"
;;
esac