Files
vns/sns.sh
Jon-William Lewis 16b9d5c469 Merge branch 'SNSv2' of alfheim:xilmwa/sns into SNSv2
Moving progress to another computer.
2016-02-09 20:37:04 -06:00

304 lines
8.8 KiB
Bash

#!/bin/bash
#==========================================================
# Simple Note System, v2.0a8
# Copyright 2014, Xenese Labs/Sicron-Perion XNF
#==========================================================
if [ -z "$HOME" ]; then HOME=/home/"$(whoami)"; fi
PROD_STR="Simple Note System"
readonly VER_STR="v2.0a8"
readonly ROOT_DIR="$HOME"/.config/xenlabs/sns
readonly NOTES_DIR="$ROOT_DIR"/notes
readonly TMP_DIR="$ROOT_DIR"/tmp
readonly CONFIG_FILE="$ROOT_DIR/sns.conf"
# Section: Functions
function w_conf {
if [ ! -r "$ROOT_DIR" ]; then mkdir -p "$ROOT_DIR"; fi
if [ ! -d "$TMP_DIR" ]; then mkdir -p "$TMP_DIR" ; fi
cat > "$CONFIG_FILE" << EOF
#==========================================================
# Simple Note System Config, v2.0a8
# Copyright 2014, Xenese Labs/Sicron-Perion XNF
#==========================================================
#File extension to use (for listing notes)
EXT=note
#Preferred Editor
if [ -z "$EDITOR" ]; then
EDITOR=vim
fi
#Encryption
#WARNING: ANY PREVIOUSLY UNENCRYPTED NOTES WILL BE LOST
#ENCRYPTION="TRUE"
ENCRYPTION="FALSE"
PUBKEY=""
EOF
chmod 600 "$CONFIG_FILE"
}
function pause {
read -rp " Press [Enter] to continue."
echo ""
}
function help {
echo ""
echo "usage: sns [-ce] NAME NOTEBOOK SECTION"
echo " sns [-d ] NAME NOTEBOOK SECTION"
echo " sns [-lp] NOTEBOOK"
echo " sns [-w ]"
echo " sns [-h ]"
echo ""
echo " -c | --create : Create note"
echo " -d | --delete : Delete note"
echo " -e | --edit : Open note for editing"
echo " -h | --help : Display this message"
echo " -p | --print : Print note to console"
echo " -l | --list : List all notes in NOTEBOOK"
echo " -w | --wconf : Rewrite default configuration"
echo ""
}
function p_header(){
printf "TITLE: %s\nDATE: %s\n" "$NAME" "$(date)"
}
function encrypt(){
# This function, given a recipient, $PUBKEY; a file to encrypt, $TMP_NOTE; and an
# output file, "$NOTE", will encrypt $TMP_NOTE to $NOTE against $PUBKEY's private
# GPG key.
gpg -r "$PUBKEY" -o "$NOTE" -e "$TMP_NOTE"
}
function decrypt(){
# This function, given a recipient, $PUBKEY; a file to decrypt, $TMP_NOTE; and an
# output file, "$NOTE", will decrpyt $TMP_NOTE to $NOTE against $PUBKEY's private
# GPG key.
gpg -d "$NOTE"
}
function create(){
if [ -e "$NOTE" ]; then
printf "\nERROR: Note already exists\nHint: use -e to edit the note.\n"
exit
else
mkdir -p "$NOTE_DIR"
fi
if [ -z "$ENCRYPTION" ]; then
echo "TITLE: $NAME" > "$NOTE"
echo "DATE: $(date)" >> "$NOTE"
elif [ "$ENCRYPTION" == "TRUE" ]; then
TMP_NOTE="$TMP_DIR"/"$SESSION_ID"
p_header > "$TMP_NOTE"
encrypt
fi
if [ -e "$NOTE" ]; then
echo "Created note: $NOTEBOOK/$SECTION/$NAME."
fi
}
function delete(){
if [ "$DELETE" == "TRUE" ]; then
if [ -e "$NOTE" ]; then
rm "$NOTE"
echo ""
echo "Deleted note: $NOTEBOOK/$SECTION/$NAME."
exit
else
echo ""
echo "ERROR: Note $NOTEBOOK/$SECTION/$NAME does not exist."
exit
fi
fi
}
function edit(){
if [ ! -r "$NOTE" ]; then
echo "ERROR: Note cannot be opened for editing."
exit 40;
fi
if [ "$ENCRYPTION" == "TRUE" ]; then
cp "$NOTE" "$NOTE".bk
if [ ! -d "$ROOT_DIR"/tmp ]; then mkdir "$ROOT_DIR"/tmp; fi
TMP_NOTE="$TMP_DIR/$SESSION_ID"
decrypt > "$TMP_NOTE"
else TMP_NOTE="$NOTE"; fi
if [ -z "$CREATE" ]; then printf "\nEDIT %s" "$(date)" >> "$TMP_NOTE"; fi
"$EDITOR" "$TMP_NOTE"
if [ "$ENCRYPTION" == "TRUE" ]; then
rm "$NOTE"
encrypt;
if [ -r "$NOTE" ]; then rm "$NOTE".bk; fi
fi
}
function print(){
if [ -r "$NOTE" ]; then
if [ -z "$CREATE" ]; then
if [ "$ENCRYPTION" == "TRUE" ]; then
decrypt
else
cat "$NOTE"
fi
else
printf "\nERROR: Note cannot be found.\n"
fi
fi
}
function list(){
if [ -d "$NOTES_DIR"/"$NOTEBOOK" ]; then
printf "+%s\n" "$NOTEBOOK"
find "$NOTES_DIR"/"$NOTEBOOK" -type f | while read -r NOTE; do
printf " -%s\n" "$(basename \"$NOTE\" | cut -d . -f 1 )"
done
fi
}
# End Section: Functions
###############################################################################
#==============================================================================
# Begin Main Program
#==============================================================================
###############################################################################
#==============================================================================
# Section: Configuration / Stage 1
#==============================================================================
if [ -r "$CONFIG_FILE" ]; then
source "$CONFIG_FILE"
else
create_sns_root
source "$CONFIG_FILE"
fi
if [ "$ENCRYPTION" == "TRUE" ]; then
if [ -z "$PUBKEY" ]; then
ERR_NO_KEY="TRUE"
ENCRYPTION="FALSE"
fi
command -v gpg >/dev/null 2>&1 ||\
{ ERR_NO_GPG="TRUE"; ENCRYPTION="FALSE"; }
fi
if [ "$ENCRYPTION" == "TRUE" ]; then
PROD_STR="Simple Note System (Encryption Enabled)"
if [ ! -d "$ROOT_DIR"/tmp ]; then
mkdir -p "$ROOT_DIR"/tmp
fi
fi
echo "$PROD_STR, $VER_STR"
if [ -z "$EDITOR" ]; then
>&2 echo "Error no editor specified in environment."
elif [ -n "$ERR_NO_GPG" ]; then
>&2 echo " Error: Encryption was specified, but GPG is not installed."
exit 100
elif [ -n "$ERR_NO_KEY" ]; then
>&2 echo " Error: No GPG recipient was provided in $CONFIG_FILE. "
exit 110
fi
#==============================================================================
# End Section: Configuration / Stage 1
#==============================================================================
#==============================================================================
# Section: Argument Parsing / Stage 2
#==============================================================================
NAME=""
NOTEBOOK=""
SECTION=""
if [ -z "$1" ]; then help; exit 20
else
ARGS=( "$@" )
for ARG in "${ARGS[@]}"; do
if [ "$ARG" = "-c" ] || [ "$ARG" = "--create" ]; then CREATE="TRUE"
elif [ "$ARG" = "-d" ] || [ "$ARG" = "--delete" ]; then DELETE="TRUE"
elif [ "$ARG" = "-e" ] || [ "$ARG" = "--edit" ]; then EDIT="TRUE"
elif [ "$ARG" = "-ce" ] || [ "$ARG" = "-ec" ]; then EDIT="TRUE"; CREATE="TRUE"
elif [ "$ARG" = "-p" ] || [ "$ARG" = "--print" ]; then PRINT="TRUE"
elif [ "$ARG" = "-l" ] || [ "$ARG" = "--list" ]; then LIST="TRUE"
elif [ "$ARG" = "-h" ] || [ "$ARG" == "--help" ]; then help; exit 0
elif [ "$ARG" = "-w" ] || [ "$ARG" == "--wconf" ]; then create_sns_root; exit 0
else
if [ -z "$NAME" ] && [ -n "$ARG" ]; then NAME="$ARG"
elif [ -z "$NOTEBOOK" ] && [ -n "$ARG" ]; then NOTEBOOK="$ARG"
elif [ -z "$SECTION" ] && [ -n "$ARG" ]; then SECTION="$ARG"
fi
fi
done
if [ -n "$NAME" ] && [ -z "$NOTEBOOK" ] && [ -n "$LIST" ]; then
# If we got a note title above, but no notebook, and the list option
# was specified, we assume that the detected note title is actually a
# notebook name.
NOTEBOOK="$NAME"
NAME=""
fi
fi
# Note: w_conf and help have highest priority, as they are the only functions
# that can work without any arguments.
#==============================================================================
# End Section: Argument Parsing / Stage 2
#==============================================================================
#==============================================================================
# Section: Actions / Stage 3
#==============================================================================
# List only requires a notebook, and is exclusive.
if [ -z "$NOTEBOOK" ]; then
echo " ERROR: Insufficient arguments:"
echo " Notebook not specified"
exit 30
fi
if [ -n "$LIST" ]; then
list
exit 0
fi
#All other functions require a note title and notebook.
if [ -z "$NAME" ]; then
echo " ERROR: Insufficient arguments:"
echo " Title not specified"
exit 30
fi
SESSION_ID="$RANDOM"
NOTE_DIR="$NOTES_DIR"/"$NOTEBOOK"/"$SECTION"/
if [ "$ENCRYPTION" == "TRUE" ]; then readonly NOTE="$NOTE_DIR""$NAME"."$EXT".gpg
else readonly NOTE="$NOTE_DIR""$NAME"."$EXT"
fi
if [ "$PRINT" == "TRUE" ]; then print; exit 0; fi
if [ "$DELETE" == "TRUE" ]; then delete; exit 0; fi
if [ "$CREATE" == "TRUE" ]; then create; fi
if [ "$EDIT" == "TRUE" ]; then edit; fi
#==============================================================================
# End Section: Actions / Stage 3
#==============================================================================