dotfiles/secrecy

53 lines
1.2 KiB
Bash

#!/bin/bash
set -e
binary=age
repourl="https://github.com/FiloSottile/age"
helptxt="Usage: secrecy [OPTION] FILE
-e, --encrypt Encrypts a new file
-d, --decrypt Decrypts an already encrypted file
-h, --help Prints this text and exit
If no flags are given, the file will be encrypted by default."
function printerr() { echo "$*" >&2; }
function encrypt() {
if [ $1 = "" ]; then
printerr "No file to encrypt was provided."
exit 1
fi
$binary -p "${1}" > "${1}.age"
rm -f "${1}"
}
function decrypt() {
if [ $1 = "" ]; then
printerr "No file to decrypt was provided."
exit 1
fi
$binary -d "${1}" > ${1::-4}
rm -f "${1}"
}
if ! hash $binary 2>/dev/null ; then
printerr "You will need to install $binary to use this script."
printerr "More info about $binary on $repourl"
exit 1
elif [ $# -eq 0 ]; then
printf '%s\n' "$helptxt"
echo
printerr "You didn't enter any arguments, dumbass."
exit 1
fi
case $1 in
"-d" | "--decrypt") decrypt $2 ;;
"-e" | "--encrypt") encrypt $2 ;;
"-h" | "--help") printf '%s\n' "$helptxt" ;;
*) encrypt $1 ;;
esac