#!/bin/bash
# Password management script

umask 077
if [ $# -ne 1 ]
then
   echo usage: pwm -i or -e or a search string
   exit
fi

# pwm -i: first-time-only initialisation
if [ $1 = "-i" ]
then
   if [ -e ~/.pwminfo.gpg ]
   then
       echo Already initialised
       exit 1
   fi
   echo initialisation
   gpg --gen-key
   echo password store > ~/.pwminfo
   gpg --encrypt --recipient $USER ~/.pwminfo
   rm ~/.pwminfo
   exit
fi

# pwm -e: edit the password store
if [ $1 = "-e" ]
then
   echo edit
   gpg --quiet --decrypt ~/.pwminfo.gpg > ~/.pwminfo
   mv ~/.pwminfo.gpg ~/.pwminfo.gpg.save
   vi ~/.pwminfo
   gpg --encrypt --recipient $USER ~/.pwminfo
   rm ~/.pwminfo ~/.pwminfo.gpg.save
   exit
fi

# Else, extract a password
   gpg --quiet --decrypt ~/.pwminfo.gpg | grep $1

