Files
vns/src/includes/edit.sns.sh
Jon-William Lewis 6105190e78 Fixed critical issues relating to encryption, including misplaced files
and failure to erase temporary files.
2016-02-15 18:48:56 -06:00

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
}