~jbauer/kontaktdb

kontaktdb/kontaktdb -rwxr-xr-x 4.5 KiB
33c1b010Jake Bauer Increment patch version number 2 years ago
                                                                                
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#!/bin/sh

# kontaktdb - Store, retrieve, and edit contact information in plain text
#
# Copyright (C) 2020  Jake Bauer
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.

VERSION="0.2.1"

if [ -n "$KONTAKT_DB" ]; then
    database="$KONTAKT_DB"
elif [ -n "$XDG_DATA_HOME" ]; then
    database="$XDG_DATA_HOME"/kontaktdb
else
    database="$HOME"/.kontaktdb
fi

# POSIX Shell "Strict Mode"
set -o errexit
set -o nounset
IFS=$(printf '\n\t')

touch $database

queryfile="/tmp/kontaktdb-query-results.txt"
name=""  # Intentionally blank
email="" # Intentionally blank
query="" # Intentionally blank

helptext=$(cat << EOF
kontaktdb - Store, retrieve, and edit contact information in plain text

Usage: kontaktdb {action} [arguments]

Available actions:
-h|--help                           show brief help and exit
-v|--version                        print kontaktdb's version number and exit
-a|--add <name> <email>             add a contact to the database
-c|--change <query> <name> <email>  change an existing contact's information
-r|--retrive <query>                retrieve a contact's information
-d|--delete <query>                 delete a contact
-l|--list                           list all contacts in the database

Friendly names of actions can also be used. E.g. 'kontaktdb add' is valid.

Arguments:
<query>    the string to search the database for
<name>     the contact's name
<email>    the contact's email

Examples:

    $ kontaktdb -a "John Smith" "john.smith@example.com"
    New contact added to database

    $ kontaktdb -r john
    john.smith@example.com	John Smith

    $ kontaktdb -c john "John Doe" "john.doe@example.com"
    Contact information changed

    $ kontaktdb -d john
    Contact deleted from database
EOF
)

# Escapes all special BRE characters which could appear in a returned entry
# The first argument is the entry to escape
escape_entry()
{
echo "$1" > "$queryfile"
entry=$(sed 's/\\/\\\\/g; s/\//\\\//g; s/\^/\\^/g; s/\[/\\[/g; s/\*/\\*/g;
    s/\./\\./g; s/\$/\\$/g' "$queryfile")
}

retrieve_contact()
{
    [ -z "$query" ] && echo "Please specify a query string" && exit 1
    grep -i "$query" "$database" || true
}

add_contact()
{
    [ -z "$name" ] && echo "The contact's name must be specified" && exit 1
    [ -z "$email" ] && echo "The contact's email must be specified" && exit 1

    entry=$(retrieve_contact)
    escape_entry "$entry"
    if [ -n "$entry" ]; then
        echo "A contact with that email already exists"
        exit 1
    fi

    printf "%s\t%s\n" "$email" "$name" >> "$database"
    echo "New contact added to database"
}

delete_contact()
{
    entry=$(retrieve_contact)
    escape_entry "$entry"
    sed -i "/$entry/d" "$database"
    echo "Contact deleted from database"
}

change_contact()
{
    [ -z "$name" ] && echo "The contact's new name must be specified" && exit 1
    [ -z "$email" ] && echo "The contact's new email must be specified" && exit 1

    entry=$(retrieve_contact)
    escape_entry "$entry"
    sed -i "/$entry/d" "$database"

    printf "%s\t%s\n" "$email" "$name" >> "$database"
    echo "Contact information changed"
}

while test $# -gt 0; do
    case "$1" in
        -h|--help|help)
            echo "$helptext"
            exit 0
            ;;
        -v|--version|version)
            echo "$VERSION"
            exit 0
            ;;
        -a|--add|add)
            name=$2
            email=$3
            query=$email
            add_contact
            exit 0
            ;;
        -c|--change|change)
            query=$2
            name=$3
            email=$4
            change_contact
            exit 0
            ;;
        -r|--retrieve|retrieve)
            query=$2
            retrieve_contact
            exit 0
            ;;
        -d|--delete|delete)
            query=$2
            delete_contact
            exit 0
            ;;
        -l|--list|list)
            cat "$database"
            exit 0
            ;;
        *)
            echo "Unrecognized option; see 'kontaktdb help'."
            exit 1
            ;;
    esac
done