← back to Stayclaim

scripts/filmla-cpra-followup.sh

117 lines

#!/bin/bash
#
# filmla-cpra-followup.sh
#
# Fires once at 9:15 AM PT on 2026-05-09 (10-day CPRA deadline) via launchd.
# Checks Gmail (steve-office) for any inbound from filmla.com that is NOT
# the auto-ticket-creation acknowledgment. If found → log and exit. If not →
# send the courtesy follow-up via George (DW Gmail HTTP agent on tailnet).
#
# Idempotent via sentinel file. Self-disables the launchd job on success
# so it doesn't fire again next May.

set -euo pipefail

GEORGE="http://100.107.67.67:9850"
ENV_FILE="${FILMLA_CPRA_ENV_FILE:-$HOME/.filmla-cpra-followup.env}"
if [ -f "$ENV_FILE" ]; then
  set -a
  # shellcheck disable=SC1090
  . "$ENV_FILE"
  set +a
fi

AUTH="${FILMLA_GEORGE_BASIC_AUTH:-}"
LOG="$HOME/Library/Logs/filmla-cpra-followup.log"
SENTINEL="$HOME/Library/Application Support/filmla-cpra-followup.done"
LABEL="com.steve.filmla-cpra-followup"
TARGET_DATE="2026-05-09"

mkdir -p "$(dirname "$SENTINEL")"

log() {
  printf '[%s] %s\n' "$(date '+%Y-%m-%d %H:%M:%S %Z')" "$*" | tee -a "$LOG"
}

# Date guard — only fires on the target day (CPRA 10-day deadline).
# Override with FILMLA_FORCE=1 for testing.
TODAY=$(date '+%Y-%m-%d')
if [ "${FILMLA_FORCE:-0}" != "1" ] && [ "$TODAY" != "$TARGET_DATE" ]; then
  log "Today is $TODAY, target is $TARGET_DATE. Not firing. (Set FILMLA_FORCE=1 to override.)"
  exit 0
fi

if [ -f "$SENTINEL" ]; then
  log "Sentinel exists — already ran on $(cat "$SENTINEL"). Exiting."
  exit 0
fi

if [ -z "$AUTH" ]; then
  log "FILMLA_GEORGE_BASIC_AUTH is required (format: user:password)."
  exit 1
fi

log "=== FilmLA CPRA follow-up firing ==="

# Step 1: search Gmail. Exclude auto-ack ("New Service Ticket Created").
QUERY='from:filmla.com newer_than:11d -subject:"New Service Ticket Created"'
search_response=$(curl -sS --max-time 20 -u "$AUTH" \
  -G --data-urlencode "q=$QUERY" \
  "$GEORGE/api/search")

total=$(echo "$search_response" | jq -r '.total // 0')
log "Search: q='$QUERY' → total=$total"

if [ "$total" -gt 0 ]; then
  sample=$(echo "$search_response" | jq -c '.messages[0]')
  log "FilmLA REPLIED — no follow-up needed. Sample: $sample"
  echo "$(date -u +%Y-%m-%dT%H:%M:%SZ) replied" > "$SENTINEL"
  /bin/launchctl bootout "gui/$(id -u)/$LABEL" 2>/dev/null || true
  log "Booted out launchd job. Done."
  exit 0
fi

log "No reply detected. Sending follow-up to info@filmla.com via George."

read -r -d '' BODY_HTML <<'HTML' || true
<p>Hello FilmLA team,</p>
<p>Following up on my California Public Records Act request submitted on April 29, 2026 (subject: &ldquo;California Public Records Act request &mdash; historical film permit data&rdquo;). Cal. Gov. Code &sect; 7922.535 requires a determination within 10 calendar days, which falls on today.</p>
<p>Please confirm:</p>
<ul>
  <li>(a) whether the request can be fulfilled, and an estimated production date,</li>
  <li>(b) whether any compilation fee will apply (and the estimate), or</li>
  <li>(c) whether any specific exemption is being asserted.</li>
</ul>
<p>If a 14-day extension under &sect; 7922.535(b) is needed, please send written notice citing the specific basis for the extension.</p>
<p>Thank you for your continued attention.</p>
<p>Regards,<br>
Steve Abrams<br>
Designer Wallcoverings<br>
+1 888 373 4564</p>
HTML

payload=$(jq -n \
  --arg to "info@filmla.com" \
  --arg subject "Following up: CPRA request submitted 2026-04-29" \
  --arg body "$BODY_HTML" \
  '{to:$to, subject:$subject, body:$body}')

send_response=$(curl -sS --max-time 30 -u "$AUTH" \
  -H "Content-Type: application/json" \
  -X POST "$GEORGE/api/send" \
  -d "$payload")

ok=$(echo "$send_response" | jq -r '.ok // .id // .messageId // empty')
if [ -z "$ok" ]; then
  log "SEND FAILED — response: $send_response"
  log "Leaving sentinel ABSENT so we can retry manually if relaunched."
  exit 1
fi

mid=$(echo "$send_response" | jq -r '.id // .messageId // .threadId // "unknown"')
log "SENT — id=$mid full=$send_response"

echo "$(date -u +%Y-%m-%dT%H:%M:%SZ) sent id=$mid" > "$SENTINEL"
/bin/launchctl bootout "gui/$(id -u)/$LABEL" 2>/dev/null || true
log "Booted out launchd job. Done."