#!/bin/bash
# Created by Ben Okopnik on Mon Oct 29 12:03:32 UTC 2001
# Edits or searches a crypt-encoded "pass" file in the users home dir

[ -z "$1" ] && { printf "Usage: ${0##*/} -e|<search_term>\n"; exit; }

STTY=`stty -g`
printf "Enter password (screen echo disabled):\n"
stty cbreak -echo
read
stty $STTY
[ -z "$REPLY" ] && { printf "No password entered.\n"; exit; }

[ "$1" == "-e" ] && {
        OLDMASK=`umask`
        umask 077
        DIR=`mktemp /tmp/$$.XXXXXX`
        rm $DIR; mkdir $DIR     # So I cheated. Sue me. :)
        trap "[ -d $DIR ] && { rm -rf $DIR; umask $OLDMASK; }" 0
        crypt $REPLY < ~/pass > $DIR/pass

        file $DIR/pass|grep -q text
        [ $? -eq 0 ] && {
                $EDITOR $DIR/pass && \
                crypt $REPLY < $DIR/pass > ~/pass
        } || {
                printf "Edit failed - the result was not a text file!\n"
        }

        rm -rf $DIR
        umask $OLDMASK
} || {
        crypt "$REPLY" < ~/pass | grep -i "$1"
}

