Testing:
Print function works
This commit is contained in:
Jon-William Lewis
2015-03-18 14:53:05 -05:00
parent 45717e33b5
commit a0ad2e7d48
16 changed files with 389 additions and 398 deletions

371
sns.sh
View File

@@ -1,15 +1,20 @@
#!/bin/bash
#==========================================================
# Simple Note System, v2.0a1
# Simple Note System, v2.0a3
# Copyright 2014, Xenese Labs/Sicron-Perion XNF
#==========================================================
PROD_STR="Simple Note System"
VER_STR=v2.0a1
VER_STR="v2.0a3"
#==============================================================================
# Section: Helper Functions
#==============================================================================
# Section: Functions
function init_default_config() {
ROOTDIR=$HOME/.sns
BASEDIR=$ROOTDIR/notes
EXT=note
EDITOR=vim
ENCRYPTION="FALSE"
}
function writeconf {
cat > $HOME/.sns/sns.conf << EOF
#==========================================================
@@ -35,43 +40,157 @@ EOF
chmod 600 $ROOTDIR/sns.conf
}
function pause {
read -p " Press [Enter] to continue."
echo ""
read -p " Press [Enter] to continue."
echo ""
}
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 "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 " -p | --print : Print note to console"
echo " -l | --list : List all notes in NOTEBOOK"
echo " -w | --wconf : Write default configuration to ~/.sns (useful for Encryption)"
echo ""
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 : Write default configuration to ~/.sns (useful for Encryption)"
echo ""
}
function create(){
if [ -z "$CREATE" -a -z "$EDIT" -a -z "$PRINT" ]; then #If no action specified, print help and exit
help
exit
elif [ "$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
#==============================================================================
# End Section: Helper Functions
#==============================================================================
#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
}
function 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
}
function edit (){
if [ "$EDIT" == "TRUE" ]; then
if [ -r "$NOTE" -o -r ${NOTE%.*} ]; then
if [ -z "$CREATE" ]; then
if [ "$ENCRYPTION" == "TRUE" ]; then
TMP_NAME=$ROOTDIR/tmp/$RANDOM
openssl enc -d -aes-256-cbc -in ${NOTE%.*} -pass pass:$ENC_KEY > $TMP_NAME
echo "" >> $TMP_NAME
echo "EDIT $(date)" >> $TMP_NAME
else
echo "" >> $NOTE
echo "EDIT $(date)" >> $NOTE
fi
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
}
function print(){
if [ "$PRINT" == "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
else
cat $NOTE
echo "" >> $NOTE
fi
else
echo ""
echo "ERROR: Note cannot be found."
echo ""
fi
fi
fi
}
function list(){
if [ -d "$BASEDIR"/"$NOTEBOOK" ]; then
echo ""
printf "Notes in $(basename $NOTEBOOK):"
echo ""
NOTES=( $(find $BASEDIR/$NOTEBOOK -name "*.$EXT" -print0 | sed s:$BASEDIR/$NOTEBOOK/:" ":g | sed -e s:".$EXT"::g | 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
}
# End Section: Functions
#==============================================================================
# Section: Configuration
#==============================================================================
if [ -r $HOME/.sns/sns.conf ]; then
source $HOME/.sns/sns.conf
source $HOME/.sns/sns.conf
else
ROOTDIR=$HOME/.sns
BASEDIR=$ROOTDIR/notes
EDITOR=vim
EXT=note
init_default_config
fi
if [ "$ENCRYPTION" == "TRUE" ]; then
@@ -115,7 +234,7 @@ SECTION=""
if [ -z "$1" ]; then #If no input was given, print help
help
exit
exit 20
else #Assume the user wants to do something.
ARGS=( "$@" )
for ARG in ${ARGS[@]};do
@@ -130,184 +249,52 @@ else #Assume the user wants to do something.
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
if [ -n "$NAME" -a -z "$NOTEBOOK" ]; then
NOTEBOOK="$NAME"
NAME=""
fi
fi
# Note: writeconf has highest priority, and it is the only function that can
# work without any parameters.
#==============================================================================
# End Section: Argument Parsing
#==============================================================================
# Help requires no arguments, and is exclusive.
if [ -n "$HELP" ]; then help; exit 0; fi
# If no name or notebook is specified, the create, edit, and print functions will not function properly.
# Check $NAME and $NOTEBOOK and exit if either are empty.
if [ -z "$NAME" -o -z "$NOTEBOOK" ]; then;
echo " ERROR: Insufficient arguments";
help; #Remind the user how to use the script
exit 10;
# 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
NOTEDIR=$BASEDIR/$NOTEBOOK/$SECTION/
NOTE=$NOTEDIR$NAME.$EXT
if [ "$ENCRYPTION" == "TRUE" ]; then
NOTE=$NOTE.tmp
fi
#==========================================================================
# Subection: Create
#==========================================================================
if [ -z "$CREATE" -a -z "$EDIT" -a -z "$PRINT" ]; then #If no action specified, print help and exit
help
exit
else
if [ "$ENCRYPTION" == "TRUE" ]; then NOTE=$NOTE.tmp fi
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
# delete.sns.sh
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: Edit
#==========================================================================
if [ "$EDIT" == "TRUE" ]; then
if [ -r "$NOTE" -o -r ${NOTE%.*} ]; then
if [ -z "$CREATE" ]; then
if [ "$ENCRYPTION" == "TRUE" ]; then
TMP_NAME=$ROOTDIR/tmp/$RANDOM
openssl enc -d -aes-256-cbc -in ${NOTE%.*} -pass pass:$ENC_KEY > $TMP_NAME
echo "" >> $TMP_NAME
echo "EDIT $(date)" >> $TMP_NAME
else
echo "" >> $NOTE
echo "EDIT $(date)" >> $NOTE
fi
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
#==========================================================================
# Subection: Print
#==========================================================================
if [ "$PRINT" == "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
else
cat $NOTE
echo "" >> $NOTE
fi
else
echo ""
echo "ERROR: Note cannot be found."
echo ""
fi
fi
fi
# list.sns.sh
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 ""
NOTES=( $(find $BASEDIR/$NOTEBOOK -name "*.$EXT" -print0 | sed s:$BASEDIR/$NOTEBOOK/:" ":g | sed -e s:".$EXT"::g | 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