Formatting changes
This commit is contained in:
223
sns.sh
223
sns.sh
@@ -1,7 +1,7 @@
|
||||
#!/bin/bash
|
||||
#==========================================================
|
||||
# Simple Note System, v2.0a8
|
||||
# Copyright 2014, Xenese Labs/Sicron-Perion XNF
|
||||
# Copyright 2016, Xenese Labs/Sicron-Perion XNF
|
||||
#==========================================================
|
||||
|
||||
if [ -z "$HOME" ]; then HOME=/home/"$(whoami)"; fi
|
||||
@@ -14,6 +14,7 @@ readonly TMP_DIR="$ROOT_DIR"/tmp
|
||||
readonly CONFIG_FILE="$ROOT_DIR/sns.conf"
|
||||
|
||||
|
||||
|
||||
# Section: Functions
|
||||
function create_sns_root {
|
||||
|
||||
@@ -112,7 +113,6 @@ function create(){
|
||||
if [ -e "$NOTE" ]; then
|
||||
echo "Created note: $NOTEBOOK/$SECTION/$NAME."
|
||||
fi
|
||||
|
||||
}
|
||||
function delete(){
|
||||
if [ "$DELETE" == "TRUE" ]; then
|
||||
@@ -180,131 +180,114 @@ function list(){
|
||||
|
||||
|
||||
|
||||
|
||||
###############################################################################
|
||||
#==============================================================================
|
||||
# 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 [ -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"
|
||||
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
# Entry Point
|
||||
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
#==============================================================================
|
||||
# Stage 1: Read Configuration
|
||||
#==============================================================================
|
||||
if [ -r "$CONFIG_FILE" ]; then
|
||||
source "$CONFIG_FILE"
|
||||
else
|
||||
create_sns_root
|
||||
source "$CONFIG_FILE"
|
||||
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.
|
||||
if [ "$ENCRYPTION" == "TRUE" ]; then
|
||||
if [ -z "$PUBKEY" ]; then
|
||||
ERR_NO_KEY="TRUE"
|
||||
ENCRYPTION="FALSE"
|
||||
fi
|
||||
|
||||
#==============================================================================
|
||||
# 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
|
||||
command -v gpg >/dev/null 2>&1 ||\
|
||||
{ ERR_NO_GPG="TRUE"; ENCRYPTION="FALSE"; }
|
||||
fi
|
||||
|
||||
if [ -n "$LIST" ]; then
|
||||
list
|
||||
exit 0
|
||||
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
|
||||
|
||||
#All other functions require a note title and notebook.
|
||||
echo "$PROD_STR, $VER_STR"
|
||||
|
||||
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 [ -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
|
||||
#==============================================================================
|
||||
# Stage 2: Argument Parsing
|
||||
#==============================================================================
|
||||
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" = "-i" ] || [ "$ARG" == "--init" ]; 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 a note title was specified, but no notebook, and the list option
|
||||
# was specified, assume that the detected note title is actually the
|
||||
# name of a notebook
|
||||
NOTEBOOK="$NAME"
|
||||
NAME=""
|
||||
fi
|
||||
fi
|
||||
# w_conf and help are called here to avoid excess stage 3 code.
|
||||
#==============================================================================
|
||||
# Section: Actions / Stage 3
|
||||
#==============================================================================
|
||||
# All options not requiring at least a notebook to be specified have been dealt
|
||||
# with; if one isn't specified, throw code 30.
|
||||
if [ -z "$NOTEBOOK" ]; then
|
||||
printf "\n%s\n %s\n" "ERROR: Insufficient arguments:" "Notebook not specified"
|
||||
exit 30
|
||||
fi
|
||||
# List is the only option requiring only a notebook.
|
||||
# If list isn't the selected option and no note name was specified, throw code 30.
|
||||
if [ "$LIST" == TRUE ]; then
|
||||
list
|
||||
exit 0
|
||||
elif [ -z "$NAME" ]; then
|
||||
printf "\n%s\n %s\n" "ERROR: Insufficient arguments:" "Notebook not specified"
|
||||
exit 30
|
||||
fi
|
||||
|
||||
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 [ "$ENCRYPTION" == "TRUE" ]; then
|
||||
SESSION_ID="$RANDOM" #SESSION_ID later becomes the temporary filename
|
||||
readonly NOTE="$NOTE_DIR/$NAME.$EXT.gpg"
|
||||
else
|
||||
readonly NOTE="$NOTE_DIR/$NAME.$EXT"
|
||||
echo "$NOTE"
|
||||
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
|
||||
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
|
||||
#==============================================================================
|
||||
#==============================================================================
|
||||
# End Section: Actions / Stage 3
|
||||
#==============================================================================
|
||||
|
||||
Reference in New Issue
Block a user