#!/bin/sh
#
# This is a little interactive tool which helps you
# to maintain your accounts on one place.
#
# It uses an encrypted database for protecting the
# account list. The tool does not work with temporary
# files for security reasons.
#
# Just execute it. It will show you a little menu of
# all available commands. That's really easy :-)
#
# T.v. Dein <tlinden@cpan.org>


#
# the account database
db=~/.accdb

#
# check if dbtool version is 1.4 or higher
version=`dbtool -V 2>&1 | sed 's/[a-zA-Z .]*//g'`
if test -n "$version"; then
    if test $version -lt 14; then
	echo "This version of dbtool does not support encryption!"
	exit 1
    fi
else
    echo "dbtool is not installed!"
    exit 1
fi

#
# get the passphrase
echo -n "Enter passphrase: "
read PW

if test -z "$PW"; then
    echo "empty passphrase!"
    exit -1
fi

#
# store it in a local environment variable,
# so it will not appear in the process list and
# dbtool itself will not ask for it
export DB_PASSPHRASE=$PW


#
# the silly menu
function menu {
    echo
    echo -n "[L]ist [N]ew [S]earch [Q]uit> "
}


#
# go
echo
menu

while :
do
    read command

    case $command in
      l|L)
	echo
	if [ -e $db ]; then
	    #
	    # just dump all entries out. Use a custom output
	    # separator for better formatting with sed :-)
	    dbtool -d $db -p -D -F "" | sed 's//  =>   /'
	fi
      n|N)
	echo
	echo -n "Enter entry name: "
	read name
	if [ "x$name" = "x" ]; then
	    echo "empty name!"
	else
	    echo -n "Enter username: "
	    read user
	    echo -n "Enter password: "
	    read pass
	    #
	    # create a new entry, separate the key and the value
	    # using the pipe character, overwrite existing entry
	    echo "$name| Username: $user, Password: $pass" \
		| dbtool -p -i -f -d $db -F "|"
	    echo "entry $name inserted."
	fi
      s|S)
	echo -n "Enter search string: "
	read string
	#
	# search for the given key
	dbtool -p -d $db -s -k $string
      q|Q)
	echo
	echo "Thanks for the fish."
	echo
	exit 0
    esac

    menu
done

#
# clear the environment variable. just in case...
unset DB_PASSPHRASE
