177 lines
5.7 KiB
Bash
177 lines
5.7 KiB
Bash
# Simple Note System
|
|
# Copyright (C) 2016, Jon Lewis
|
|
#
|
|
# This program is free software; you can redistribute it and/or modify
|
|
# it under the terms of the GNU General Public License as published by
|
|
# the Free Software Foundation; either version 2 of the License, or
|
|
# (at your option) any later version.
|
|
#
|
|
# This program is distributed in the hope that it will be useful,
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
# GNU General Public License for more details.
|
|
#
|
|
# You should have received a copy of the GNU General Public License along
|
|
# with this program; if not, write to the Free Software Foundation, Inc.,
|
|
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
function create(){
|
|
# Depends : p_header
|
|
# Requires: $NOTE, $NOTE_DIR,
|
|
# Optional: $ENCRYPTION, $SESSION_ID, $TMP_DIR encrypt
|
|
# Given a valid setup, create writes the standard note header as specified
|
|
# by p_header, to $NOTE.
|
|
|
|
# Refuse to overwrite a note
|
|
if [ -e "$NOTES_DIR/$NOTE" ]; then
|
|
>&2 printf "$RED_COLOR!$RESET_COLOR %s\n\t%s\n"\
|
|
"Note already exists"\
|
|
"Hint: use -e to edit the note."
|
|
exit 200
|
|
fi
|
|
|
|
# If the note's notebook/section does not exist,
|
|
# create the appropriate folders.
|
|
mkdir -p "$NOTES_DIR"/"$(dirname "$NOTE")"
|
|
|
|
# Write the standard note header
|
|
if [ "$ENCRYPTION" == "TRUE" ]; then
|
|
TMP_NOTE="$TMP_DIR"/"$SESSION_ID"
|
|
p_header > "$TMP_NOTE"
|
|
encrypt
|
|
else
|
|
p_header > "$NOTES_DIR/$NOTE"
|
|
fi
|
|
# Make sure the note exists, and inform the user of the result.
|
|
if [ -e "$NOTES_DIR/$NOTE" ]; then
|
|
printf "%s\n" "- Created note: ${NOTE%.*}"
|
|
else
|
|
>&2 printf "$RED_COLOR!$RESET_COLOR %s\n"\
|
|
"Something went wrong, and the note was not created."
|
|
fi
|
|
}
|
|
function delete(){
|
|
# Requires: $NOTE, $NOTE_DIR
|
|
# Given a valid $NOTE, delete removes $NOTE from sns.
|
|
|
|
if [ -e "$NOTES_DIR/$NOTE" ]; then
|
|
printf "$RED_COLOR!!$RESET_COLOR %s%s" "Delete " "${NOTE%.*}"
|
|
read -p " (y/N) " YN
|
|
case "$YN" in
|
|
Y|y)
|
|
rm "$NOTES_DIR/$NOTE"
|
|
printf "%s\n" "- Deleted note: ${NOTE%.*}."
|
|
;;
|
|
*)
|
|
printf "%s\n" "Aborted."
|
|
;;
|
|
esac
|
|
#Cleanup empty notebooks/sections]
|
|
find "$NOTES_DIR" -mindepth 1 -type d | tac |\
|
|
while read -r DIR ; do
|
|
if [ ! "$(ls -A "$DIR")" ]; then
|
|
rmdir "$DIR"
|
|
fi
|
|
done
|
|
else
|
|
>&2 printf "$RED_COLOR!$RESET_COLOR %s\n" "Note ${NOTE%.*} does not exist."
|
|
fi
|
|
}
|
|
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
|
|
printf "%s\n" "- encrypting ${NOTE%.*}"
|
|
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
|
|
|
|
# If $VCTL is defined, add the edited note to the repo.
|
|
if [ "$VCTL" ]; then
|
|
>&2 printf "%s %s %s\n" "- adding to" "$VCTL" "repo".
|
|
verctl "$VCTL" add ""$NOTES_DIR"/$NOTE"
|
|
verctl "$VCTL" commit -m "Added/Changed $NOTE" > /dev/null
|
|
fi
|
|
}
|
|
function help {
|
|
printf "\n%s" "usage: sns [-cedlp] <notebook/section/name>"
|
|
if [ -z "$VCTL" ]; then
|
|
printf "\n%s%s%s" "usage: sns " "$VCTL" " ..."
|
|
fi
|
|
printf "\n%s" " sns [-hi]"
|
|
|
|
printf "\n%s" " -c | --create : Create note"
|
|
printf "\n%s" " -C | --config : Edit Config"
|
|
printf "\n%s" " -d | --delete : Delete note"
|
|
printf "\n%s" " -e | --edit : Open note for editing"
|
|
printf "\n%s" " -h | --help : Display this message"
|
|
printf "\n%s" " -i | --init : Write default config and initalize SNS store"
|
|
printf "\n%s" " -l | --list : List all notes in NOTEBOOK"
|
|
printf "\n%s" " -p | --print : Print note to console"
|
|
printf "\n\n"
|
|
}
|
|
function list(){
|
|
# This function, given a folder, $NOTE, will list the contents of $NOTE.
|
|
cd "$(dirname "$NOTES_DIR/$NOTE")"
|
|
find . -type f -name "*$EXT" | while read file; do
|
|
printf "%s\n" "$file"
|
|
done
|
|
|
|
|
|
}
|
|
function p_header(){
|
|
printf "# %s\n## %s\n" "$(basename "${NOTE%.*}")" "$(date "$DATE_FMT")"
|
|
}
|
|
function print(){
|
|
# Given an existing file, $NOTE, print prints the contents of $NOTE to stdout.
|
|
|
|
if [ -r "$NOTE" ]; then
|
|
if [ "$ENCRYPTION" == "TRUE" ]; then decrypt #to stdout
|
|
else cat "$NOTE"; fi
|
|
else
|
|
>&2 printf "$RED_COLOR!$RESET_COLOR %s\n\t%s\n"\
|
|
"Note cannot be found."
|
|
exit 205 #ERR_NOTE_NO_READ
|
|
fi
|
|
}
|