|
| 1 | +#!/bin/bash |
| 2 | +# Edit a file that has been encoded with gpg. |
| 3 | + |
| 4 | +# this gets us some functions and variables needed by various programs |
| 5 | +if [[ -e "${HOME}/docs/code/library.sh" ]] ; then |
| 6 | + source "${HOME}/docs/code/library.sh" |
| 7 | +fi |
| 8 | + |
| 9 | +function usage { |
| 10 | + echo "Usage: $0 FILE" |
| 11 | +} |
| 12 | + |
| 13 | +# exit after printing first argument to this function |
| 14 | +function die { |
| 15 | + # echo the first argument |
| 16 | + echo $1 |
| 17 | + |
| 18 | + exit 1 |
| 19 | +} |
| 20 | + |
| 21 | +# this will get the location of $1 (from the $PATH) |
| 22 | +function get_location_of_file() |
| 23 | +{ |
| 24 | + tmp_filename=$1 |
| 25 | + tmp_fullpath=`which ${tmp_filename} 2>/dev/null` |
| 26 | + |
| 27 | + if [ -n "${tmp_fullpath}" ] |
| 28 | + then |
| 29 | + echo ${tmp_fullpath} |
| 30 | + fi |
| 31 | +} |
| 32 | + |
| 33 | +# this takes an argument list of filenames and outputs |
| 34 | +# the first one that can be found on $PATH. |
| 35 | +# It outputs a full path to the file. |
| 36 | +function get_correct_filename_from_choices() |
| 37 | +{ |
| 38 | + while ((1)) |
| 39 | + do |
| 40 | + # break if there are no more arguments |
| 41 | + if [ -z $1 ] |
| 42 | + then |
| 43 | + break |
| 44 | + fi |
| 45 | + |
| 46 | + # get the full path of the filename |
| 47 | + tmp_filename=$1 |
| 48 | + shift |
| 49 | + tmp_fullpath=`get_location_of_file ${tmp_filename}` |
| 50 | + |
| 51 | + # if this program is on $PATH, echo it's full path |
| 52 | + # and get out of this loop |
| 53 | + if [ -n "${tmp_fullpath}" ] |
| 54 | + then |
| 55 | + echo ${tmp_fullpath} |
| 56 | + break |
| 57 | + fi |
| 58 | + done |
| 59 | +} |
| 60 | + |
| 61 | +GPG=`get_correct_filename_from_choices "gpg1" "gpg"` |
| 62 | + |
| 63 | +# make sure there is only one argument |
| 64 | +if [ "$#" != "1" ] |
| 65 | +then |
| 66 | + usage |
| 67 | + exit 1 |
| 68 | +fi |
| 69 | + |
| 70 | +file_name=$1 |
| 71 | + |
| 72 | +# make sure the argument is a file |
| 73 | +[[ -f $file_name ]] || die "ERROR! $file_name is not a file." |
| 74 | + |
| 75 | +# get us a temp file |
| 76 | +temp_file=`mktemp puppies.XXXXXX` |
| 77 | + |
| 78 | +# read in password |
| 79 | + |
| 80 | +read -s -p "Password: " password |
| 81 | +echo |
| 82 | + |
| 83 | +# unencrypt the file and put it's output in tempfile |
| 84 | +echo $password | $GPG --passphrase-fd 0 -d ${file_name} > ${temp_file} || |
| 85 | + die "ERROR! Could not decrypt $file_name" |
| 86 | + |
| 87 | +# edit the tempfile |
| 88 | +$EDITOR ${temp_file} || |
| 89 | + die "ERROR! Could not edit the decrypted file ${temp_file}" |
| 90 | + |
| 91 | +# reencrypt the tempfile |
| 92 | +echo $password | $GPG --passphrase-fd 0 -c -o ${file_name} ${temp_file} || |
| 93 | + die "ERROR! Could not reencrypt ${temp_file} to ${file_name}." |
| 94 | + |
| 95 | +# delete the tempfile |
| 96 | +rm ${temp_file} |
| 97 | + |
0 commit comments