utils/entry.sh

120 lines
3.1 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=$(date +%Y-%m-%d)
AUTHOR="$WAREHOUSE_AUTHOR"
EDITMODE=0
print_help() {
echo "Usage: entry [OPTION…]"
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"
}
# 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
echo "Date: $1"
DATE="$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