[READ-ONLY] Mirror of https://github.com/mrgnw/cv.
1#!/bin/bash
2
3# PDF Generation Daemon for macOS
4# Watches for git triggers and automatically processes them using cv.skate-in.ts.net
5
6set -e
7
8REPO_DIR="/Users/m/dev/cv"
9TRIGGER_FILE="$REPO_DIR/.git-trigger"
10REMOTE_URL="https://cv.skate-in.ts.net"
11LOG_DIR="$REPO_DIR/logs"
12LOG_FILE="$LOG_DIR/pdf-daemon.log"
13
14# Ensure log directory exists
15mkdir -p "$LOG_DIR"
16
17# Function to log with timestamp
18log() {
19 echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1" | tee -a "$LOG_FILE"
20}
21
22# Function to check if remote server is available
23check_remote_server() {
24 if curl -s --max-time 5 "$REMOTE_URL" > /dev/null 2>&1; then
25 return 0
26 else
27 return 1
28 fi
29}
30
31# Function to trigger PDF generation via remote server
32trigger_remote_pdf_generation() {
33 log "🌐 Triggering PDF generation via $REMOTE_URL"
34
35 if ! check_remote_server; then
36 log "❌ Remote server $REMOTE_URL is not accessible"
37 return 1
38 fi
39
40 # Trigger PDF generation on the remote server
41 local response=$(curl -s -X POST \
42 -H "Content-Type: application/json" \
43 -d '{"force": false}' \
44 "$REMOTE_URL/dev/api/pdf" 2>/dev/null)
45
46 if [[ $? -eq 0 ]]; then
47 log "✅ Remote PDF generation triggered successfully"
48 log "📄 Response: $response"
49 return 0
50 else
51 log "❌ Failed to trigger remote PDF generation"
52 return 1
53 fi
54}
55
56# Function to process git trigger
57process_git_trigger() {
58 local trigger_content=""
59 if [[ -f "$TRIGGER_FILE" ]]; then
60 trigger_content=$(cat "$TRIGGER_FILE" 2>/dev/null || echo "{}")
61 rm -f "$TRIGGER_FILE"
62 fi
63
64 log "🔔 Processing git trigger: $trigger_content"
65
66 # Change to repo directory
67 cd "$REPO_DIR"
68
69 # Get current branch
70 local branch=$(git rev-parse --abbrev-ref HEAD 2>/dev/null || echo "unknown")
71 log "📍 Current branch: $branch"
72
73 # Trigger remote PDF generation
74 if ! trigger_remote_pdf_generation; then
75 log "❌ Remote PDF generation failed, skipping git operations"
76 return 1
77 fi
78
79 # Wait a bit for PDFs to be generated
80 log "⏳ Waiting for PDF generation to complete..."
81 sleep 10
82
83 # Check if there are any PDF changes to commit
84 local pdf_changes=$(git status --porcelain static/ .pdf-cache.json 2>/dev/null | grep -E '\.(pdf|json)$' || true)
85
86 if [[ -z "$pdf_changes" ]]; then
87 log "ℹ️ No PDF or cache changes detected, nothing to commit"
88 return 0
89 fi
90
91 # Show what changed
92 log "📋 PDF and cache changes detected:"
93 echo "$pdf_changes" | while read line; do
94 log " - $line"
95 done
96
97 # Add PDF files and cache
98 log "➕ Adding PDF files and cache to git..."
99 git add static/*.pdf static/sans/*.pdf .pdf-cache.json 2>/dev/null || true
100
101 # Check if there's anything to commit after adding
102 if git diff --cached --quiet 2>/dev/null; then
103 log "ℹ️ No staged changes, nothing to commit"
104 return 0
105 fi
106
107 # Commit with timestamp
108 local timestamp=$(date '+%Y-%m-%d %H:%M:%S')
109 local commit_msg="Update PDFs and cache - $timestamp"
110 log "💾 Committing: $commit_msg"
111
112 if git commit -m "$commit_msg" 2>/dev/null; then
113 log "✅ Commit successful"
114
115 # Push to current branch
116 log "🚀 Pushing to $branch..."
117 if git push origin "$branch" 2>/dev/null; then
118 log "✅ Push successful"
119 else
120 log "❌ Push failed"
121 return 1
122 fi
123 else
124 log "❌ Commit failed"
125 return 1
126 fi
127
128 log "✅ Git workflow completed successfully"
129}
130
131# Main daemon loop
132log "🚀 PDF Generation Daemon started"
133log "📁 Watching: $TRIGGER_FILE"
134log "🌐 Remote server: $REMOTE_URL"
135
136while true; do
137 if [[ -f "$TRIGGER_FILE" ]]; then
138 log "🔔 Git trigger detected!"
139
140 if process_git_trigger; then
141 log "✅ Trigger processed successfully"
142 else
143 log "❌ Trigger processing failed"
144 fi
145
146 log "👀 Continuing to watch for triggers..."
147 fi
148
149 # Check every 3 seconds
150 sleep 3
151done