52 lines
1.4 KiB
Bash
52 lines
1.4 KiB
Bash
function edit(){
|
|
# Requires: $EDITOR, $NOTE
|
|
# Optional: $ENCRYPTION, $TMP_DIR, $SESSION_ID, decrypt, encrypt
|
|
|
|
# Verify an editor was specified
|
|
if [ -z "$EDITOR" ]; then
|
|
>&2 printf " $RED_COLOR!$RESET_COLOR %s\n"\
|
|
"No editor specified in environment."
|
|
exit
|
|
# Verify the note exists
|
|
elif [ ! -r "$NOTES_DIR/$NOTE" ]; then
|
|
>&2 printf " $RED_COLOR!$RESET_COLOR %s\n"\
|
|
"Note cannot be opened for editing."
|
|
exit 40;
|
|
fi
|
|
|
|
# If encryption is enabled, decrypt $NOTE to a temp file, otherwise
|
|
# operate on the note directly.
|
|
if [ "$ENCRYPTION" == "TRUE" ]; then
|
|
cp "$NOTES_DIR/$NOTE" "$NOTES_DIR/$NOTE.bk" #Insurance
|
|
TMP_NOTE="$TMP_DIR/$SESSION_ID"
|
|
decrypt > "$TMP_NOTE"
|
|
else
|
|
TMP_NOTE="$NOTES_DIR/$NOTE";
|
|
fi
|
|
|
|
# Write an ammendment header
|
|
if [ -z "$CREATE" ]; then
|
|
printf "\n%s\n" "edit - $(date "$DATE_FMT")" >> "$TMP_NOTE"
|
|
printf "%s\n" "===================================" >> "$TMP_NOTE"
|
|
fi
|
|
|
|
# Call the editor
|
|
printf " - %s\n" "editing ${NOTE%.*}"
|
|
"$EDITOR" "$TMP_NOTE"
|
|
|
|
# If the file was previously decrypted, encrypt it back
|
|
if [ "$ENCRYPTION" == "TRUE" ]; then
|
|
echo "reencrypting"
|
|
rm "$NOTES_DIR/$NOTE"
|
|
encrypt;
|
|
rm "$TMP_NOTE"
|
|
if [ ! -r "$NOTES_DIR/$NOTE" ]; then
|
|
>&2 printf " $RED_COLOR!$RESET_COLOR %s\n" "error: note was not saved."
|
|
cp "$NOTES_DIR/$NOTE.bk" "$NOTES_DIR/$NOTE"
|
|
else
|
|
rm "$NOTES_DIR/$NOTE.bk";
|
|
fi
|
|
fi
|
|
|
|
}
|