#!/usr/bin/bash

# CCPA disclosure and deletion letters from the command
# line.  Just type:

#   ccpa privacy@example.com

# to start a new email with your CCPA (and my best
# guesses at what to say for the other privacy
# juristictions) text already set up.

# My boilerplate letter is here: https://blog.zgp.org/ccpa-letter/
# Please share yours if you have something that will
# work better.

# You get a chance to edit the mail before sending, so
# this is your opportuntity to share your brand safety
# concerns if you have any.

# This script works with Bash and Mutt. If you have an
# alternate version for your own mailer, please let me
# know and I'll link to it.

# Uses mo(1) to process templates if available.
# Source: https://github.com/tests-always-included/mo

LETTER=../../privacy-docs/ccpa-letter.md
SUBJECT="Subject: Disclosure and deletion of my personal information"
PGP=no

# Path to your letter. Can be relative to the location
# of the script.

# Facebook-centric opt-out letter
FBLETTER=../../privacy-docs/ccpa-opt-out.md

# Deletion/objection letter
DELETELETTER=../../privacy-docs/ccpa-delete.md

# Right to Know letter
RTKLETTER=../../privacy-docs/ccpa-rtk.md

# Utility stuff.  Run the script from the directory
# where it is installed, and clean up the temp file.
fail() {
	echo $1
	exit 1
}

cleanup(){
	rm -f $TMPFILE
	popd &> /dev/null
}
trap 'cleanup' EXIT
pushd $PWD &> /dev/null
cd $(dirname "$0")
TMPFILE=$(mktemp) || exit 1

# Extract your "From" address from your Mutt config
# file, and the "To" from the command-line argument.
FROM=$(egrep '^set from' $HOME/.muttrc | cut -d '=' -f 2 | tr -d '"')

if [ "x$CCPA_EMAIL" == 'x' ]; then
	export CCPA_EMAIL=$FROM
fi

if [ "x$CCPA_ADDRESS" == 'x' ]; then
	export CCPA_ADDRESS="Address not set. Set CCPA_ADDRESS in your environment."
fi

if [ "x$CCPA_PHONE" == 'x' ]; then
	export CCPA_PHONE="Phone not set. Set CCPA_PHONE in your environment."
fi

if [ $1 == '--fb' ]; then
	LETTER=$FBLETTER
	SUBJECT="Subject: CCPA: opt out of sale of my personal information"
	TO=$2
	PGP=yes
elif [ $1 == '-d' ]; then
	LETTER=$DELETELETTER
	SUBJECT="Subject: Deletion of my personal information"
	TO=$2
	PGP=no
elif [ $1 == '--rtk' ]; then
	LETTER=$RTKLETTER
	SUBJECT="Subject: California Consumer Privacy Act"
	TO=$2
	PGP=no
else
	TO=$1
fi

echo $TO | grep -q '@' || fail "Usage: $0 <email address>"

# Write the headers. The X- header is to help keep
# track of which recipients got which versions.
echo "From: $FROM" > $TMPFILE
echo $SUBJECT >> $TMPFILE
echo "To: $TO" >> $TMPFILE
echo -n "X-CCPA-Content: " >> $TMPFILE
git hash-object $LETTER >> $TMPFILE

# Chop off the front matter from the letter. If you
# have more or fewer lines, change this.  Then run Mutt
# with the tempfile as your draft and without your
# regular .signature file.

# If the "mo" template tool is installed, pre-process the
# letter to include values for CCPA_EMAIL, CCPA_ADDRESS, and CCPA_PHONE.
if [ -e $(which mo) ]; then
	tail -n +7 $LETTER | mo >> $TMPFILE
else
	tail -n +7 $LETTER >> $TMPFILE
fi

mutt -e "set signature=" -e "set crypt_autosign=$PGP" -H $TMPFILE

# All done.
