v2.0a3 -> v2.0a4

Rearranged tree to be more sane.
Made formatting changes to stage 2.
Fixed a bug where edit would be bypassed if the note was just created.
This commit is contained in:
Jon-William Lewis
2015-05-07 21:23:12 -05:00
parent a0ad2e7d48
commit 64e504ebcb
11 changed files with 170 additions and 109 deletions

135
sns.sh Normal file → Executable file
View File

@@ -9,13 +9,25 @@ VER_STR="v2.0a3"
# Section: Functions
function init_default_config() {
if [ -z "$ROOTDIR" ]; then
ROOTDIR=$HOME/.sns
fi
if [ -z "$BASEDIR" ]; then
BASEDIR=$ROOTDIR/notes
fi
if [ -z "$EXT" ]; then
EXT=note
fi
if [ -z "$EDITOR" ]; then
EDITOR=vim
fi
if [ -z "$ENC_KEY" ]; then
ENCRYPTION="FALSE"
else
ENCRYPTION="TRUE"
fi
}
function writeconf {
function w_conf {
cat > $HOME/.sns/sns.conf << EOF
#==========================================================
# Simple Note System Config, v2.0a1
@@ -62,33 +74,50 @@ function help {
echo " -w | --wconf : Write default configuration to ~/.sns (useful for Encryption)"
echo ""
}
function p_header(){
printf "TITLE: $NAME\nDATE: $(date)\n"
}
#function create(){
#
# #Check if note exists
# 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
# echo " openssl enc -aes-256-cbc -salt -in $NOTE -out $NOTE.enc -pass pass:$ENC_KEY"
# fi
# fi
# fi
#}
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
if [ -e "$NOTE" -o -e ${NOTE%.*} ]; then
printf "\nERROR: Note already exists\nHint: use -e to edit the note.\n"
exit
elif [ -z "$ENCRYPTION" ]; then
echo "TITLE: $NAME" > $NOTE
echo "DATE: $(date)" >> $NOTE
elif [ "$ENCRYPTION" == "TRUE" ]; then
mkdir -p "$NOTEDIR"
touch "$NOTE"
echo "$(p_header)" | openssl enc -aes-256-cbc -salt -out "$NOTE"\
-pass pass:$ENC_KEY
#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
echo "$NOTE"
}
function delete(){
if [ "$DELETE" == "TRUE" ]; then
@@ -109,30 +138,29 @@ function delete(){
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 [ -r "$NOTE" -o -r ${NOTE%.*} ]; then
#Pre-Processing (Encryption)
if [ -z "$CREATE" ]; then
if [ "$ENCRYPTION" == "TRUE" ]; then
openssl enc -aes-256-cbc -salt -in $NOTE -out ${NOTE%.*} -pass pass:$ENC_KEY
rm $NOTE
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
$EDITOR $TMP_NAME
else
echo "" >> $NOTE
echo "EDIT $(date)" >> $NOTE
$EDITOR $NOTE
fi
else
echo ""
echo "ERROR: Note cannot be opened for editting."
echo ""
fi
#Post-Processing (Encryption)
if [ "$ENCRYPTION" == "TRUE" ]; then
openssl enc -aes-256-cbc -salt -in $TMP_NAME -out $NOTE -pass pass:$ENC_KEY
rm "$TMP_NAME"
fi
else
printf "\nERROR: Note cannot be opened for editting.\n"
fi
}
function print(){
@@ -203,7 +231,7 @@ fi
if [ "$ENCRYPTION" == "TRUE" ]; then
PROD_STR="Simple Note System (Encryption Enabled)"
EXT="$EXT.enc"
EXT="$EXT"
if [ ! -d ~/.sns/tmp ]; then
mkdir -p ~/.sns/tmp
fi
@@ -222,7 +250,7 @@ pause
fi
#==============================================================================
# End Section: Read Configuration
# End Section: Configuration
#==============================================================================
#==============================================================================
# Section: Argument Parsing
@@ -245,7 +273,7 @@ else #Assume the user wants to do something.
elif [ "$ARG" = "-p" -o "$ARG" = "--print" ]; then PRINT="TRUE"
elif [ "$ARG" = "-l" -o "$ARG" = "--list" ]; then LIST="TRUE"
elif [ "$ARG" = "-h" -o "$ARG" == "--help" ]; then help; exit 0
elif [ "$ARG" = "-w" -o "$ARG" == "--wconf" ]; then writeconf; exit
elif [ "$ARG" = "-w" -o "$ARG" == "--wconf" ]; then w_conf; exit
else
if [ -z "$NAME" -a -n $ARG ]; then
NAME=$ARG
@@ -262,7 +290,7 @@ else #Assume the user wants to do something.
fi
fi
# Note: writeconf has highest priority, and it is the only function that can
# Note: w_conf has highest priority, and it is the only function that can
# work without any parameters.
#==============================================================================
@@ -295,6 +323,7 @@ fi
NOTEDIR=$BASEDIR/$NOTEBOOK/$SECTION/
NOTE=$NOTEDIR$NAME.$EXT
if [ "$ENCRYPTION" == "TRUE" ]; then NOTE=$NOTE.tmp fi
if [ "$ENCRYPTION" == "TRUE" ]; then NOTE=$NOTE.enc; fi
if [ "$PRINT" == "TRUE" ]; then print; exit 0; fi
if [ "$CREATE" == "TRUE" ]; then create; fi
if [ "$EDIT" == "TRUE" ]; then edit; fi