#!/bin/bash
# crypt_gpg v0.1
# Copyright (c) 2013 Daniel Lange, http://daniel-lange.com.
# Released into the public domain. NO LIABILITY ACCEPTED WHATSOEVER. USE AT YOUR OWN RISK.

# CRYPT_KEY may be max. 2400 (+ a few) bytes
# 
CRYPT_KEY="${HOME}/.gnupg/mykey001"
METHOD="--symmetric"

if (($# == 0)) || [[ "$1" == "--help" ]] || [[ "$1" == "-h" ]]; then
	echo "Usage: $0 [--decrypt] <filename> [<filename> ...]"
	exit 1
fi

if [[ ! -e "$CRYPT_KEY" ]]; then
	echo "Error: Cryptographic key does not exist on this system."
	exit 2
fi

if [[ "$1" == "--decrypt" ]] || [[ "$1" == "-d" ]]; then
        METHOD="--multifile --decrypt"
        shift
fi

# --multifile works for decrypt but not for encrypt...

if [[ "$METHOD" == "--symmetric" ]]; then
	for FILE in "$@"; do
		cat "$CRYPT_KEY" | tr -d "\n\r\000" | gpg $METHOD --cipher-algo AES256 --passphrase-fd 0 --batch --verbose $FILE
	done
else
	cat "$CRYPT_KEY" | tr -d "\n\r\000" | gpg $METHOD --cipher-algo AES256 --passphrase-fd 0 --batch --verbose $*
fi

