Monorepo for Aesthetic.Computer
aesthetic.computer
1#!/bin/sh
2# upload-log.sh — Upload ac-native.log to machines API
3# Called in background on wifi connect. Reads auth from config.json.
4# Bundled into /scripts/upload-log.sh in the initramfs.
5
6LOG="/mnt/ac-native.log"
7CONFIG="/mnt/config.json"
8MACHINE_ID_FILE="/mnt/.machine-id"
9API="https://aesthetic.computer/api/machine-logs"
10CA="/etc/pki/tls/certs/ca-bundle.crt"
11
12# Need log file and config
13[ -f "$LOG" ] || exit 0
14[ -f "$CONFIG" ] || exit 0
15
16# Extract user sub from config (simple grep — no jq in initramfs)
17SUB=$(sed -n 's/.*"sub"[[:space:]]*:[[:space:]]*"\([^"]*\)".*/\1/p' "$CONFIG" | head -1)
18[ -z "$SUB" ] && exit 0
19echo "[upload-log] sub=$SUB mid=$MID" >> "$LOG"
20
21# Machine ID
22MID="unknown"
23[ -f "$MACHINE_ID_FILE" ] && MID=$(cat "$MACHINE_ID_FILE" | tr -d '\n')
24
25# Read log lines, escape for JSON, limit to 200 lines
26LINES=""
27n=0
28while IFS= read -r line && [ $n -lt 200 ]; do
29 # Escape backslashes, double quotes, and control chars
30 escaped=$(printf '%s' "$line" | sed 's/\\/\\\\/g; s/"/\\"/g; s/ /\\t/g')
31 if [ -n "$LINES" ]; then
32 LINES="$LINES,\"$escaped\""
33 else
34 LINES="\"$escaped\""
35 fi
36 n=$((n + 1))
37done < "$LOG"
38
39[ -z "$LINES" ] && exit 0
40
41# Build JSON body (include sub for device auth)
42BODY="{\"machineId\":\"$MID\",\"sub\":\"$SUB\",\"lines\":[$LINES],\"sessionType\":\"log\"}"
43
44# Write body to temp file (avoid shell escaping issues with curl -d)
45echo "$BODY" > /tmp/log-upload.json
46
47# Upload (use sub as a simple auth — the API will validate)
48curl -sf -X POST "$API" \
49 -H "Content-Type: application/json" \
50 --cacert "$CA" \
51 -d @/tmp/log-upload.json \
52 --max-time 15 > /tmp/log-upload-result.json 2>&1
53
54rc=$?
55rm -f /tmp/log-upload.json
56echo "[upload-log] rc=$rc mid=$MID lines=$n" >> "$LOG"