Pensieve/pensieve
2017-01-24 15:31:37 +01:00

172 lines
3.6 KiB
Bash
Executable file

#!/bin/bash
# usage: pensieve [OPTIONS] {test|stat|read|add} [file]
#
# -p pager
# -q quiet
# -f filter, n.i.
# -j DATE jump (implies -p)
# DATE: YYYY.MM.DD
# -e entries per page (implies -p), n.i.
EXIT_SUCCESS=0
EXIT_FAILURE=1
SEPERATOR="####################"
opts=$(getopt -o "qpfj:e:" -- $@)
if test $? != 0; then
exit $EXIT_FAILURE
fi
eval set -- "$opts"
pager=0
bquiet=0
jump=""
epp=-1
quiet() {
test $bquiet != 0
return $?
}
while true; do
case "$1" in
-p) pager=1; shift;;
-q) bquiet=1; shift;;
-f) shift;;
-j) jump="$2"; shift 2;;
-e) epp=$2; shift 2;;
--) shift; break;
esac
done
mode=$1
file=$2
if test "$file" = ""; then
file=~/.diary
fi
quiet || echo -n "Password: "
read -s password
quiet || echo
sleep 1s
quiet || echo
if test "$mode" = "stat"; then
content=$(gpg --quiet --batch --passphrase "$password" --decrypt $file 2> /dev/null)
if test $? != 0; then
echo "There was an error while decrypting file. Right passord?" 1>&2
exit $EXIT_FAILURE
fi
echo "Encrypted size: $(stat -L --format=%s $file)"
echo "Decrypted size: $(echo $content | wc -c)"
echo "Entries: $(echo "$content" | grep $SEPERATOR | wc -l)"
dates=$(echo "$content" | grep -A 1 $SEPERATOR | grep -v $SEPERATOR)
echo "First entry: $(echo "$dates" | head -n 1)"
echo "Last entry: $(echo "$dates" | tail -n 1)"
exit $EXIT_SUCCESS
fi
if test "$mode" = "test"; then
gpg --quiet --batch --passphrase "$password" --decrypt $file 2>&1 > /dev/null
if test $? = 0; then
quiet || echo "Success."
exit $EXIT_SUCCESS
else
quiet || echo "Fail."
exit $EXIT_FAILURE
fi
fi
if test "$mode" = "add"; then
tmpfile="/dev/shm/$$.diary.$(echo $file | sed 's/\//-/g')"
content=""
if test -f $file; then
content=$(gpg --quiet --batch --passphrase "$password" --decrypt $file)
if test $? != 0; then
echo "There was an error while decrypting file. Right password?" 1>&2
exit $EXIT_FAILURE
fi
else
echo -n "File not found. Create new diary? [y/n] "
read yn
if test "$yn" != "yes" -a "$yn" != "y"; then
exit $EXIT_SUCCESS
fi
touch $file
fi
editor $tmpfile
if test $? != 0; then
echo "Editor exited with error. Aborting." 1>&2
exit $EXIT_FAILURE
fi
datestring=$(date "+%Y.%m.%d. %H:%M:%S")
echo "$content" | cat - <(echo $SEPERATOR) <(echo $datestring) $tmpfile | gpg --batch --passphrase "$password" --symmetric > $file
rm $tmpfile
exit $EXIT_SUCCESS
fi
debug() {
cat 1>&2
}
if test "$mode" = "read"; then
content=$(gpg --quiet --batch --passphrase "$password" --decrypt $file)
if test $? != 0; then
echo "There was an error while decrypting file. Right password?"1>&2
exit $EXIT_FAILURE
fi
if test "$epp" != "-1"; then
#todo
exit $EXIT_SUCCESS
elif test "$jump" != ""; then
jY=$(echo $jump | awk -F. '{print $1 }')
jM=$(echo $jump | awk -F. '{print $2 }')
jD=$(echo $jump | awk -F. '{print $3 }')
quiet || echo "Searching for year $jY month $jM day $jD..."
fl=$(echo "$content" | grep -A 1 $SEPERATOR | grep -v -- "--" | grep -v $SEPERATOR | awk -F. '{
if ($1 >'$jY')
print NR;
else if ($1=="'$jY'" && $2 > '$jM')
print NR;
else if ($1=="'$jY'" && $2=="'$jM'" && $3 >= '$jD')
print NR;
else
;
}'
)
fl=$(echo "$fl" | head -n 1)
if test "$fl" == ""; then
exit $EXIT_SUCCESS;
fi
fl=$(echo "$content" | grep -n "$SEPERATOR" | sed $fl'q;d' | cut -f1 -d:)
# TODO maybe the following line just works with less
echo "$content" | pager +$fl
exit $EXIT_SUCCESS
fi
if test "$pager" = 0; then
echo "$content"
else
echo "$content" | pager
fi
exit $EXIT_SUCCESS
fi
echo "Unknown mode." 1>&2
exit $EXIT_FAILURE