This commit is contained in:
Jon-William Lewis
2016-08-11 16:02:08 -05:00
parent 47c697be24
commit 7438806847
13 changed files with 198 additions and 133 deletions

View File

@@ -73,12 +73,23 @@ properties of SNS:
SNS requires the identifier of a GPG public key for encryption to function.
**A word about encryption**: Enabling encryption will cause problems if
previously un-encrypted notes exist. In the future, I'd like to resolve these,
however for the time being it's best to decide when you install SNS if you'd
like to enable encryption or not.
**Encryption amendment**: As of SNS 2a10, experimental support is in place for
migrating between encrypted and unencrypted note stores.
* Version Control program
SNS can be configured to use a version control program such as Git,
Mercurial, Subversion, etc. When configured, SNS can pass commands through
to the specified program in the context of SNS's store. For example, if Git
was configured here, then `sns git init` would initialize a Git repository
for your notes; `sns git remote add` could be used to add a remote repo, and
`sns git push` could push committed changes to the notes store to that
remote.
**A word about encryption:**
When editing an encrypted note, SNS will decrypt
the note to a temporary file inside its store. The temporary file will have a
random name, but a predictable location. It can only be read by the user and
root, and is deleted after editing, but can be recovered by forensic utilities.
SNS's encryption is mainly useful when the store is being _transferred over a
network_.
## Tips and Tricks
* To list all notes in all notebooks, use `sns -l .`

View File

@@ -19,19 +19,33 @@
# Color codes for messages
readonly RED_COLOR='\033[1;31m'
readonly YELLOW_COLOR='\033[1;33m'
readonly BLUE_COLOR='\033[1;34m'
readonly RESET_COLOR='\033[0m'
if [ "$(id -u)" != "0" ]; then
printf "$RED_COLOR!$RESET_COLOR - %s\n"\
"Please run as root or specify a PREFIX= you have write access to."
exit
# Set variables for header
PROD_STR="Simple Note System"
PROD_SUB="Install Script"
# Set default build directory if none specified
if [ ! -d "$BUILD_ROOT" ]; then BUILD_ROOT="build"; fi
# Set default install prefix according to platform
if [ -z "$INSTALL_DIR" ]; then
case "$(uname)" in
"Darwin" )
INSTALL_ROOT=/usr/local/opt
;;
*)
INSTALL_ROOT=/opt
;;
esac
INSTALL_DIR="$INSTALL_ROOT/sns"
fi
S="build/sns.sh"
function build {
bash header.sh
echo -e "\n# Section: Functions"
function sns_out {
# Compile SNS from source files and print the resulting script to stdout
cat ./src/includes/header.sh
printf "%s\n" "# Section: Functions"
cat ./src/includes/init_store.sns.sh
cat ./src/includes/verify_store.sns.sh
cat ./src/includes/help.sns.sh
@@ -47,129 +61,173 @@ function build {
cat ./src/main/stage2.sns.sh
cat ./src/main/stage3.sns.sh
}
function clean {
if [ -d "$BUILD_ROOT" ]; then rm -rf "$BUILD_ROOT"; fi
}
function build_sns {
clean
if [ ! -d ."$BUILD_ROOT" ]; then mkdir $BUILD_ROOT; fi
sns_out > "$BUILD_ROOT/sns"
cp -f src/bash-completion/sns ./build/bash-completion
}
function install {
mkdir -p "$PREFIX/bin"
mkdir -p "$PREFIX/etc/bash-completion.d"
chmod +x "$S"
function check_install_dir_perms {
STATS=( $(stat -c "%a %u %g" "$INSTALL_ROOT"))
O=$(echo "${STATS[1]}" | cut -c 1)
G=$(echo "${STATS[1]}" | cut -c 2)
E=$(echo "${STATS[1]}" | cut -c 3)
cp -f "$S" "$PREFIX/bin/sns"
cp -f ./build/bash-completion "$PREFIX/etc/bash-completion.d/sns"
USER=$(id -u)
GROUP=$(id -g)
if [ "${STATS[2]}" == "$USER" ] && [ "$O" == "7" ]; then WRITE="1";
elif [ "${STATS[3]}" == "$GROUP" ] && [ "$G" == "7" ]; then WRITE="1";
elif [ "$E" == "7" ]; then WRITE="1";
else WRITE="0";
fi
if [ "$(id -u)" == "0" ]; then WRITE=1; fi
}
function install_sns {
# Make sure we can write to "$INSTALL_DIR"
check_install_dir_perms
if [ "$WRITE" == "0" ]; then
printf "$RED_COLOR!$RESET_COLOR - %s %s\n"\
"Superuser permissions required to install to" "$INSTALL_DIR."
printf "$YELLOW_COLOR!$RESET_COLOR - %s\n"\
"Will use sudo as necessary."
USE_SUDO="sudo"
else
USE_SUDO=""
fi
# Prepare target environment
"$USE_SUDO" mkdir -p "$INSTALL_DIR"
"$USE_SUDO" mkdir -p "$INSTALL_DIR/bin"
"$USE_SUDO" mkdir -p "$INSTALL_DIR/etc/bash-completion.d"
# Install files from "$BUILD_ROOT" to "$INSTALL_DIR"
"$USE_SUDO" install -m 0755 "$BUILD_ROOT/sns" "$INSTALL_DIR/bin/sns"
"$USE_SUDO" install -m 0644 "$BUILD_ROOT/bash-completion" "$INSTALL_DIR/etc/bash-completion.d/sns"
}
function verify {
if [ ! -r "$PREFIX/bin/sns" ]; then
if [ ! -r "$INSTALL_DIR/bin/sns" ]; then
printf "$RED_COLOR!$RESET_COLOR - %s\n"\
"could not write to $PREFIX/bin/sns"
"could not write to $INSTALL_DIR/bin/sns"
exit 15
elif [ ! -r "$PREFIX"/etc/bash-completion.d/sns ]; then
printf "$RED_COLOR!$RESET_COLOR - %s\n"\
"could not write to $PREFIX/etc/bash-completion.d/sns"
exit 15
fi
if [ -z "$(which sns)" ]; then
printf "$YELLOW_COLOR*$RESET_COLOR - %s\n\t$YELLOW_COLOR%s\n$RESET_COLOR %s\n\n"\
"sns was not found in PATH. Please add"\
"export PATH=$PREFIX/bin:\$PATH"\
"to your ~/.bashrc or ~/.bash_profile"
elif [ "$(which sns)" != "$PREFIX/bin/sns" ]; then
printf "$RED_COLOR!$RESET_COLOR - %s\n\t%s\n %s\n\n"\
"Another installation of sns "\
"($(which sns))"\
" is conflicting with this one."
else
printf "$YELLOW_COLOR*$RESET_COLOR - %s\n"\
"Simple Note System was installed to $PREFIX/bin/sns successfully."
printf "$YELLOW_COLOR*$RESET_COLOR - %s\n"\
"Bash completion was installed to $PREFIX/etc/bash-completion.d/sns successfully."
printf "$YELLOW_COLOR*$RESET_COLOR - %s $BLUE_COLOR%s$RESET_COLOR\n"\
"Simple Note System was installed to" "$INSTALL_DIR/bin/sns."
SNS_INSTALLED=1
fi
if grep "source $PREFIX/etc/bash-completion.d/sns" < "$BASH_SETTINGS" > /dev/null; then
if [ ! -r "$INSTALL_DIR/etc/bash-completion.d/sns" ]; then
printf "$RED_COLOR!$RESET_COLOR - %s\n"\
"could not write to $INSTALL_DIR/etc/bash-completion.d/sns"
exit 15
else
printf "$YELLOW_COLOR*$RESET_COLOR - %s $BLUE_COLOR%s$RESET_COLOR\n"\
"Bash completion was installed to" "$INSTALL_DIR/etc/bash-completion.d/sns."
fi
DEFAULT_SNS="$(which sns 2>/dev/null)"
if [ -n "$DEFAULT_SNS" ] && [ "$DEFAULT_SNS" != "$INSTALL_DIR/bin/sns" ]; then
printf "$RED_COLOR!$RESET_COLOR - %s %s %s\n"\
"Another installation of sns"\
"($DEFAULT_SNS)"\
"was detected."
fi
if [ -z "$DEFAULT_SNS" ]; then
printf "\n$YELLOW_COLOR*$RESET_COLOR - %s\n\t$YELLOW_COLOR%s\n$RESET_COLOR %s\n\n"\
"sns was not found in PATH. Please add"\
"export PATH=$INSTALL_DIR/bin:\$PATH"\
" to your ~/.bashrc or ~/.bash_profile"
fi
if grep "source $INSTALL_DIR/etc/bash-completion.d/sns" < "$BASH_SETTINGS" > /dev/null; then
SNS_BASH_COMPLETION=1;
else
SNS_BASH_COMPLETION=0;
fi
if [ "$SNS_BASH_COMPLETION" != "1" ]; then
printf "$YELLOW_COLOR*$RESET_COLOR - %s\n\t$YELLOW_COLOR%s\n$RESET_COLOR %s\n\n"\
"to enable bash completion, add"\
"source $PREFIX/etc/bash-completion.d/sns"\
printf "\n$YELLOW_COLOR*$RESET_COLOR - %s\n\t$YELLOW_COLOR%s\n$RESET_COLOR %s\n\n"\
"To enable bash completion, add"\
"source $INSTALL_DIR/etc/bash-completion.d/sns"\
" to your ~/.bashrc or ~/.bash_profile"
else
printf "$YELLOW_COLOR*$RESET_COLOR - %s\n"\
"Bash completion already enabled."
fi
if [ "$SNS_INSTALLED" == "1" ]; then
printf "$YELLOW_COLOR*$RESET_COLOR - %s $BLUE_COLOR%s$RESET_COLOR %s\n"\
"Installation succeeded. You should run" "sns -i" "now."
fi
}
function print_help {
printf "\n%s" "usage: ./install [bchiu]"
printf " %s" "usage: ./install [bchiu]"
printf "\n%s" " -b | --build-only : build, but do not install sns"
printf "\n%s" " -c | --clean : clean build directory"
printf "\n%s" " -h | --help : Print this information"
printf "\n%s" " -i | --install : build and install sns"
printf "\n%s" " -u | --uninstall : remove sns from the system (will not remove notes)"
printf "\n"
printf "\n\n%s" "Run Variables:"
printf "\n %s %s" "BUILD_DIR= " "Directory to build SNS in"
printf "\n %s %s" "INSTALL_DIR=" "Directory to install SNS to"
printf "\n\n"
}
# Platform detection
if [ -z "$PREFIX" ]; then
case "$(uname)" in
"Darwin" )
PREFIX=/usr/local/opt/sns
;;
*)
PREFIX=/opt/sns
;;
esac
fi
# Sanity check
if [ -z "$PREFIX" ]; then
if [ -z "$INSTALL_DIR" ]; then
printf "$RED_COLOR!$RESET_COLOR - %s\n"\
"Failed to determine an install prefix please specify with PREFIX=\n"
"Failed to guess optimal install directory please specify with INSTALL_DIR=\n"
exit 10
fi
if [ -r "$HOME"/.bash_profile ]; then
BASH_SETTINGS="$HOME/.bash_profile"
elif [ -r "$HOME"/.bashrc ]; then
if [ -r "$HOME"/.bashrc ]; then
BASH_SETTINGS="$HOME/.bashrc"
elif [ -r "$HOME"/.bash_profile ]; then
BASH_SETTINGS="$HOME/.bash_profile"
else
printf "$RED_COLOR!$RESET_COLOR - %s\n"\
"Failed to determine bash settings file (e.g. ~/.bashrc)"
exit 13
fi
# Print header
printf "\n %s\n" "$PROD_STR"
printf " %s\n" "$PROD_SUB"
printf "%s\n\n" "--------------------"
# Argument Parsing
for ARG in "$@"; do
case "$ARG" in
-h|--help)
print_help
exit
;;
-b|--build-only)
if [ ! -d ./build ]; then mkdir ./build; fi
build > "$S"; chmod +x "$S"
cp src/bash-completion/sns ./build/bash-completion
build_sns
printf "\n"
exit
;;
-c|--clean)
if [ -d ./build ]; then rm -rf build; fi
clean
exit
;;
-u|--uninstall)
rm -rf $PREFIX
check_install_dir_perms
if [ "$WRITE" == "1" ]; then
rm -rf "$INSTALL_DIR"
else
sudo rm -rf "$INSTALL_DIR"
fi
exit
;;
-i|--install)
if [ ! -d ./build ]; then mkdir ./build; fi
build > "$S"
cp src/bash-completion/sns ./build/bash-completion
install
build_sns
install_sns
verify
printf "\n"
exit
;;
-h|--help)
print_help
exit
;;
esac

Binary file not shown.

View File

@@ -27,7 +27,7 @@ function create(){
fi
# Make sure the note exists, and inform the user of the result.
if [ -e "$NOTES_DIR/$NOTE" ]; then
printf " - %s\n" "Created note: ${NOTE%.*}"
printf "%s\n" "- Created note: ${NOTE%.*}"
else
>&2 printf "$RED_COLOR!$RESET_COLOR %s\n"\
"Something went wrong, and the note was not created."

View File

@@ -4,11 +4,11 @@ function delete(){
if [ -e "$NOTES_DIR/$NOTE" ]; then
rm "$NOTES_DIR/$NOTE"
printf " - %s\n" "Deleted note: ${NOTE%.*}."
printf "%s\n" "- Deleted note: ${NOTE%.*}."
#Cleanup empty notebooks/sections]
find "$NOTES_DIR" -mindepth 1 -type d | tac |\
while read -r DIR ; do
if [ ! "$(ls -A $DIR)" ]; then
if [ ! "$(ls -A "$DIR")" ]; then
rmdir "$DIR"
fi
done

View File

@@ -31,12 +31,12 @@ if [ -z "$CREATE" ]; then
fi
# Call the editor
printf " - %s\n" "editing ${NOTE%.*}"
printf "%s\n" "- editing ${NOTE%.*}"
"$EDITOR" "$TMP_NOTE"
# If the file was previously decrypted, encrypt it back
if [ "$ENCRYPTION" == "TRUE" ]; then
printf " - %s\n" "encrypting ${NOTE%.*}"
printf "%s\n" "- encrypting ${NOTE%.*}"
rm "$NOTES_DIR/$NOTE"
encrypt;
rm "$TMP_NOTE"

View File

@@ -1,8 +1,3 @@
PROD_STR="Simple Note System"
VER_STR="v2.0a11"
YEAR=2016
cat << EOF
#!/bin/bash
# Simple Note System
# Copyright (C) 2016, Jon Lewis
@@ -22,15 +17,15 @@ cat << EOF
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
# Prevent freak accidents involving the root directory
if [ -z "\$HOME" ]; then HOME=/home/"\$(whoami)"; fi
if [ -z "$HOME" ]; then HOME=/home/"$(whoami)"; fi
# Store files and locations
readonly PROD_STR="$PROD_STR"
readonly VER_STR="$VER_STR"
readonly ROOT_DIR="\$HOME"/.local/sns
readonly NOTES_DIR="\$ROOT_DIR"/notes
readonly TMP_DIR="\$ROOT_DIR"/tmp
readonly CONFIG_FILE="\$ROOT_DIR/sns.conf"
readonly PROD_STR="Simple Note System"
readonly VER_STR="v2.0a12"
readonly ROOT_DIR="$HOME"/.local/sns
readonly NOTES_DIR="$ROOT_DIR"/notes
readonly TMP_DIR="$ROOT_DIR"/tmp
readonly CONFIG_FILE="$ROOT_DIR/sns.conf"
#Color codes for messages
readonly RED_COLOR='\033[1;31m'
@@ -38,6 +33,5 @@ readonly YELLOW_COLOR='\033[1;33m'
readonly RESET_COLOR='\033[0m'
#Print the program header to stdout
printf "%s\n" "\$PROD_STR"
printf "%s\n" "$PROD_STR"
printf "%s\n" "------------------"
EOF

View File

@@ -1,5 +1,8 @@
function help {
printf "\n%s" "usage: sns [-cedlp] <notebook/section/name>"
if [ -z "$VCTL" ]; then
printf "\n%s%s%s" "usage: sns " "$VCTL" " ..."
fi
printf "\n%s" " sns [-hi ]"
printf "\n%s" " -c | --create : Create note"
@@ -9,5 +12,5 @@ function help {
printf "\n%s" " -i | --init : Write default config and initalize SNS store"
printf "\n%s" " -l | --list : List all notes in NOTEBOOK"
printf "\n%s" " -p | --print : Print note to console"
printf "\n"
printf "\n\n"
}

View File

@@ -4,6 +4,7 @@ if [ ! -d "$ROOT_DIR" ]; then mkdir -p "$ROOT_DIR" ; WILL_INIT="TRUE"; fi
if [ ! -d "$TMP_DIR" ]; then mkdir -p "$TMP_DIR" ; WILL_INIT="TRUE"; fi
if [ ! -d "$NOTES_DIR" ]; then mkdir -p "$NOTES_DIR"; fi
if [ ! -r "$CONFIG_FILE" ]; then
cat > "$CONFIG_FILE" << EOF
# This file contains directives for the Simple Note System.
@@ -26,21 +27,19 @@ PUBKEY="" # Public Key
# Encryption is done using GPG. You must enter your
# public key's identifier here.
#VCTL="" # Version Control program
#VCTL="" # Version Control Program
# Set this to the name of your preferred version control
# program to use it in SNS. For example, if VCTL is Set
# to `git`, `sns git ...`` will call `git ...` in SNS's
# store. This can be used to revert changes, or to sync
# across computers.
# program to use it in SNS. Examples: git, hg, svn
EOF
chmod 600 "$CONFIG_FILE"
printf " - %s\n" "Rewrote Default Configuration"
printf "%s\n" "- Rewrote Default Configuration"
else
printf "$RED_COLOR!$RESET_COLOR - %s" "Refusing to overwrite existing config"
fi
if [ "$WILL_INIT" == "TRUE" ]; then
printf " - %s\n" "Environment initialized in $ROOT_DIR"
printf "%s\n" "- Environment initialized in $ROOT_DIR"
else
printf " - %s\n" "Store already initialized."
printf "%s\n" "- Store already initialized."
fi
}

View File

@@ -1,3 +1,3 @@
function p_header(){
printf "# %s\n## %s\n" "$(basename ${NOTE%.*})" "$(date "$DATE_FMT")"
printf "# %s\n## %s\n" "$(basename "${NOTE%.*}")" "$(date "$DATE_FMT")"
}