#!/bin/ksh
# Password management script
# Author: Perette Barella
# Copyright 1998 - 2018 Devious Fish
VERSION='$Id: pass 45 2018-11-16 17:11:51Z perette $'

readonly arg0=$(basename $0)
NAMEOPTS=""
USAGE="ef:"
[[ $(getopts '[-][12:abc]' flag --abc; print -- 0$flag) == "012" ]] &&
        NAMEOPTS="-a $arg0" &&
        USAGE=$'
[-1?'$VERSION$']
[+NAME?\f?\f - Password lookup and editor]
[+DESCRIPTION?\b\f?\f\b looks up passwords in an encrypted text file,
and allows editing that text file.  Searches are case-blind.]
[+?Although \b\f?\f\b was written for password management, it can be
repurposed for other files using the \v-f\v option and shell \balias\b
command.]
[f:file?Specify the password file.  Files are encrypted only if
the the file extension is \v.cpt\v.]:[file]
[e:edit?Edit the file using the editor specified by the
\vVISUAL\v or \vEDITOR\v environment variables, or \bvi\b(1) by default.]
[+FILES?]
{
  [+passwords.cpt?The default password file.]
}
[+ENVIRONMENT?]
{
  [+PASSFILE?Sets an alternate path for the password file.
      Overriden by \v-f\v.]
}
[+SEE ALSO?\bcloud\b(1), \bpn\b(1), \bccrypt\b(1).]

search terms ...

[-author?Perette Barella <perette@deviousfish.com>
'


typeset edit=false
file=${PASSFILE:-passwords.cpt}
while getopts $NAMEOPTS "$USAGE" option
do
	case "$option" in
	    f)
		file="$OPTARG"
		;;
	    e)
		edit=true
		;;
	esac
done
shift $((OPTIND - 1))

if $edit
then
	cloud edit -t -f "$file"
elif (( $# == 0 ))
then
	OPTIND=0
	getopts $NAMEOPTS "$USAGE" option --short
else
	cloud extract -f "$file" "$@"
fi
exit $?

