aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFlorian Fischer <florian.fl.fischer@fau.de>2017-04-06 12:23:26 +0200
committerFlorian Fischer <florian.fl.fischer@fau.de>2017-04-06 12:23:26 +0200
commitaccbe651c848d521e8fa53a3c0962643cb193b8b (patch)
treecc0fa0e82ca6bb1503131dc072266cd38e57b889
parent2c03584f66d6bb05a1af4f418146e9444e5d3a49 (diff)
downloaddiary-accbe651c848d521e8fa53a3c0962643cb193b8b.tar.gz
diary-accbe651c848d521e8fa53a3c0962643cb193b8b.zip
rewrite diary
-rwxr-xr-xdiary161
1 files changed, 130 insertions, 31 deletions
diff --git a/diary b/diary
index 4208a3c..806526a 100755
--- a/diary
+++ b/diary
@@ -1,4 +1,4 @@
-#!/bin/bash
+#!/usr/bin/env bash
# Copyright © 2017 Florian Fischer <muhq@muhq.space>
# This program is free software. It comes without any warranty, to
@@ -7,8 +7,122 @@
# To Public License, Version 2, as published by Sam Hocevar. See
# http://www.wtfpl.net/ for more details.
+
+get_file_name() {
+ if [ "$1" ]
+ then
+ file_name=$(date -d $1 +%Y-%m-%d 2>/dev/null)
+ if [ $? -ne 0 ]
+ then
+ return 1
+ fi
+ else
+ file_name=$(date +%Y-%m-%d)
+ fi
+}
+
+help_show() {
+ echo "${0##*/} show [date] - shows one or all dates in \$PAGER"
+}
+
+show_file() {
+ if [ $(echo $1 | cut -f 2 -d ".") = "gpg" ]
+ then
+ { echo $1; gpg -q -d $1; } | $PAGER
+ else
+ { echo $1; cat $1; } | $PAGER
+ fi
+}
+
+cmd_show() {
+ if [ "$1" ]
+ then
+ get_file_name $1
+ show_file ${file_name}*
+ else
+ for f in *
+ do
+ show_file $f
+ done
+ fi
+}
+
+help_list() {
+ echo "${0##*/} list - not implemented yet"
+}
+
+cmd_list() {
+ echo "not implemented yet"
+}
+
+help_help() {
+ echo "${0##*/} help - print a general or specific help message"
+}
+
+cmd_help() {
+ case "$1" in
+ show) help_show ;;
+ list) help_list ;;
+ help) help_help ;;
+ edit) help_edit ;;
+ *) help_usage ;;
+ esac
+}
+
+help_usage() {
+ echo "${0##*/} [cmd] [date]"
+ echo -e "\n\tedit [date] - edit the entry of date/today"
+ echo -e "\tshow [date] - open one or all entries in \$PAGER"
+ echo -e "\thelp [cmd] - print a help message"
+ echo -e "\tlist - not implemented yet"
+ echo -e "\nIf no command is given edit is used."
+}
+
+help_edit() {
+ echo "${0##*/} edit [date] - edit the entry of date/today"
+}
+
+cmd_edit_or_usage() {
+ if [ "$1" = "edit" ]
+ then
+ shift
+ fi
+
+ get_file_name $1
+
+ if [ $? -ne 0 ]
+ then
+ cmd_help
+ exit $?
+ fi
+
+ if [ ! -f ${file_name}* ]
+ then
+ echo -e "Rating: " > $file_name
+ elif [ -f ${file_name}*.gpg ]
+ then
+ file_name=$(echo ${file_name}* | cut -d "," -f 1 -)
+ gpg --output ${file_name} -d ${file_name}*.gpg
+ rm ${file_name}*.gpg
+ fi
+
+ eval $EDITOR ${file_name}*
+
+ # Put the rating in the file name
+ rating=$(head -1 $file_name | cut -f 2 -d " " -)
+ mv $file_name $file_name,$rating
+ file_name="$file_name,$rating"
+
+ # Encrypt the file_name
+ if [ -n "$pgp_id" ]
+ then
+ gpg -r "$pgp_id" -e ${file_name}
+ rm ${file_name}
+ fi
+}
+
# source config
-if [ -n "$XDGCONFIG" ]
+if [ "$XDGCONFIG" ]
then
if [ -f $XDG_CONFIG/diary/config ]
then
@@ -22,44 +136,29 @@ then
source $HOME/.diaryrc
else
echo "no configuration found"
- return 1
+ exit 1
fi
-pushd $diary_location > /dev/null
-# Get the entry name
+echo $diary_location
+pushd $diary_location > /dev/null
-entry=$(date -d $1 +%Y-%m-%d 2> /dev/null)
-if [ $? -ne 0 ]
-then
- entry=$(date +%Y-%m-%d)
-fi
-if [ ! -f ${entry}* ]
+# set defaults
+if [ ! "$EDITOR" ]
then
- echo -e "Rating: " > $entry
- new="true"
-elif [ -f ${entry}*.gpg ]
-then
- entry=$(echo ${entry}* | cut -d "." -f 1 -)
- gpg --output ${entry} -d ${entry}.gpg
- rm ${entry}.gpg
+ EDITOR=nano
fi
-
-eval $EDITOR ${entry}*
-
-if [ -n "$new" ]
+if [ ! "$PAGER" ]
then
- rating=$(head -1 $entry | cut -f 2 -d " " -)
- mv $entry $entry,$rating
- entry="$entry,$rating"
+ PAGER=less
fi
-# Encrypt the entry
-if [ -n "$pgp_id" ]
-then
- gpg -r "$pgp_id" -e ${entry}
- rm ${entry}
-fi
+case "$1" in
+show) shift; cmd_show "$@" ;;
+list) shift; cmd_list "$@" ;;
+help) shift; cmd_help "$@" ;;
+*) cmd_edit_or_usage "$@" ;;
+esac
popd > /dev/null