# 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. declare -r FILE="$NOTES_DIR/$NOTE.$EXT" # Refuse to overwrite a note if [ -e "$FILE" ]; 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 "$FILE")" # Write the standard note header if [ "$ENCRYPTION" == "TRUE" ]; then TMP_NOTE="$TMP_DIR"/"$SESSION_ID" p_header > "$TMP_NOTE" encrypt else p_header > "$FILE" fi # Make sure the note exists, and inform the user of the result. if [ -e "$FILE" ]; 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. declare -r FILE="$NOTES_DIR/$NOTE.$EXT" if [ -e "$FILE" ]; then printf "$RED_COLOR!!$RESET_COLOR %s%s" "Delete " "${NOTE%.*}" read -p " (y/N) " YN case "$YN" in Y|y) rm "$FILE" 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 # Set filename declare -r FILE="$NOTES_DIR/$NOTE.$EXT" # 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 "$FILE" ]; 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 "$FILE" "$FILE.bk" #Insurance TMP_NOTE="$TMP_DIR/$SESSION_ID" decrypt > "$TMP_NOTE" else TMP_NOTE="$FILE"; 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 "$FILE" encrypt; rm "$TMP_NOTE" if [ ! -r "$FILE" ]; then >&2 printf "$RED_COLOR!$RESET_COLOR %s\n" "error: note was not saved." cp "$FILE.bk" "$FILE" else rm "$FILE.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 "$FILE" verctl "$VCTL" commit -m "Added/Changed $NOTE" > /dev/null fi } function help { printf "\n%s" "usage: sns [-cedlp] " 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. # If not given a folder, it will list all notes in the store. if [ ! "$NOTEBOOK" ]; then NOTEBOOK="."; fi cd "$NOTES_DIR" 2>/dev/null || exit 0 find "$NOTEBOOK" -type f -name "*$EXT" 2>/dev/null || exit 0 | 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 }