diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000..8f18d2b Binary files /dev/null and b/.DS_Store differ diff --git a/build.sh b/build.sh index aad3dcc..77dd483 100755 --- a/build.sh +++ b/build.sh @@ -1,3 +1,9 @@ +#!/bin/bash +#========================================================== +# Simple Note System - Build Script +# Copyright 2016, Xenese Labs/Sicron-Perion XNF +#========================================================== + S=sns.sh bash header.sh > "$S" @@ -13,15 +19,6 @@ cat ./src/includes/edit.sns.sh >> "$S" cat ./src/includes/print.sns.sh >> "$S" cat ./src/includes/list.sns.sh >> "$S" printf "%s\n\n\n\n" "# End Section: Functions" >> "$S" - -printf "\n" >> "$S" -printf "%s\n" "###############################################################################" >> "$S" -printf "%s\n" "#==============================================================================" >> "$S" -printf "%s\n" "# Begin Main Program" >> "$S" -printf "%s\n" "#==============================================================================" >> "$S" -printf "%s\n" "###############################################################################" >> "$S" -printf "\n" >> "$S" - cat ./src/main/stage1.sns.sh >> "$S" cat ./src/main/stage2.sns.sh >> "$S" cat ./src/main/stage3.sns.sh >> "$S" diff --git a/errors.ref b/errors.ref index 508077d..620ffb6 100644 --- a/errors.ref +++ b/errors.ref @@ -9,4 +9,7 @@ ERR_INSUFFICIENT_ARGS 30 A required argument was not provided Encryption---------------------------------------------------------------------- ERR_NO_GPG 100 GPG is not installed -ERR_NO_KEY 110 No recipient specified \ No newline at end of file +ERR_NO_KEY 110 No recipient specified + +Create-------------------------------------------------------------------------- +ERR_NOTE_EXiSTS 200 The specified note already exists \ No newline at end of file diff --git a/header.sh b/header.sh index 6ba4564..cd39b16 100644 --- a/header.sh +++ b/header.sh @@ -1,13 +1,12 @@ -#!/bin/bash - PROD_STR="Simple Note System" VER_STR="v2.0a8" +YEAR=2016 cat << EOF #!/bin/bash #========================================================== # $PROD_STR, $VER_STR -# Copyright 2014, Xenese Labs/Sicron-Perion XNF +# Copyright $YEAR, Xenese Labs/Sicron-Perion XNF #========================================================== if [ -z "\$HOME" ]; then HOME=/home/"\$(whoami)"; fi @@ -19,4 +18,5 @@ readonly NOTES_DIR="\$ROOT_DIR"/notes readonly TMP_DIR="\$ROOT_DIR"/tmp readonly CONFIG_FILE="\$ROOT_DIR/sns.conf" + EOF diff --git a/sns.sh b/sns.sh index a2be9c9..8beb03f 100755 --- a/sns.sh +++ b/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 -#============================================================================== \ No newline at end of file + #============================================================================== + # End Section: Actions / Stage 3 + #============================================================================== diff --git a/sns.xcodeproj/project.pbxproj b/sns.xcodeproj/project.pbxproj index 5eb29b4..d55d3e0 100644 --- a/sns.xcodeproj/project.pbxproj +++ b/sns.xcodeproj/project.pbxproj @@ -15,11 +15,12 @@ 5D22D6A81AFC4F5A0036DC52 /* p_header.sh */ = {isa = PBXFileReference; lastKnownFileType = text.script.sh; path = p_header.sh; sourceTree = ""; }; 5D22D6A91AFC4F5A0036DC52 /* pause.sns.sh */ = {isa = PBXFileReference; lastKnownFileType = text.script.sh; path = pause.sns.sh; sourceTree = ""; }; 5D22D6AA1AFC4F5A0036DC52 /* print.sns.sh */ = {isa = PBXFileReference; lastKnownFileType = text.script.sh; path = print.sns.sh; sourceTree = ""; }; - 5D22D6AB1AFC4F5A0036DC52 /* create_sns_root.sns.sh */ = {isa = PBXFileReference; lastKnownFileType = text.script.sh; path = create_sns_root.sns.sh; sourceTree = ""; }; + 5D22D6AB1AFC4F5A0036DC52 /* init_store.sns.sh */ = {isa = PBXFileReference; lastKnownFileType = text.script.sh; path = init_store.sns.sh; sourceTree = ""; }; 5D22D6AD1AFC4F5A0036DC52 /* stage1.sns.sh */ = {isa = PBXFileReference; lastKnownFileType = text.script.sh; path = stage1.sns.sh; sourceTree = ""; }; 5D22D6AE1AFC4F5A0036DC52 /* stage2.sns.sh */ = {isa = PBXFileReference; lastKnownFileType = text.script.sh; path = stage2.sns.sh; sourceTree = ""; }; 5D22D6AF1AFC4F5A0036DC52 /* stage3.sns.sh */ = {isa = PBXFileReference; lastKnownFileType = text.script.sh; path = stage3.sns.sh; sourceTree = ""; }; 5D22D6B01AFC5B100036DC52 /* libencryption.sh */ = {isa = PBXFileReference; lastKnownFileType = text.script.sh; path = libencryption.sh; sourceTree = ""; }; + 5D75D24F1C5F13DF001E7B33 /* verify_store.sh */ = {isa = PBXFileReference; lastKnownFileType = text.script.sh; path = verify_store.sh; sourceTree = ""; }; 5D7E611F1AB74D33001D49B9 /* build.sh */ = {isa = PBXFileReference; lastKnownFileType = text.script.sh; path = build.sh; sourceTree = ""; }; 5D7E91FB1B27FB620030B30D /* header.sh */ = {isa = PBXFileReference; lastKnownFileType = text.script.sh; path = header.sh; sourceTree = ""; }; 5DE839831AB9DACE006CB4F6 /* sns.sh */ = {isa = PBXFileReference; lastKnownFileType = text.script.sh; path = sns.sh; sourceTree = ""; }; @@ -47,8 +48,9 @@ 5D22D6A81AFC4F5A0036DC52 /* p_header.sh */, 5D22D6A91AFC4F5A0036DC52 /* pause.sns.sh */, 5D22D6AA1AFC4F5A0036DC52 /* print.sns.sh */, - 5D22D6AB1AFC4F5A0036DC52 /* create_sns_root.sns.sh */, + 5D22D6AB1AFC4F5A0036DC52 /* init_store.sns.sh */, 5D22D6B01AFC5B100036DC52 /* libencryption.sh */, + 5D75D24F1C5F13DF001E7B33 /* verify_store.sh */, ); path = includes; sourceTree = ""; diff --git a/sns.xcodeproj/project.xcworkspace/xcuserdata/xilmwa.xcuserdatad/UserInterfaceState.xcuserstate b/sns.xcodeproj/project.xcworkspace/xcuserdata/xilmwa.xcuserdatad/UserInterfaceState.xcuserstate new file mode 100644 index 0000000..137df62 Binary files /dev/null and b/sns.xcodeproj/project.xcworkspace/xcuserdata/xilmwa.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/src/includes/create.sns.sh b/src/includes/create.sns.sh index 54de245..a0549ed 100644 --- a/src/includes/create.sns.sh +++ b/src/includes/create.sns.sh @@ -1,22 +1,31 @@ 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 + # Depends : p_header + # Requires: $NOTE, $NOTE_DIR, $NOTEBOOK, $SECTION, $NAME + # Optional: $ENCRYPTION, $SESSION_ID, $TMP_DIR encrypt + # Given a valid setup, create writes the standard note header as specified + # by p_header, to $NOTE. - if [ -z "$ENCRYPTION" ]; then - echo "TITLE: $NAME" > "$NOTE" - echo "DATE: $(date)" >> "$NOTE" - elif [ "$ENCRYPTION" == "TRUE" ]; then + # Refuse to overwrite a note + if [ -e "$NOTE" ]; then + printf "\nERROR: Note already exists\nHint: use -e to edit the note.\n" + exit 200 + # If the notebook doesn't exist, create it. + elif [ ! -d "$NOTE_DIR" ]; then + mkdir -p "$NOTE_DIR" + fi + + # Write the standard note header + if [ "$ENCRYPTION" == "TRUE" ]; then TMP_NOTE="$TMP_DIR"/"$SESSION_ID" p_header > "$TMP_NOTE" encrypt - fi - - if [ -e "$NOTE" ]; then + else + p_header > "$NOTE" + fi + # Make sure the note exists, and inform the user. + if [ -e "$NOTE" ]; then echo "Created note: $NOTEBOOK/$SECTION/$NAME." - fi - + else + printf "%s\n" "Something went wrong." + fi } diff --git a/src/includes/delete.sns.sh b/src/includes/delete.sns.sh index f0b47dc..696d9d1 100644 --- a/src/includes/delete.sns.sh +++ b/src/includes/delete.sns.sh @@ -1,14 +1,10 @@ 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 + #Requires: $NOTE, $NOTEBOOK, $SECTION, $NAME + # Given a valid $NOTE, delete removes $NOTE from sns. + if [ -e "$NOTE" ]; then + rm "$NOTE" + printf "\n%s\n" "Deleted note: $NOTEBOOK/$SECTION/$NAME." + else + printf "\n%s\n" "ERROR: Note $NOTEBOOK/$SECTION/$NAME does not exist." + fi } diff --git a/src/includes/edit.sns.sh b/src/includes/edit.sns.sh index 91f0e7f..54702c5 100644 --- a/src/includes/edit.sns.sh +++ b/src/includes/edit.sns.sh @@ -1,15 +1,20 @@ function edit(){ +# Requires: $EDITOR, $NOTE +# Optional: $ENCRYPTION, $TMP_DIR, $SESSION_ID, decrypt, encrypt + +# Verify an editor was specified if [ -z "$EDITOR" ]; then >&2 echo "Error no editor specified in environment." exit +# Verify the note exists elif [ ! -r "$NOTE" ]; then echo "ERROR: Note cannot be opened for editing." exit 40; fi - +# When encryption is enabled, decrypt $NOTE to a temp file if [ "$ENCRYPTION" == "TRUE" ]; then - cp "$NOTE" "$NOTE".bk - if [ ! -d "$ROOT_DIR"/tmp ]; then mkdir "$ROOT_DIR"/tmp; fi + cp "$NOTE" "$NOTE".bk #Insurance + if [ ! -d "$TMP_DIR" ]; then mkdir "$TMP_DIR"; fi TMP_NOTE="$TMP_DIR/$SESSION_ID" decrypt > "$TMP_NOTE" else TMP_NOTE="$NOTE"; fi diff --git a/src/includes/verify_store.sh b/src/includes/verify_store.sh index 1e75289..f4fc06f 100644 --- a/src/includes/verify_store.sh +++ b/src/includes/verify_store.sh @@ -1,7 +1,6 @@ -#!/bin/sh - -# verify_store.sh -# sns -# -# Created by Jon-William Lewis on 1/31/16. -# +function verify_store { + STORE_DIRS=("$ROOT_DIR" "$NOTES_DIR" "$TMP_DIR") + for DIR in ${STORE_DIRS[]}; do + mkdir -p "$DIR" + done +} \ No newline at end of file diff --git a/src/main/stage1.sns.sh b/src/main/stage1.sns.sh index d0eadd1..b2ddef2 100644 --- a/src/main/stage1.sns.sh +++ b/src/main/stage1.sns.sh @@ -1,41 +1,41 @@ -#============================================================================== -# Section: Configuration / Stage 1 -#============================================================================== -if [ -r "$CONFIG_FILE" ]; then - source "$CONFIG_FILE" -else - create_sns_root - source "$CONFIG_FILE" -fi + #++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + # Entry Point + #++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + #============================================================================== + # Stage 1: Read Configuration / Verify Integrity + #============================================================================== + if [ -r "$CONFIG_FILE" ]; then + source "$CONFIG_FILE" + else + init_store + source "$CONFIG_FILE" + fi -if [ "$ENCRYPTION" == "TRUE" ]; then - if [ -z "$PUBKEY" ]; then - ERR_NO_KEY="TRUE" - ENCRYPTION="FALSE" - fi + verify_store - command -v gpg >/dev/null 2>&1 ||\ - { ERR_NO_GPG="TRUE"; ENCRYPTION="FALSE"; } -fi + if [ "$ENCRYPTION" == "TRUE" ]; then + if [ -z "$PUBKEY" ]; then + ERR_NO_KEY="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 + command -v gpg >/dev/null 2>&1 ||\ + { ERR_NO_GPG="TRUE"; ENCRYPTION="FALSE"; } + fi -echo "$PROD_STR, $VER_STR" + if [ "$ENCRYPTION" == "TRUE" ]; then + PROD_STR="Simple Note System (Encryption Enabled)" + if [ ! -d "$ROOT_DIR"/tmp ]; then + mkdir -p "$ROOT_DIR"/tmp + fi + fi -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 + echo "$PROD_STR, $VER_STR" - -#============================================================================== -# End Section: Configuration / Stage 1 -#============================================================================== + 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 diff --git a/src/main/stage2.sns.sh b/src/main/stage2.sns.sh index b62b775..0fcc5fc 100644 --- a/src/main/stage2.sns.sh +++ b/src/main/stage2.sns.sh @@ -1,41 +1,34 @@ -#============================================================================== -# 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" = "-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" + #============================================================================== + # 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 - 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 -#============================================================================== + # w_conf and help are called here to avoid excess stage 3 code. diff --git a/src/main/stage3.sns.sh b/src/main/stage3.sns.sh index da7f738..67deb52 100644 --- a/src/main/stage3.sns.sh +++ b/src/main/stage3.sns.sh @@ -1,39 +1,37 @@ -#============================================================================== -# 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"/ + #============================================================================== + # 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" ]; + 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" + 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 -#============================================================================== \ No newline at end of file + #============================================================================== + # End Section: Actions / Stage 3 + #==============================================================================