Added old SNS script

This commit is contained in:
Jon-William Lewis
2019-04-08 08:58:20 -05:00
parent 6077825431
commit 7ecc9e0361
6 changed files with 316 additions and 494 deletions

311
sns.sh Normal file
View File

@@ -0,0 +1,311 @@
#!/bin/bash
#==========================================================
# Simple Note System, v1.0
# Copyright 2014, Xenese Labs/Sicron-Perion XNF
#==========================================================
PROD_STR="Simple Note System"
VER_STR=v1.0
#==============================================================================
# Section: Helper Functions
#==============================================================================
function writeconf {
cat > $HOME/.sns << EOF
#==========================================================
# Simple Note System Config, v1.0
# Copyright 2014, Xenese Labs/Sicron-Perion XNF
#==========================================================
#Directory where notes will be stored
BASEDIR=$HOME/notes
#File extension to use (for listing notes)
EXT=note
#Preferred Editor
EDITOR=vim
#Encryption
#WARNING: ANY PREVIOUSLY UNENCRYPTED NOTES WILL BE LOST
ENCRYPTION="FALSE"
ENC_KEY=""
EOF
chmod 600 $HOME/.sns
}
function help {
echo ""
echo "usage: sns [-ce] NAME NOTEBOOK SECTION"
echo " sns [-d ] NAME NOTEBOOK SECTION"
echo " sns [-l ] NOTEBOOK"
echo " sns [-w ]"
echo ""
echo " -c | --create : Create note"
echo " -d | --delete : Delete note"
echo " -e | --edit : Open note for editing"
echo " -l | --list : List all notes in NOTEBOOK"
echo " -w | --wconf : Write default configuration to ~/.sns (useful for Encryption)"
echo ""
}
function pause {
read -p " Press [Enter] to continue."
echo ""
}
#==============================================================================
# End Section: Helper Functions
#==============================================================================
#==============================================================================
# Section: Configuration
#==============================================================================
if [ -r $HOME/.sns ]; then
source $HOME/.sns
else
BASEDIR=$HOME/notes
EDITOR=vim
EXT=note
fi
if [ "$ENCRYPTION" == "TRUE" ]; then
if [ -z "$ENC_KEY" ]; then
ERR_NO_KEY="TRUE"
ENCRYPTION="FALSE"
pause
fi
command -v openssl >/dev/null 2>&1 || { ERR_NO_SSL="TRUE"; ENCRYPTION="FALSE"; }
fi
if [ "$ENCRYPTION" == "TRUE" ]; then
PROD_STR="Simple Note System (Encryption Enabled)"
EXT="$EXT.enc"
if [ ! -d ~/.tmp ]; then
mkdir -p ~/.tmp
fi
fi
echo "$PROD_STR, $VER_STR"
if [ -n "$ERR_NO_SSL" ]; then
echo >&2 " Warning: OpenSSL not installed. Encryption disabled."
fi
if [ -n "$ERR_NO_KEY" ]; then
echo " Warning: No encryption key was provided. Encryption disabled."
fi
if [ -n "$ERR_NO_SSL" -o -n "$ERR_NO_KEY" ]; then
pause
fi
#==============================================================================
# End Section: Read Configuration
#==============================================================================
#==============================================================================
# Section: Argument Parsing
#==============================================================================
NAME=""
NOTEBOOK=""
SECTION=""
if [ -z "$1" ]; then #If no input was given, print help
help
exit
else #Assume the user wants to do something.
ARGS=( "$@" )
for ARG in ${ARGS[@]};do
if [ "$ARG" = "-c" -o $ARG == "--create" ]; then
CREATE="TRUE"
elif [ "$ARG" = "-d" -o $ARG == "--delete" ]; then
DELETE="TRUE"
elif [ "$ARG" = "-e" -o $ARG == "--edit" ]; then
EDIT="TRUE"
elif [ "$ARG" = "-ce" -o $ARG == "-ec" ]; then
EDIT="TRUE"
CREATE="TRUE"
elif [ "$ARG" == "-l" -o "$ARG" == "--list" ]; then #If user asks for list, print list
LIST="TRUE"
elif [ "$ARG" == "-h" -o "$ARG" == "--help" ]; then #If user asks for help, print help
help
exit
elif [ "$ARG" == "-w" -o "$ARG" == "--wconf" ]; then #If user wants to write config, write config
writeconf
exit
else
if [ -z "$NAME" -a -n $ARG ]; then
NAME=$ARG
echo "Name: $NAME"
elif [ -z "$NOTEBOOK" -a -n $ARG ]; then
NOTEBOOK=$ARG
echo "Notebook: $NOTEBOOK"
elif [ -z "$SECTION" -a -n $ARG ]; then
SECTION=$ARG/
echo "Section: $SECTION"
fi
fi
done
fi
#==============================================================================
# End Section: Argument Parsing
#==============================================================================
#==============================================================================
# Section: Main
#==============================================================================
NOTEDIR=$BASEDIR/$NOTEBOOK/$SECTION
NOTE=$NOTEDIR$NAME.$EXT
if [ "$ENCRYPTION" == "TRUE" ]; then
NOTE=$NOTE.tmp
fi
#==========================================================================
# Subection: List
#==========================================================================
if [ -n "$LIST" ]; then
NOTEBOOK="$NAME" #In case of a list command, arg parsing fails.
if [ -z "$NOTEBOOK" ]; then
echo " ERROR: Insufficient arguments"
help
exit
else
if [ -d "$BASEDIR"/"$NOTEBOOK" ]; then
echo ""
printf "Notes in $(basename $NOTEBOOK):"
echo ""
#find $BASEDIR/$NOTEBOOK -name "*.$EXT" -print0 | sed s:$BASEDIR/$NOTEBOOK/:" ":g | sed -e s:$EXT::g | tr "." " "
NOTES=( $(find $BASEDIR/$NOTEBOOK -name "*.$EXT" -print0 | sed s:$BASEDIR/$NOTEBOOK/:" ":g | sed -e s:$EXT::g | tr "." " " | tr "/" " ") )
let i=0
for NOTE in ${NOTES[@]}; do
if [ -d $BASEDIR/$NOTEBOOK/$NOTE ]; then
if [ "$LAST_SECTION" != "$NOTE" ]; then
printf " Section: $NOTE\n"
fi
LAST_SECTION=$NOTE
else
#if [ $(($i % 1)) -eq 0 ]; then
# printf "\n "
#fi
printf " $NOTE\n"
fi
let i++
done
printf "\n"
else
echo ""
echo "ERROR: Notebook $NOTEBOOK does not exist."
echo ""
fi
fi
exit
fi
#======================================================================
# Sanity Check - Actions below require a valid $NAME and $NOTEBOOK
#======================================================================
if [ -z "$NAME" -o -z "$NOTEBOOK" ]; then
if [ -z "$NAME" -o -z "$NOTEBOOK" ]; then
echo " ERROR: Insufficient arguments"
help
exit
fi
fi
#==========================================================================
# Subection: Delete
#==========================================================================
if [ "$DELETE" == "TRUE" ]; then
if [ -e $NOTE -o -e ${NOTE%.*} ]; then
if [ "$ENCRYPTION" == "TRUE" ]; then
rm ${NOTE%.*}
else
rm $NOTE
fi
echo ""
echo "Deleted note: $NOTEBOOK/$SECTION$NAME."
exit
else
echo ""
echo "ERROR: Note $NOTEBOOK/$SECTION$NAME does not exist."
exit
fi
fi
#==========================================================================
# Subection: Create
#==========================================================================
if [ -z "$CREATE" -a -z "$EDIT" ]; then #If no action specified, print help and exit
help
exit
else
if [ "$CREATE" == "TRUE" ]; then
if [ -e $NOTE -o -e ${NOTE%.*} ]; then
echo ""
echo "ERROR: Note already exists"
echo "Hint: use -e to edit the note."
echo ""
exit
else
#Create any necessary folders
mkdir -p $NOTEDIR
#Fill in title
echo "TITLE: $NAME" > $NOTE
#Fill the second line with the date
echo "DATE: $(date)" >> $NOTE
if [ "$ENCRYPTION" == "TRUE" ]; then
if [ $EDIT == "FALSE" ]; then
openssl enc -aes-256-cbc -salt -in $NOTE -out ${NOTE%.*} -pass pass:$ENC_KEY
fi
fi
fi
fi
#==========================================================================
# Subection: Edit
#==========================================================================
if [ "$EDIT" == "TRUE" ]; then
if [ -r "$NOTE" -o -r ${NOTE%.*} ]; then
if [ -z "$CREATE" ]; then
if [ "$ENCRYPTION" == "TRUE" ]; then
openssl enc -d -aes-256-cbc -in ${NOTE%.*} -pass pass:$ENC_KEY > $NOTE
fi
echo "" >> $NOTE
echo "EDIT $(date)" >> $NOTE
fi
$EDITOR $NOTE
if [ "$ENCRYPTION" == "TRUE" ]; then
openssl enc -aes-256-cbc -salt -in $NOTE -out ${NOTE%.*} -pass pass:$ENC_KEY
rm $NOTE
fi
else
echo ""
echo "ERROR: Note cannot be opened for editting."
echo ""
fi
fi
fi
exit