about summary refs log tree commit diff stats
path: root/ayu
blob: 26287b3b1b8c3891a460c148aaa4bf3574e5732e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#!/bin/sh

# Exit on error
set -e

# Disabling some shellcheck stuff here as I won't really solve it anyway.
# shellcheck disable=SC2154,SC1091

ayu_dir=${ayu_dir:-$HOME/.ayu}
ayu_settings=${ayu_settings:-$XDG_CONFIG_HOME/ayu}
ayu_store=${ayu_store:-$HOME/.ayu-store}

# Check if the above directories exist, otherwise create them
for dir in "$ayu_dir" "$ayu_settings" "$ayu_store"; do
    test -d "$dir" || mkdir -p "$dir"
done

# Same thing for the configuration file
if ! test -f "$ayu_settings"/config; then
    cat << EOF > "$ayu_settings"/config
#ayu_clipboard=$(wl-copy --primary)
#ayu_clipboard=$(xsel -ib)
ayu_editor=${ayu_editor:-vi}
ayu_private_key=${ayu_dir}/private_key
ayu_public_key=${ayu_dir}/public_key
EOF
    . "$ayu_settings"/config
else
    . "$ayu_settings"/config
fi

# Switch directory to the store, else bail out
cd "$ayu_store" || exit 1

## THEGOODS ##

fn_copy() {
    fn_view "$2" | "$ayu_clipboard"
}
fn_edit() {
    age --decrypt --identity="${ayu_private_key}" --output="${1%%.age}" "${1%%.age}.age"
    ${ayu_editor} "${1%%.age}"
    age --encrypt --recipients-file "${ayu_public_key}" --output="${1%%.age}.age" "${1%%.age}"
    rm "${1%%.age}"
}
fn_list() {
    tree "$ayu_store"
}
fn_new() {
    test -d "$1" && fn_usage && exit

    tmpfile="$(mktemp)"
    ${ayu_editor} "$tmpfile"

    mkdir -p "$(dirname "$1")"
    age --encrypt --recipients-file "${ayu_public_key}" --output="$tmpfile.age" "$tmpfile"

    mv "$tmpfile.age" "${1%%.age}".age
    rm "$tmpfile"
}
fn_remove_recursive() {
    rm -rf "$@"
}
fn_remove_single() {
    rm -f "${1}${2}.age"
}
fn_usage() {
    printf "Usage: [ -c | -e | -l | -n | -r | -R | -v ] <entry> \n"
}
fn_view() {
    if [ -f "${1%%.age}".age ]; then
	age --decrypt --identity="${ayu_private_key}" "${1%%.age}.age"
    elif [ -d "${1:-.}" ]; then
	tree "${1:-.}"
    else
	fn_usage
	exit
    fi
}
## SDOOGEHT ##

## Command line parsing (I don't get why this has be so difficult for say, Lua)
case $1 in
    -c | copy) fn_copy "$2" ;;
    -e | edit) fn_edit "$2" ;;
    -l | list) fn_list ;;
    -n | add) fn_new "$2" ;;
    -r | del) fn_remove_single "$2" ;;
    -R | delr) fn_remove_recursive "$2" ;;
    -v | view) fn_view "$2" ;;
    *) fn_usage ;;
esac