#!/bin/bash

PASSWD_LIST=passwords.db.cpt
PASSWD_LIST_UNENCRYPTED=passwords.db
EDITOR=vim

# create our tempfile for our temporary password storage
TEMPFILE=`tempfile 2>/dev/null` || TEMPFILE=/tmp/`basename $0`.tmp

# we have to make sure that we delete our tempfile whatever way we exit
trap "rm -f $TEMPFILE" 0 1 2 5 15

# prompt the user for the password
dialog --backtitle "Password Database" --title "Master Password" --clear --insecure --passwordbox "Enter the Password Database master password." 10 51 2> $TEMPFILE

RETVAL=$?

case $RETVAL in
	0)
		PASSWD=`cat $TEMPFILE`;;
	1)
		echo "Authentication Required!";;
	255)
		echo "Authentication Required!";;
esac
		
# decrypt the password list
ccrypt -d $PASSWD_LIST -K $PASSWD 2> /dev/null
RETVAL=$?

# if decryption succeeded, open the password list in vim
# and then re-encrypt it after vim closes
case $RETVAL in
	0)
		$EDITOR $PASSWD_LIST_UNENCRYPTED 2> /dev/null;
		ccrypt -e $PASSWD_LIST_UNENCRYPTED -K $PASSWD 2> /dev/null;;
	1|4)
		echo "Invalid Password!";;
esac

clear
