utils/entry.sh
Irrlicht ba9fb86efb
[entry] Improve help and fix reliability
When the use specifies a date, reformat it use the _date_ command to
make sure it's in the appropriate format for further treatment.
2026-02-13 12:39:18 +01:00

123 lines
3.7 KiB
Bash
Executable file

#!/bin/bash
# A script to create journal entries.
# Default behavior is to create an entry for the current date. See "help" for usage and options.
# Unsafe script
set -e
DATE_FORMAT="+%Y-%m-%d"
DATE=$(date $DATE_FORMAT)
AUTHOR="$WAREHOUSE_AUTHOR"
EDITMODE=0
print_help() {
echo "Usage: entry [OPTION…]"
echo ""
echo "Create a journal entry. The root directory is controled by the WAREHOUSE_DIR envionment variable. The Default author is controled by the WAREHOUSE_AUTHOR environment variable. The entry is created at WAREHOUSE_DIR/content/journal/<year>/<month> where year is the current <year> in \"YYYY\" format and <month> in \"MM\" format. Uses date for operation."
echo ""
echo " -a, --author author Change the author name in the created entry."
echo " -e, --edit Edit the entry file created using user defined editor. The editor is specified using the EDITOR environment variable. Does nothing if said variable is not defined."
echo " -h, --help Display this help message and exit."
echo " -n, --new [DATE] Create a new entry for the specified DATE (optional). If no date is specified, use current date. This is default behavior. DATE is expected in a format valid for the 'date' command. If multiple \"-n\" option are specified, only the last one is taken into account. Useful if you need to, for example, set an alias with a relative date, e.g. \"yesterday\" but still want to be able to specify a date when invoking _entry_."
}
# Try to edit the entry file corresponding to the given date. Only edit the file if the '-e' option was specified
edit_file() {
if [ "$EDITMODE" -eq 1 ] && [ -n "$EDITOR" ]
then
$EDITOR "$OUTPATH"
fi
}
# Write the data to the specified file. Creates the file if it doesn't exists
write_entry() {
echo "New template : $FULLDATE"
echo "Title: Journal du $DAY ${MOUNTHNAME^} $YEAR" >> "$OUTPATH"
echo "Date: $DATE" >> "$OUTPATH"
echo "Author: $AUTHOR" >> "$OUTPATH"
echo "Category: $YEAR" >> "$OUTPATH"
echo "Tags:" >> "$OUTPATH"
echo "" >> "$OUTPATH"
echo "## Les bonnes choses" >> $OUTPATH
echo >> $OUTPATH
echo "-" >> $OUTPATH
}
while [ $# -gt 0 ]
do
case $1 in
-a|--author)
shift
if [ -z "$1" ]
then
echo "You must specify an author"
exit 1
fi
AUTHOR="$1"
shift
;;
-e|--edit)
EDITMODE=1
shift
;;
-h|--help)
shift
print_help
exit 0
;;
# Make a new template starting using current date (today)
-n|--new)
shift # past argument
if [ -n "$1" ]
then
DATE="$(date $DATE_FORMAT -d $1)"
shift
fi
;;
-*|--*)
echo "Unknown option $1"
exit 1
;;
*)
echo "Extra useless argument: $1"
exit 1
;;
esac
done
YEAR=$(date +%Y -d "$DATE")
MOUNTH=$(date +%m -d "$DATE")
MOUNTHNAME=$(LC_TIME=fr_FR.UTF-8 date +%B -d "$DATE")
TMP=$(date +%e -d "$DATE")
DAY=${TMP/ /}
MOUNTHDIR=$(date "+%m" -d "$DATE")
FULLDATE="$(date +%e -d $DATE) $(date +%B -d $DATE) $(date +%Y -d $DATE)"
FILENAME="$DATE.md"
OUTDIR="content/journal/$YEAR/$MOUNTHDIR"
if [ ! -z WARHOUSE_DIR ]
then
OUTDIR="$WAREHOUSE_DIR/$OUTDIR"
fi
OUTPATH="$OUTDIR/$FILENAME"
if [ -f "$OUTPATH" ]
then
if [ "$EDITMODE" -eq 1 ]
then
edit_file
exit 0
else
echo "Error, $OUTDIR: file exists"
exit 1
fi
fi
if [ ! -d "$OUTDIR" ]
then
echo "Creating necessary directories $OUTDIR"
mkdir -p "$OUTDIR"
fi
write_entry
edit_file