diff --git a/autofax.py b/autofax.py index d9a8996..e79cc8a 100755 --- a/autofax.py +++ b/autofax.py @@ -2,6 +2,7 @@ """AutoFax - Send a fax every hour via Telnyx and log results.""" import json +import subprocess import sys import time from datetime import datetime, timezone @@ -249,7 +250,26 @@ def update_previous_statuses(entries: list[dict]) -> list[dict]: return entries +def check_expiry(): + """If past the expiry date, remove cron and exit.""" + expires_file = config.REPORTS_DIR / ".expires" + if not expires_file.exists(): + return + try: + expiry = datetime.fromisoformat(expires_file.read_text().strip()) + if datetime.now() >= expiry: + print("AutoFax expired. Removing cron job.") + result = subprocess.run(["crontab", "-l"], capture_output=True, text=True) + lines = [l for l in result.stdout.splitlines() if "autofax" not in l] + subprocess.run(["crontab", "-"], input="\n".join(lines) + "\n", text=True) + notify("AutoFax Expired", "7-day schedule complete. Cron job removed.") + sys.exit(0) + except Exception: + pass + + def main(): + check_expiry() pdf_path = find_claim_pdf() entries = load_log() diff --git a/install.sh b/install.sh index f529edf..d9ea3ae 100755 --- a/install.sh +++ b/install.sh @@ -20,12 +20,19 @@ echo "Installing dependencies..." # Create directories mkdir -p "$SCRIPT_DIR/claims" "$SCRIPT_DIR/reports" +# Write expiry timestamp (7 days from now) +if date --version >/dev/null 2>&1; then + date -d "+7 days" "+%Y-%m-%dT%H:%M:%S" > "$SCRIPT_DIR/reports/.expires" +else + date -v+7d "+%Y-%m-%dT%H:%M:%S" > "$SCRIPT_DIR/reports/.expires" +fi + # Check for .env if [ ! -f "$SCRIPT_DIR/.env" ]; then echo "" echo "WARNING: No .env file found!" - echo "Copy .env.example to .env and fill in your credentials:" - echo " cp $SCRIPT_DIR/.env.example $SCRIPT_DIR/.env" + echo " Run: python3 setup_telnyx.py" + echo " Or: cp .env.example .env (then edit manually)" echo "" fi @@ -37,35 +44,15 @@ if [ "$PDF_COUNT" -eq 0 ]; then echo "" fi -# Calculate expiry date (7 days from now) -if date --version >/dev/null 2>&1; then - # GNU date - EXPIRY=$(date -d "+7 days" "+%Y-%m-%d %H:%M") -else - # BSD/macOS date - EXPIRY=$(date -v+7d "+%Y-%m-%d %H:%M") -fi +# Install cron job +echo "Installing cron job..." +TMPFILE=$(mktemp) +crontab -l 2>/dev/null | grep -v "$CRON_TAG" > "$TMPFILE" || true +echo "0 * * * * $PYTHON $SCRIPT_DIR/autofax.py >> $SCRIPT_DIR/reports/cron.log 2>&1 $CRON_TAG" >> "$TMPFILE" +crontab "$TMPFILE" +rm -f "$TMPFILE" -# Install cron jobs -echo "Installing cron jobs..." -# Remove any existing autofax entries -crontab -l 2>/dev/null | grep -v "$CRON_TAG" > /tmp/autofax_cron || true - -# Hourly fax job -echo "0 * * * * $PYTHON $SCRIPT_DIR/autofax.py >> $SCRIPT_DIR/reports/cron.log 2>&1 $CRON_TAG" >> /tmp/autofax_cron - -# Self-destruct: remove autofax cron entries after 7 days -# Runs once at the expiry time, removes all autofax lines, then removes itself -echo "0 0 * * * $PYTHON -c \" -import subprocess, datetime -if datetime.datetime.now() >= datetime.datetime.fromisoformat('$(date -d '+7 days' '+%Y-%m-%dT%H:%M' 2>/dev/null || date -v+7d '+%Y-%m-%dT%H:%M')'): - result = subprocess.run(['crontab', '-l'], capture_output=True, text=True) - lines = [l for l in result.stdout.splitlines() if 'autofax' not in l] - subprocess.run(['crontab', '-'], input=chr(10).join(lines), text=True) -\" 2>/dev/null $CRON_TAG" >> /tmp/autofax_cron - -crontab /tmp/autofax_cron -rm /tmp/autofax_cron +EXPIRY=$(cat "$SCRIPT_DIR/reports/.expires") echo "" echo "=== Setup Complete ===" @@ -73,7 +60,7 @@ echo "Fax will be sent every hour on the hour." echo "Auto-expires: $EXPIRY (7 days from now)" echo "" echo "Checklist:" -echo " [ ] .env file configured with Telnyx credentials" +echo " [ ] Telnyx configured (.env file or run setup_telnyx.py)" echo " [ ] Claim PDF placed in claims/ directory" echo "" echo "Manual test: $PYTHON $SCRIPT_DIR/autofax.py"