Files
vns/sns.sh
2016-12-30 21:07:07 -06:00

108 lines
3.0 KiB
Bash
Executable File

#!/bin/bash
XL_PRODUCT="Simple Note System"
XL_VER="v2a"
# Environment
readonly SNS_STORE="$HOME/.local/sns"
readonly SNS_KEYFILE="$SNS_STORE/.pubkey"
readonly SNS_DEPS=("gpg" "tree" "git")
readonly IFS=$'\n\t'
set -euo pipefail
# Function Definitions
# sns_initStore Initializes the SNS Store ( -i)
# sns_printHelp Prints help page ( -h)
# sns_list Lists all notes in `tree` format ( *)
# sns_create Creates note; calls sns_edit() ( -c)
# sns_edit Decrypts note to /tmp, calls editor, re-encrypts note ( -e)
# sns_print Prints note to stdout ( -p)
# sns_rm Deletes note from store ( -d)
# sns_gitPassthrough Passes through all instructions to git (git)
# sns_checkDeps Checks the system for required dependencies
# sns_checkStore Checks if $SNS_STORE exists
# sns_sanityCheck Wrapper for sns_checkDeps and sns_checkStore
function sns_initStore {
mkdir -p "$SNS_STORE"
echo $* | awk '{print $1}' > "$SNS_KEYFILE"
}
function sns_printHelp(){
printf "%s" "Import helpfile from sns v2"
}
function sns_list(){
cd "$SNS_STORE" || exit
tree -C --noreport "$(echo $* | awk '{print $1}')"
}
function sns_create(){
gpg -r "$SNS_PUBKEY" -o "echo $* | awk '{print $1}'" < /dev/null
sns_edit "echo $* | awk '{print $1}'"
}
function sns_print(){
gpg -d "echo $* | awk '{print $1}'"
}
function sns_rm(){
rm -f "echo $* | awk '{print $1}'"
}
function sns_gitPassthrough(){
cd "$SNS_STORE" || exit;
git "$@";
}
function sns_checkDeps(){
local SNS_RETURN="true";
for DEP in "${SNS_DEPS[@]}"; do
if test ! -e "$(which gpg 2>/dev/null)"; then
echo "Dependency missing: $DEP";
SNS_RETURN="false";
fi
done
"$SNS_RETURN";
}
function sns_checkStore(){
if [ -d "$SNS_STORE" ]; then true; else false; fi
}
function sns_sanityCheck {
if ! sns_checkDeps; then
exit 10
fi
if ! sns_checkStore; then
if [ "$(echo "$SNS_ACTION" | awk '{print $1;}')" != "sns_initStore" ]; then
printf "%s\n" "Please run \`sns -i\` to initialize sns."
exit 20
fi
fi
}
function sns_argParse(){
declare -ga SNS_ACTION=()
while getopts ":i:hc:e:p:d:" OPT; do
case "$OPT" in
i)
SNS_ACTION=("sns_initStore" "$OPTARG");;
h)
SNS_ACTION=("sns_printHelp");;
c)
SNS_ACTION=("sns_create $OPTARG");;
e)
SNS_ACTION=("sns_edit $OPTARG");;
p)
SNS_ACTION=("sns_print $OPTARG");;
d)
SNS_ACTION=("sns_rm $OPTARG");;
esac
done
if [ "${#SNS_ACTION[@]}" -eq 0 ]; then
if [ "$(echo "$@" | awk '{print $1}')" == "git" ]; then
SNS_ACTION=($@)
else
if [ -d "$SNS_STORE/$*" ]; then
SNS_ACTION=("sns_list" "./$*")
fi
fi
fi
}
# Entry Point
printf "%s\n%s\n" "$XL_PRODUCT" "$XL_VER"
sns_argParse "$@"
sns_sanityCheck
"${SNS_ACTION[@]}"