Split three more linux_configuration feature and fix scripts

setup_syncyomi.sh 408 -> 138 (two cuts), setup_personal_website.sh
396 -> 192, fix_thorium.sh 377 -> 208.

All three already declared SCRIPT_DIR and the splitter added a second
one, which aborts at runtime with "readonly variable" while bash -n and
shellcheck stay clean. The stubbed run caught it; the splitter now
reuses an existing declaration.

Fixed SC2164 in build_website rather than suppressing it: a bare
cd "$WEBSITE_SRC" inside a subshell, with pnpm install and pnpm run
build after it. A failed cd would have built in the caller's directory.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_011w2Pj4Ek3ZNMQVYcFHgXDY
This commit is contained in:
Krzysztof Rudnicki 2026-08-16 18:48:01 +02:00
parent 9e62430b8d
commit 1a75ed5bf5
9 changed files with 747 additions and 649 deletions

View File

@ -0,0 +1,18 @@
{
"title": "Split three more linux_configuration feature and fix scripts",
"objective": "Bring setup_syncyomi.sh (408), setup_personal_website.sh (396) and fix_thorium.sh (377) under the 250-line cap, and teach the splitter to reuse an existing SCRIPT_DIR rather than redeclaring it.",
"acceptance_criteria": [
"All three entry scripts and all four new libs are at most 250 lines.",
"bash -n and shellcheck pass on every touched file, with no new suppression and no shellcheck disable directive.",
"Each entry script declares SCRIPT_DIR exactly once. All three already had one, and a second declaration aborts the script at runtime with 'readonly variable' while passing bash -n and shellcheck.",
"The stubbed run reports every moved function as defined, for all three scripts.",
"No script is executed. All three write Docker compose stacks, Caddy config or browser profile data.",
"The unchecked cd exposed in build_website is fixed rather than suppressed."
],
"out_of_scope": [
"Executing any setup or fix step.",
"clean_audio.sh and setup_passwordless_system.sh, both deferred for reasons already recorded.",
"Changing what any script does when run, beyond the cd guard."
],
"verifier": "scratchpad/verify_split.sh per script (bash -n + shellcheck -x + stubbed run), plus check_file_length.sh showing none of the six files, plus pre-commit run --files and the real git commit for jscpd."
}

View File

@ -0,0 +1,44 @@
{
"intent": "Split three more linux_configuration scripts under the cap, fixing a latent unchecked cd the split exposed.",
"scope": [
"linux_configuration/scripts/single_use/features/setup_syncyomi.sh (408 -> 138) + lib/syncyomi_stack.sh, lib/syncyomi_expose.sh",
"linux_configuration/scripts/single_use/features/setup_personal_website.sh (396 -> 192) + lib/website_build.sh",
"linux_configuration/scripts/single_use/fixes/fix_thorium.sh (377 -> 208) + lib/thorium_repairs.sh",
"Non-goal: no script executed"
],
"changes": [
"setup_syncyomi.sh needed two cuts: the compose/startup half went to syncyomi_stack.sh, which was still 279 lines, so the Caddy vhost, DuckDNS record and firewall rules went on to syncyomi_expose.sh.",
"All three entry scripts already declared SCRIPT_DIR, and the splitter had added a second declaration to each. That aborts the script with 'readonly variable' at runtime while passing bash -n and shellcheck; the stubbed run caught it on setup_personal_website.sh. Removed from all three, and the splitter now reuses an existing declaration.",
"Fixed SC2164 in build_website: a bare cd \"$WEBSITE_SRC\" inside a subshell, followed by pnpm install and pnpm run build. A failed cd would have run the build in whatever directory the caller happened to be in. Now guarded with || die."
],
"verification": [
{
"command": "scratchpad/verify_split.sh per script",
"result": "fail then pass",
"evidence": "setup_personal_website.sh first aborted with 'SCRIPT_DIR: readonly variable'. After the fix, all three report 'libs sourced, functions defined'."
},
{
"command": "shellcheck on each new lib",
"result": "fail then pass",
"evidence": "SC2164 on the unguarded cd in website_build.sh, fixed at the source. Clean afterwards, with no disable directive anywhere."
},
{
"command": "bash ~/utils/scripts/check_file_length.sh --all | grep <these files>",
"result": "pass",
"evidence": "No output - all three entry scripts and all four libs are under the cap."
},
{
"command": "pre-commit run --files <changed>, then the real git commit",
"result": "pass",
"evidence": "Clean, jscpd included, now with nine new libs across the linux_configuration commits."
}
],
"risks": [
"None of the three was run: they write Docker compose stacks, Caddy vhosts and browser profile data. The stubbed run proves the libs are reachable and the functions defined, not that the scripts still work end to end.",
"The cd guard is a behaviour change in the failure path only: previously the build would have run in the wrong directory, now it dies. That is the intended correction, but it is a change."
],
"rollback": [
"git revert the commit; the three scripts return to 408, 396 and 377 lines and the four libs disappear.",
"After rollback, re-run bash -n and shellcheck on the restored scripts."
]
}

View File

@ -0,0 +1,143 @@
#!/bin/bash
# Caddy vhost, DuckDNS record and firewall rules for exposing SyncYomi.
#
# Sourced by setup_syncyomi.sh; split out to keep syncyomi_stack.sh under
# the 250-line cap. Sourced rather than run, so it inherits the caller's
# strict mode and the variables defined above the source line.
# --- Step 6: Caddy reverse-proxy block (append + reload) ---------------------
configure_caddy() {
if grep -qE "^${SYNCYOMI_SUBDOMAIN} \{" "${CADDYFILE}"; then
log_ok "Caddyfile already has a block for ${SYNCYOMI_SUBDOMAIN}"
else
log_info "Adding Caddy block for ${SYNCYOMI_SUBDOMAIN}"
# NOTE: appended to the shared Gitea Caddyfile. Re-running setup_gitea.sh
# regenerates that file and drops this block — re-run setup_syncyomi.sh
# to restore it. Caddyfile uses tab indentation.
printf '\n%s {\n\treverse_proxy 127.0.0.1:%s\n}\n' "${SYNCYOMI_SUBDOMAIN}" "${SYNCYOMI_PORT}" \
>>"${CADDYFILE}"
fi
docker exec "${CADDY_CONTAINER}" caddy reload --config "${CADDYFILE_IN_CONTAINER}" ||
die "caddy reload failed — check 'docker logs ${CADDY_CONTAINER}'."
log_ok "Caddy reloaded (auto-HTTPS will be issued once the subdomain resolves)"
}
# --- Step 7: DuckDNS subdomain (extend existing updater; no new cron) --------
configure_duckdns() {
local label="${SYNCYOMI_SUBDOMAIN%.duckdns.org}"
if grep -qE "domains=[^&\"']*${label}([,&\"']|$)" "${DUCKDNS_UPDATER}"; then
log_ok "DuckDNS updater already includes '${label}'"
else
log_info "Adding '${label}' to the existing DuckDNS updater"
sed -i -E "s|(domains=)([^&\"']*)|\1\2,${label}|" "${DUCKDNS_UPDATER}"
fi
# Refresh now (harmless if the subdomain isn't registered yet).
bash "${DUCKDNS_UPDATER}" >/dev/null 2>&1 || log_warn "DuckDNS update returned non-zero (register the subdomain first)."
if getent hosts "${SYNCYOMI_SUBDOMAIN}" >/dev/null 2>&1; then
log_ok "${SYNCYOMI_SUBDOMAIN} resolves"
else
log_warn "${SYNCYOMI_SUBDOMAIN} does not resolve yet — create the '${label}' subdomain in the DuckDNS dashboard."
fi
}
# --- Step 8: firewall (open 80/443 via the owning script, idempotent) -------
ensure_firewall() {
if sudo nft list ruleset 2>/dev/null | grep -qE 'dport \{[^}]*80[^}]*443'; then
log_ok "Firewall already allows tcp/80 + tcp/443"
elif [[ -f ${WG_SCRIPT} ]]; then
log_info "Opening 80/443 via setup_wireguard_ssh.sh allow-web"
sudo bash "${WG_SCRIPT}" allow-web
else
log_warn "setup_wireguard_ssh.sh not found — ensure tcp/80+443 are open."
fi
}
# --- Step 9: report ----------------------------------------------------------
print_report() {
local url="https://${SYNCYOMI_SUBDOMAIN}"
local key="(create one in the web UI)"
[[ -f ${TOKEN_FILE} ]] && key="$(<"${TOKEN_FILE}")"
cat <<EOF
============================================================================
SyncYomi is up.
Host address : ${url}
API key : ${key}
Web UI login : ${SYNCYOMI_USER} / (password in ${CONF_FILE})
TachiyomiSY setup (on the phone):
Settings → Data & storage → Sync → SyncYomi
Host : ${url}
API key : ${key}
Use the SAME API key on every device you want to sync.
If the subdomain is new, create it once in the DuckDNS dashboard
(label: ${SYNCYOMI_SUBDOMAIN%.duckdns.org}) so Caddy can issue TLS.
Acceptance test: on your phone with Wi-Fi OFF (cellular), configure the
sync above and confirm the library round-trips.
============================================================================
EOF
}
# --- status subcommand -------------------------------------------------------
status_cmd() {
print_setup_header "SyncYomi status"
echo
if [[ -f ${COMPOSE_FILE} ]]; then
docker compose -f "${COMPOSE_FILE}" ps
else
log_warn "No compose file at ${COMPOSE_FILE}"
fi
echo
local code
code="$(syncyomi_ready "${SYNCYOMI_LOCAL}" || true)"
if [[ ${code} == "204" || ${code} == "403" ]]; then
log_ok "Local API answering (${SYNCYOMI_LOCAL}, HTTP ${code})"
else
log_warn "Local API not answering (HTTP ${code:-none})"
fi
local sub="${SYNCYOMI_SUBDOMAIN:-kuhy-sync.duckdns.org}"
if [[ -f ${CONF_FILE} ]]; then
# shellcheck source=/dev/null
source "${CONF_FILE}"
sub="${SYNCYOMI_SUBDOMAIN}"
fi
if grep -qE "^${sub} \{" "${CADDYFILE}" 2>/dev/null; then
log_ok "Caddy block present for ${sub}"
else
log_warn "No Caddy block for ${sub}"
fi
if getent hosts "${sub}" >/dev/null 2>&1; then
log_ok "${sub} resolves"
else
log_warn "${sub} does not resolve"
fi
code="$(syncyomi_ready "https://${sub}" || true)"
if [[ ${code} == "204" || ${code} == "403" ]]; then
log_ok "External HTTPS reachable (https://${sub}, HTTP ${code})"
else
log_warn "External HTTPS not reachable yet (HTTP ${code:-none})"
fi
}
# --- main --------------------------------------------------------------------
setup_cmd() {
print_setup_header "Self-hosted SyncYomi manga sync"
preflight
load_config
write_compose
start_containers
wait_for_syncyomi
ensure_local_config
mint_api_key
configure_caddy
configure_duckdns
ensure_firewall
print_report
}

View File

@ -0,0 +1,142 @@
#!/bin/bash
# Compose file, container startup and readiness polling for SyncYomi.
#
# Sourced by setup_syncyomi.sh; split out to keep it under the 250-line cap.
# Sourced rather than run, so it inherits the caller's strict mode and
# the helper functions and variables defined above the source line.
# --- Step 3: docker-compose stack (host networking, loopback bind) -----------
write_compose() {
ensure_dir "${DATA_DIR}/config"
# network_mode: host + SyncYomi's host=127.0.0.1 keeps it loopback-only
# (reached by the host-networked Caddy) and dodges this host's default-drop
# nftables FORWARD chain that would kill a bridge-networked container.
umask 077
cat >"${COMPOSE_FILE}" <<EOF
# Generated by setup_syncyomi.sh — do not edit by hand.
services:
syncyomi:
container_name: syncyomi
image: ${SYNCYOMI_IMAGE}
restart: unless-stopped
network_mode: host
environment:
- TZ=${TZ_VALUE}
volumes:
- ./config:/config
EOF
log_ok "Wrote ${COMPOSE_FILE}"
}
start_containers() {
log_info "Starting SyncYomi container"
docker compose -f "${COMPOSE_FILE}" up -d
}
# Readiness probe. SyncYomi (v1.1.x) has no public /healthz, but GET
# /api/auth/onboard answers 204 (no user yet) or 403 (a user exists) once the
# API is serving — either means "up". Echoes the HTTP code; returns 0 if ready.
# Usage: syncyomi_ready "http://127.0.0.1:8282"
syncyomi_ready() {
local base="$1" code
code="$(curl -sS -o /dev/null -w '%{http_code}' "${base}/api/auth/onboard" 2>/dev/null || true)"
echo "${code}"
[[ ${code} == "204" || ${code} == "403" ]]
}
# --- Step 4: wait until the local API answers --------------------------------
wait_for_syncyomi() {
log_info "Waiting for SyncYomi to come up on ${SYNCYOMI_LOCAL}"
local i
for ((i = 0; i < 60; i++)); do
if syncyomi_ready "${SYNCYOMI_LOCAL}" >/dev/null; then
log_ok "SyncYomi is up (API answering)"
return 0
fi
sleep 2
done
die "SyncYomi did not become healthy in 120s — check 'docker logs syncyomi'."
}
# Defensive: SyncYomi generates config/config.toml on first run. Ensure it binds
# loopback-only (never 0.0.0.0, which would bypass Caddy/TLS) and serves at root.
ensure_local_config() {
[[ -f ${CONFIG_TOML} ]] || return 0
local changed=0
if grep -qE '^\s*host\s*=' "${CONFIG_TOML}"; then
if ! grep -qE '^\s*host\s*=\s*"127\.0\.0\.1"' "${CONFIG_TOML}"; then
sed -i -E 's|^\s*host\s*=.*|host = "127.0.0.1"|' "${CONFIG_TOML}"
changed=1
fi
fi
# Force an empty baseUrl (dedicated subdomain, not a sub-path).
if grep -qE '^\s*baseUrl\s*=' "${CONFIG_TOML}" &&
! grep -qE '^\s*baseUrl\s*=\s*""' "${CONFIG_TOML}"; then
sed -i -E 's|^\s*baseUrl\s*=.*|baseUrl = ""|' "${CONFIG_TOML}"
changed=1
fi
if [[ ${changed} -eq 1 ]]; then
log_info "Patched ${CONFIG_TOML} (host=127.0.0.1, baseUrl empty) — restarting"
docker compose -f "${COMPOSE_FILE}" restart
wait_for_syncyomi
fi
}
# --- Step 5: create first account + mint API key over the local REST API -----
mint_api_key() {
if [[ -f ${TOKEN_FILE} ]]; then
log_ok "API key already minted ($(<"${TOKEN_FILE}") stored in ${TOKEN_FILE})"
return 0
fi
log_info "Creating SyncYomi account and minting API key"
local jar body http key
jar="$(mktemp)"
# shellcheck disable=SC2064
trap "rm -f '${jar}'" RETURN
# JSON with proper escaping (password may contain +,/,= from base64).
body="$(jq -n --arg u "${SYNCYOMI_USER}" --arg p "${SYNCYOMI_PASSWORD}" \
'{username:$u,password:$p}')"
# Onboard the first user only if none exists yet (GET returns 204 when empty).
http="$(curl -fsS -o /dev/null -w '%{http_code}' "${SYNCYOMI_LOCAL}/api/auth/onboard" 2>/dev/null || true)"
if [[ ${http} == "204" ]]; then
http="$(curl -sS -o /dev/null -w '%{http_code}' \
-H 'Content-Type: application/json' -d "${body}" \
"${SYNCYOMI_LOCAL}/api/auth/onboard" 2>/dev/null || true)"
[[ ${http} =~ ^2 ]] || die "Onboard failed (HTTP ${http}) — check 'docker logs syncyomi'."
log_ok "Created first account '${SYNCYOMI_USER}'"
else
log_warn "An account already exists (onboard HTTP ${http}); trying stored credentials"
fi
# Log in to get a session cookie.
http="$(curl -sS -c "${jar}" -o /dev/null -w '%{http_code}' \
-H 'Content-Type: application/json' -d "${body}" \
"${SYNCYOMI_LOCAL}/api/auth/login" 2>/dev/null || true)"
if [[ ! ${http} =~ ^2 ]]; then
log_warn "Login failed (HTTP ${http}). A different account may already own this server."
log_warn "Create an API key manually at ${SYNCYOMI_LOCAL} → Settings → API Keys."
return 0
fi
# Reuse an existing 'tachiyomisy' key if one exists (idempotent); otherwise
# create one. scopes must be a non-empty array — SyncYomi rejects a null.
key="$(curl -sS -b "${jar}" "${SYNCYOMI_LOCAL}/api/keys" 2>/dev/null |
jq -r '[.[] | select(.name=="tachiyomisy")][0].key // empty')"
if [[ -n ${key} ]]; then
log_ok "Reusing existing 'tachiyomisy' API key"
else
key="$(curl -sS -b "${jar}" \
-H 'Content-Type: application/json' \
-d '{"name":"tachiyomisy","scopes":["read","write"]}' \
"${SYNCYOMI_LOCAL}/api/keys" 2>/dev/null | jq -r '.key // empty')"
[[ -n ${key} ]] || die "API key creation returned no key — check 'docker logs syncyomi'."
fi
umask 077
printf '%s\n' "${key}" >"${TOKEN_FILE}"
chmod 600 "${TOKEN_FILE}"
log_ok "Minted API key and stored it in ${TOKEN_FILE} (0600)"
}

View File

@ -0,0 +1,213 @@
#!/bin/bash
# CV build, website generation and the Caddy stack snippets.
#
# Sourced by setup_personal_website.sh; split out to keep it under the 250-line cap.
# Sourced rather than run, so it inherits the caller's strict mode and
# the helper functions and variables defined above the source line.
# --- Phase 5: build the website --------------------------------------------
build_website() {
log_info "Installing website dependencies and building…"
(
cd "$WEBSITE_SRC" || die "Cannot enter ${WEBSITE_SRC}."
pnpm install --frozen-lockfile
pnpm run build
)
[[ -f "${WEBSITE_SRC}/dist/index.html" ]] ||
die "Build did not produce ${WEBSITE_SRC}/dist/index.html."
log_ok "Website built to ${WEBSITE_SRC}/dist."
}
# --- Phase 6: write the website's static-serve stack ------------------------
write_website_stack() {
ensure_dir "$WEBSITE_DATA_DIR"
cat >"$WEBSITE_COMPOSE" <<EOF
# Managed by setup_personal_website.sh — do not edit by hand.
services:
personal-website:
image: caddy:2.8
container_name: personal-website
restart: unless-stopped
network_mode: host
volumes:
- ${WEBSITE_SRC}/dist:/srv:ro
- ./Caddyfile:/etc/caddy/Caddyfile:ro
EOF
cat >"$WEBSITE_INNER_CADDY" <<EOF
# Managed by setup_personal_website.sh — do not edit by hand.
# Static file server on loopback; fronted by gitea-caddy on the public domain.
#
# admin off is REQUIRED: this container shares the host network namespace with
# gitea-caddy, so without it both Caddies would listen on the admin API (:2019)
# and a 'caddy reload' aimed at gitea-caddy could be applied to this one instead
# — which would make it grab 80/443 and run ACME. Disabling admin keeps this
# process strictly a loopback static server on ${WEBSITE_PORT}.
{
admin off
}
:${WEBSITE_PORT} {
root * /srv
try_files {path} /index.html
file_server
encode gzip
}
EOF
log_ok "Wrote ${WEBSITE_COMPOSE} and ${WEBSITE_INNER_CADDY}."
}
# --- Phase 7: front the website on the root domain --------------------------
write_website_snippet() {
cat >"$WEBSITE_SNIPPET" <<EOF
# Managed by setup_personal_website.sh — do not edit by hand.
${WEBSITE_DOMAIN} {
reverse_proxy 127.0.0.1:${WEBSITE_PORT}
}
EOF
log_ok "Wrote ${WEBSITE_SNIPPET}."
}
# --- Phase 8: start the website container -----------------------------------
wait_for_website() {
local _
for _ in $(seq 1 30); do
if curl -sf -o /dev/null "http://127.0.0.1:${WEBSITE_PORT}/"; then
return 0
fi
sleep 1
done
return 1
}
start_website() {
log_info "Starting the personal-website container…"
docker compose -f "$WEBSITE_COMPOSE" up -d
if wait_for_website; then
log_ok "Website answering on http://127.0.0.1:${WEBSITE_PORT}/."
else
die "Website did not become reachable on 127.0.0.1:${WEBSITE_PORT}."
fi
}
# --- Phase 9: reload the edge -----------------------------------------------
reload_caddy() {
if docker exec "$CADDY_CONTAINER" caddy validate --config /etc/caddy/Caddyfile >/dev/null 2>&1; then
docker exec "$CADDY_CONTAINER" caddy reload --config /etc/caddy/Caddyfile
log_ok "Reloaded ${CADDY_CONTAINER}."
else
die "Caddy config invalid — aborting reload. Check ${SITES_DIR}/*.caddy."
fi
}
# --- Phase 10: firewall -----------------------------------------------------
ensure_firewall() {
log_info "Ensuring ports 80/443 are open (idempotent)…"
sudo bash "$WG_SCRIPT" allow-web
}
# --- Phase 11: report -------------------------------------------------------
print_report() {
cat <<EOF
============================================================================
Personal website deployed.
============================================================================
Website : https://${WEBSITE_DOMAIN}/
Gitea : https://${GITEA_DOMAIN}/ (moved from the root)
Gitea moved to a subdomain. Re-point any local clones/mirrors:
git remote set-url <name> https://${GITEA_DOMAIN}/<owner>/<repo>.git
Acceptance test (do this on your phone):
Turn Wi-Fi OFF (use cellular) and open https://${WEBSITE_DOMAIN}/ —
it should load over HTTPS with the hero, live GitHub projects, and CV,
and be usable at phone width.
============================================================================
EOF
}
setup_cmd() {
print_setup_header "Personal website setup"
preflight
migrate_caddy_snippets
relocate_gitea
build_cv_json
build_website
write_website_stack
write_website_snippet
start_website
reload_caddy
ensure_firewall
print_report
log_ok "Personal website setup complete."
}
# --- status: self-diagnose --------------------------------------------------
status_line() {
if [[ $1 -eq 0 ]]; then
log_ok "$2"
else
log_warn "$2"
fi
}
check_http() {
# Prints 0 to stdout if the URL returns 200, else 1.
local url="$1" code
code="$(curl -sk -o /dev/null -w '%{http_code}' --max-time 10 "$url" 2>/dev/null || echo 000)"
[[ $code == "200" ]] && echo 0 || echo 1
}
status_cmd() {
print_setup_header "Personal website status"
if has_cmd node; then
status_line 0 "node present"
else
status_line 1 "node missing"
fi
if has_cmd pnpm; then
status_line 0 "pnpm present"
else
status_line 1 "pnpm missing"
fi
if [[ -f "${WEBSITE_SRC}/dist/index.html" ]]; then
status_line 0 "build present (dist/index.html)"
else
status_line 1 "build missing — run setup"
fi
if docker ps --format '{{.Names}}' | grep -qx personal-website; then
status_line 0 "personal-website container running"
else
status_line 1 "personal-website container not running"
fi
status_line "$(check_http "http://127.0.0.1:${WEBSITE_PORT}/")" \
"local static server (127.0.0.1:${WEBSITE_PORT})"
if [[ -f $WEBSITE_SNIPPET ]]; then
status_line 0 "Caddy website snippet present"
else
status_line 1 "Caddy website snippet missing"
fi
if grep -qE '^[[:space:]]*import[[:space:]]+/etc/caddy/sites' "$CADDYFILE" 2>/dev/null; then
status_line 0 "root Caddyfile uses snippet imports"
else
status_line 1 "root Caddyfile not migrated to imports"
fi
if getent hosts "$WEBSITE_DOMAIN" >/dev/null; then
status_line 0 "${WEBSITE_DOMAIN} resolves"
else
status_line 1 "${WEBSITE_DOMAIN} does not resolve"
fi
if getent hosts "$GITEA_DOMAIN" >/dev/null; then
status_line 0 "${GITEA_DOMAIN} resolves"
else
status_line 1 "${GITEA_DOMAIN} does not resolve"
fi
status_line "$(check_http "https://${WEBSITE_DOMAIN}/")" \
"external https://${WEBSITE_DOMAIN}/"
status_line "$(check_http "https://${GITEA_DOMAIN}/")" \
"external https://${GITEA_DOMAIN}/"
}

View File

@ -167,212 +167,8 @@ build_cv_json() {
fi
}
# --- Phase 5: build the website --------------------------------------------
build_website() {
log_info "Installing website dependencies and building…"
(
cd "$WEBSITE_SRC"
pnpm install --frozen-lockfile
pnpm run build
)
[[ -f "${WEBSITE_SRC}/dist/index.html" ]] ||
die "Build did not produce ${WEBSITE_SRC}/dist/index.html."
log_ok "Website built to ${WEBSITE_SRC}/dist."
}
# --- Phase 6: write the website's static-serve stack ------------------------
write_website_stack() {
ensure_dir "$WEBSITE_DATA_DIR"
cat >"$WEBSITE_COMPOSE" <<EOF
# Managed by setup_personal_website.sh — do not edit by hand.
services:
personal-website:
image: caddy:2.8
container_name: personal-website
restart: unless-stopped
network_mode: host
volumes:
- ${WEBSITE_SRC}/dist:/srv:ro
- ./Caddyfile:/etc/caddy/Caddyfile:ro
EOF
cat >"$WEBSITE_INNER_CADDY" <<EOF
# Managed by setup_personal_website.sh — do not edit by hand.
# Static file server on loopback; fronted by gitea-caddy on the public domain.
#
# admin off is REQUIRED: this container shares the host network namespace with
# gitea-caddy, so without it both Caddies would listen on the admin API (:2019)
# and a 'caddy reload' aimed at gitea-caddy could be applied to this one instead
# — which would make it grab 80/443 and run ACME. Disabling admin keeps this
# process strictly a loopback static server on ${WEBSITE_PORT}.
{
admin off
}
:${WEBSITE_PORT} {
root * /srv
try_files {path} /index.html
file_server
encode gzip
}
EOF
log_ok "Wrote ${WEBSITE_COMPOSE} and ${WEBSITE_INNER_CADDY}."
}
# --- Phase 7: front the website on the root domain --------------------------
write_website_snippet() {
cat >"$WEBSITE_SNIPPET" <<EOF
# Managed by setup_personal_website.sh — do not edit by hand.
${WEBSITE_DOMAIN} {
reverse_proxy 127.0.0.1:${WEBSITE_PORT}
}
EOF
log_ok "Wrote ${WEBSITE_SNIPPET}."
}
# --- Phase 8: start the website container -----------------------------------
wait_for_website() {
local _
for _ in $(seq 1 30); do
if curl -sf -o /dev/null "http://127.0.0.1:${WEBSITE_PORT}/"; then
return 0
fi
sleep 1
done
return 1
}
start_website() {
log_info "Starting the personal-website container…"
docker compose -f "$WEBSITE_COMPOSE" up -d
if wait_for_website; then
log_ok "Website answering on http://127.0.0.1:${WEBSITE_PORT}/."
else
die "Website did not become reachable on 127.0.0.1:${WEBSITE_PORT}."
fi
}
# --- Phase 9: reload the edge -----------------------------------------------
reload_caddy() {
if docker exec "$CADDY_CONTAINER" caddy validate --config /etc/caddy/Caddyfile >/dev/null 2>&1; then
docker exec "$CADDY_CONTAINER" caddy reload --config /etc/caddy/Caddyfile
log_ok "Reloaded ${CADDY_CONTAINER}."
else
die "Caddy config invalid — aborting reload. Check ${SITES_DIR}/*.caddy."
fi
}
# --- Phase 10: firewall -----------------------------------------------------
ensure_firewall() {
log_info "Ensuring ports 80/443 are open (idempotent)…"
sudo bash "$WG_SCRIPT" allow-web
}
# --- Phase 11: report -------------------------------------------------------
print_report() {
cat <<EOF
============================================================================
Personal website deployed.
============================================================================
Website : https://${WEBSITE_DOMAIN}/
Gitea : https://${GITEA_DOMAIN}/ (moved from the root)
Gitea moved to a subdomain. Re-point any local clones/mirrors:
git remote set-url <name> https://${GITEA_DOMAIN}/<owner>/<repo>.git
Acceptance test (do this on your phone):
Turn Wi-Fi OFF (use cellular) and open https://${WEBSITE_DOMAIN}/ —
it should load over HTTPS with the hero, live GitHub projects, and CV,
and be usable at phone width.
============================================================================
EOF
}
setup_cmd() {
print_setup_header "Personal website setup"
preflight
migrate_caddy_snippets
relocate_gitea
build_cv_json
build_website
write_website_stack
write_website_snippet
start_website
reload_caddy
ensure_firewall
print_report
log_ok "Personal website setup complete."
}
# --- status: self-diagnose --------------------------------------------------
status_line() {
if [[ $1 -eq 0 ]]; then
log_ok "$2"
else
log_warn "$2"
fi
}
check_http() {
# Prints 0 to stdout if the URL returns 200, else 1.
local url="$1" code
code="$(curl -sk -o /dev/null -w '%{http_code}' --max-time 10 "$url" 2>/dev/null || echo 000)"
[[ $code == "200" ]] && echo 0 || echo 1
}
status_cmd() {
print_setup_header "Personal website status"
if has_cmd node; then
status_line 0 "node present"
else
status_line 1 "node missing"
fi
if has_cmd pnpm; then
status_line 0 "pnpm present"
else
status_line 1 "pnpm missing"
fi
if [[ -f "${WEBSITE_SRC}/dist/index.html" ]]; then
status_line 0 "build present (dist/index.html)"
else
status_line 1 "build missing — run setup"
fi
if docker ps --format '{{.Names}}' | grep -qx personal-website; then
status_line 0 "personal-website container running"
else
status_line 1 "personal-website container not running"
fi
status_line "$(check_http "http://127.0.0.1:${WEBSITE_PORT}/")" \
"local static server (127.0.0.1:${WEBSITE_PORT})"
if [[ -f $WEBSITE_SNIPPET ]]; then
status_line 0 "Caddy website snippet present"
else
status_line 1 "Caddy website snippet missing"
fi
if grep -qE '^[[:space:]]*import[[:space:]]+/etc/caddy/sites' "$CADDYFILE" 2>/dev/null; then
status_line 0 "root Caddyfile uses snippet imports"
else
status_line 1 "root Caddyfile not migrated to imports"
fi
if getent hosts "$WEBSITE_DOMAIN" >/dev/null; then
status_line 0 "${WEBSITE_DOMAIN} resolves"
else
status_line 1 "${WEBSITE_DOMAIN} does not resolve"
fi
if getent hosts "$GITEA_DOMAIN" >/dev/null; then
status_line 0 "${GITEA_DOMAIN} resolves"
else
status_line 1 "${GITEA_DOMAIN} does not resolve"
fi
status_line "$(check_http "https://${WEBSITE_DOMAIN}/")" \
"external https://${WEBSITE_DOMAIN}/"
status_line "$(check_http "https://${GITEA_DOMAIN}/")" \
"external https://${GITEA_DOMAIN}/"
}
# shellcheck source=lib/website_build.sh
source "$SCRIPT_DIR/lib/website_build.sh"
main() {
local cmd="${1:-setup}"

View File

@ -122,278 +122,10 @@ save_config() {
log_ok "Saved config to ${CONF_FILE} (0600)"
}
# --- Step 3: docker-compose stack (host networking, loopback bind) -----------
write_compose() {
ensure_dir "${DATA_DIR}/config"
# network_mode: host + SyncYomi's host=127.0.0.1 keeps it loopback-only
# (reached by the host-networked Caddy) and dodges this host's default-drop
# nftables FORWARD chain that would kill a bridge-networked container.
umask 077
cat >"${COMPOSE_FILE}" <<EOF
# Generated by setup_syncyomi.sh — do not edit by hand.
services:
syncyomi:
container_name: syncyomi
image: ${SYNCYOMI_IMAGE}
restart: unless-stopped
network_mode: host
environment:
- TZ=${TZ_VALUE}
volumes:
- ./config:/config
EOF
log_ok "Wrote ${COMPOSE_FILE}"
}
start_containers() {
log_info "Starting SyncYomi container"
docker compose -f "${COMPOSE_FILE}" up -d
}
# Readiness probe. SyncYomi (v1.1.x) has no public /healthz, but GET
# /api/auth/onboard answers 204 (no user yet) or 403 (a user exists) once the
# API is serving — either means "up". Echoes the HTTP code; returns 0 if ready.
# Usage: syncyomi_ready "http://127.0.0.1:8282"
syncyomi_ready() {
local base="$1" code
code="$(curl -sS -o /dev/null -w '%{http_code}' "${base}/api/auth/onboard" 2>/dev/null || true)"
echo "${code}"
[[ ${code} == "204" || ${code} == "403" ]]
}
# --- Step 4: wait until the local API answers --------------------------------
wait_for_syncyomi() {
log_info "Waiting for SyncYomi to come up on ${SYNCYOMI_LOCAL}"
local i
for ((i = 0; i < 60; i++)); do
if syncyomi_ready "${SYNCYOMI_LOCAL}" >/dev/null; then
log_ok "SyncYomi is up (API answering)"
return 0
fi
sleep 2
done
die "SyncYomi did not become healthy in 120s — check 'docker logs syncyomi'."
}
# Defensive: SyncYomi generates config/config.toml on first run. Ensure it binds
# loopback-only (never 0.0.0.0, which would bypass Caddy/TLS) and serves at root.
ensure_local_config() {
[[ -f ${CONFIG_TOML} ]] || return 0
local changed=0
if grep -qE '^\s*host\s*=' "${CONFIG_TOML}"; then
if ! grep -qE '^\s*host\s*=\s*"127\.0\.0\.1"' "${CONFIG_TOML}"; then
sed -i -E 's|^\s*host\s*=.*|host = "127.0.0.1"|' "${CONFIG_TOML}"
changed=1
fi
fi
# Force an empty baseUrl (dedicated subdomain, not a sub-path).
if grep -qE '^\s*baseUrl\s*=' "${CONFIG_TOML}" &&
! grep -qE '^\s*baseUrl\s*=\s*""' "${CONFIG_TOML}"; then
sed -i -E 's|^\s*baseUrl\s*=.*|baseUrl = ""|' "${CONFIG_TOML}"
changed=1
fi
if [[ ${changed} -eq 1 ]]; then
log_info "Patched ${CONFIG_TOML} (host=127.0.0.1, baseUrl empty) — restarting"
docker compose -f "${COMPOSE_FILE}" restart
wait_for_syncyomi
fi
}
# --- Step 5: create first account + mint API key over the local REST API -----
mint_api_key() {
if [[ -f ${TOKEN_FILE} ]]; then
log_ok "API key already minted ($(<"${TOKEN_FILE}") stored in ${TOKEN_FILE})"
return 0
fi
log_info "Creating SyncYomi account and minting API key"
local jar body http key
jar="$(mktemp)"
# shellcheck disable=SC2064
trap "rm -f '${jar}'" RETURN
# JSON with proper escaping (password may contain +,/,= from base64).
body="$(jq -n --arg u "${SYNCYOMI_USER}" --arg p "${SYNCYOMI_PASSWORD}" \
'{username:$u,password:$p}')"
# Onboard the first user only if none exists yet (GET returns 204 when empty).
http="$(curl -fsS -o /dev/null -w '%{http_code}' "${SYNCYOMI_LOCAL}/api/auth/onboard" 2>/dev/null || true)"
if [[ ${http} == "204" ]]; then
http="$(curl -sS -o /dev/null -w '%{http_code}' \
-H 'Content-Type: application/json' -d "${body}" \
"${SYNCYOMI_LOCAL}/api/auth/onboard" 2>/dev/null || true)"
[[ ${http} =~ ^2 ]] || die "Onboard failed (HTTP ${http}) — check 'docker logs syncyomi'."
log_ok "Created first account '${SYNCYOMI_USER}'"
else
log_warn "An account already exists (onboard HTTP ${http}); trying stored credentials"
fi
# Log in to get a session cookie.
http="$(curl -sS -c "${jar}" -o /dev/null -w '%{http_code}' \
-H 'Content-Type: application/json' -d "${body}" \
"${SYNCYOMI_LOCAL}/api/auth/login" 2>/dev/null || true)"
if [[ ! ${http} =~ ^2 ]]; then
log_warn "Login failed (HTTP ${http}). A different account may already own this server."
log_warn "Create an API key manually at ${SYNCYOMI_LOCAL} → Settings → API Keys."
return 0
fi
# Reuse an existing 'tachiyomisy' key if one exists (idempotent); otherwise
# create one. scopes must be a non-empty array — SyncYomi rejects a null.
key="$(curl -sS -b "${jar}" "${SYNCYOMI_LOCAL}/api/keys" 2>/dev/null |
jq -r '[.[] | select(.name=="tachiyomisy")][0].key // empty')"
if [[ -n ${key} ]]; then
log_ok "Reusing existing 'tachiyomisy' API key"
else
key="$(curl -sS -b "${jar}" \
-H 'Content-Type: application/json' \
-d '{"name":"tachiyomisy","scopes":["read","write"]}' \
"${SYNCYOMI_LOCAL}/api/keys" 2>/dev/null | jq -r '.key // empty')"
[[ -n ${key} ]] || die "API key creation returned no key — check 'docker logs syncyomi'."
fi
umask 077
printf '%s\n' "${key}" >"${TOKEN_FILE}"
chmod 600 "${TOKEN_FILE}"
log_ok "Minted API key and stored it in ${TOKEN_FILE} (0600)"
}
# --- Step 6: Caddy reverse-proxy block (append + reload) ---------------------
configure_caddy() {
if grep -qE "^${SYNCYOMI_SUBDOMAIN} \{" "${CADDYFILE}"; then
log_ok "Caddyfile already has a block for ${SYNCYOMI_SUBDOMAIN}"
else
log_info "Adding Caddy block for ${SYNCYOMI_SUBDOMAIN}"
# NOTE: appended to the shared Gitea Caddyfile. Re-running setup_gitea.sh
# regenerates that file and drops this block — re-run setup_syncyomi.sh
# to restore it. Caddyfile uses tab indentation.
printf '\n%s {\n\treverse_proxy 127.0.0.1:%s\n}\n' "${SYNCYOMI_SUBDOMAIN}" "${SYNCYOMI_PORT}" \
>>"${CADDYFILE}"
fi
docker exec "${CADDY_CONTAINER}" caddy reload --config "${CADDYFILE_IN_CONTAINER}" ||
die "caddy reload failed — check 'docker logs ${CADDY_CONTAINER}'."
log_ok "Caddy reloaded (auto-HTTPS will be issued once the subdomain resolves)"
}
# --- Step 7: DuckDNS subdomain (extend existing updater; no new cron) --------
configure_duckdns() {
local label="${SYNCYOMI_SUBDOMAIN%.duckdns.org}"
if grep -qE "domains=[^&\"']*${label}([,&\"']|$)" "${DUCKDNS_UPDATER}"; then
log_ok "DuckDNS updater already includes '${label}'"
else
log_info "Adding '${label}' to the existing DuckDNS updater"
sed -i -E "s|(domains=)([^&\"']*)|\1\2,${label}|" "${DUCKDNS_UPDATER}"
fi
# Refresh now (harmless if the subdomain isn't registered yet).
bash "${DUCKDNS_UPDATER}" >/dev/null 2>&1 || log_warn "DuckDNS update returned non-zero (register the subdomain first)."
if getent hosts "${SYNCYOMI_SUBDOMAIN}" >/dev/null 2>&1; then
log_ok "${SYNCYOMI_SUBDOMAIN} resolves"
else
log_warn "${SYNCYOMI_SUBDOMAIN} does not resolve yet — create the '${label}' subdomain in the DuckDNS dashboard."
fi
}
# --- Step 8: firewall (open 80/443 via the owning script, idempotent) -------
ensure_firewall() {
if sudo nft list ruleset 2>/dev/null | grep -qE 'dport \{[^}]*80[^}]*443'; then
log_ok "Firewall already allows tcp/80 + tcp/443"
elif [[ -f ${WG_SCRIPT} ]]; then
log_info "Opening 80/443 via setup_wireguard_ssh.sh allow-web"
sudo bash "${WG_SCRIPT}" allow-web
else
log_warn "setup_wireguard_ssh.sh not found — ensure tcp/80+443 are open."
fi
}
# --- Step 9: report ----------------------------------------------------------
print_report() {
local url="https://${SYNCYOMI_SUBDOMAIN}"
local key="(create one in the web UI)"
[[ -f ${TOKEN_FILE} ]] && key="$(<"${TOKEN_FILE}")"
cat <<EOF
============================================================================
SyncYomi is up.
Host address : ${url}
API key : ${key}
Web UI login : ${SYNCYOMI_USER} / (password in ${CONF_FILE})
TachiyomiSY setup (on the phone):
Settings → Data & storage → Sync → SyncYomi
Host : ${url}
API key : ${key}
Use the SAME API key on every device you want to sync.
If the subdomain is new, create it once in the DuckDNS dashboard
(label: ${SYNCYOMI_SUBDOMAIN%.duckdns.org}) so Caddy can issue TLS.
Acceptance test: on your phone with Wi-Fi OFF (cellular), configure the
sync above and confirm the library round-trips.
============================================================================
EOF
}
# --- status subcommand -------------------------------------------------------
status_cmd() {
print_setup_header "SyncYomi status"
echo
if [[ -f ${COMPOSE_FILE} ]]; then
docker compose -f "${COMPOSE_FILE}" ps
else
log_warn "No compose file at ${COMPOSE_FILE}"
fi
echo
local code
code="$(syncyomi_ready "${SYNCYOMI_LOCAL}" || true)"
if [[ ${code} == "204" || ${code} == "403" ]]; then
log_ok "Local API answering (${SYNCYOMI_LOCAL}, HTTP ${code})"
else
log_warn "Local API not answering (HTTP ${code:-none})"
fi
local sub="${SYNCYOMI_SUBDOMAIN:-kuhy-sync.duckdns.org}"
if [[ -f ${CONF_FILE} ]]; then
# shellcheck source=/dev/null
source "${CONF_FILE}"
sub="${SYNCYOMI_SUBDOMAIN}"
fi
if grep -qE "^${sub} \{" "${CADDYFILE}" 2>/dev/null; then
log_ok "Caddy block present for ${sub}"
else
log_warn "No Caddy block for ${sub}"
fi
if getent hosts "${sub}" >/dev/null 2>&1; then
log_ok "${sub} resolves"
else
log_warn "${sub} does not resolve"
fi
code="$(syncyomi_ready "https://${sub}" || true)"
if [[ ${code} == "204" || ${code} == "403" ]]; then
log_ok "External HTTPS reachable (https://${sub}, HTTP ${code})"
else
log_warn "External HTTPS not reachable yet (HTTP ${code:-none})"
fi
}
# --- main --------------------------------------------------------------------
setup_cmd() {
print_setup_header "Self-hosted SyncYomi manga sync"
preflight
load_config
write_compose
start_containers
wait_for_syncyomi
ensure_local_config
mint_api_key
configure_caddy
configure_duckdns
ensure_firewall
print_report
}
# shellcheck source=lib/syncyomi_stack.sh
source "$SCRIPT_DIR/lib/syncyomi_stack.sh"
# shellcheck source=lib/syncyomi_expose.sh
source "$SCRIPT_DIR/lib/syncyomi_expose.sh"
main() {
local cmd="${1:-setup}"

View File

@ -158,178 +158,9 @@ remove_if_exists() {
return 1
}
# Fix 1: Handle corrupted Local State file (most common crash cause)
fix_local_state() {
log_info "Checking Local State file..."
local local_state="$THORIUM_CONFIG_DIR/Local State"
# shellcheck source=lib/thorium_repairs.sh
source "$SCRIPT_DIR/lib/thorium_repairs.sh"
if [[ -f $local_state ]]; then
# Check if it's valid JSON
if ! python3 -c "import json; json.load(open('$local_state'))" 2> /dev/null; then
log_warn "Local State file appears corrupted"
backup_if_exists "$local_state"
else
# Even if valid JSON, back it up as it can still cause crashes
log_info "Local State exists - backing up (common crash source)"
backup_if_exists "$local_state"
fi
else
log_info "No Local State file found (OK for fresh install)"
fi
}
# Fix 2: Clear singleton lock files
fix_singleton_locks() {
log_info "Clearing singleton lock files..."
local locks=(
"$THORIUM_CONFIG_DIR/SingletonLock"
"$THORIUM_CONFIG_DIR/SingletonSocket"
"$THORIUM_CONFIG_DIR/SingletonCookie"
)
local cleared=0
for lock in "${locks[@]}"; do
if remove_if_exists "$lock"; then
((cleared++)) || true
fi
done
if [[ $cleared -eq 0 ]]; then
log_info "No stale lock files found"
fi
}
# Fix 3: Clear GPU cache
fix_gpu_cache() {
log_info "Clearing GPU cache..."
local gpu_paths=(
"$THORIUM_CONFIG_DIR/GPUCache"
"$THORIUM_CONFIG_DIR/Default/GPUCache"
"$THORIUM_CONFIG_DIR/ShaderCache"
"$THORIUM_CONFIG_DIR/Default/ShaderCache"
)
local cleared=0
for cache in "${gpu_paths[@]}"; do
if remove_if_exists "$cache"; then
((cleared++)) || true
fi
done
if [[ $cleared -eq 0 ]]; then
log_info "No GPU cache to clear"
fi
}
# Fix 4: Clear crash reports (can accumulate and cause issues)
fix_crash_reports() {
log_info "Clearing old crash reports..."
local crash_dir="$THORIUM_CONFIG_DIR/Crash Reports"
if [[ -d $crash_dir ]]; then
local crash_count
crash_count=$(find "$crash_dir" -type f 2> /dev/null | wc -l)
if [[ $crash_count -gt 0 ]]; then
if [[ $DRY_RUN == true ]]; then
echo " [dry-run] Would clear $crash_count crash report(s)"
else
rm -rf "$crash_dir"
log_ok "Cleared $crash_count crash report(s)"
fi
fi
fi
}
# Fix 5: Aggressive cleaning (optional)
fix_aggressive() {
if [[ $AGGRESSIVE != true ]]; then
return
fi
log_warn "Applying aggressive fixes (may lose some site data)..."
local aggressive_paths=(
"$THORIUM_CONFIG_DIR/Default/Service Worker"
"$THORIUM_CONFIG_DIR/Default/Cache"
"$THORIUM_CONFIG_DIR/Default/Code Cache"
"$THORIUM_CONFIG_DIR/Default/IndexedDB"
"$THORIUM_CONFIG_DIR/BrowserMetrics"
"$THORIUM_CONFIG_DIR/component_crx_cache"
)
for path in "${aggressive_paths[@]}"; do
remove_if_exists "$path"
done
# Backup potentially corrupted databases
local db_files=(
"$THORIUM_CONFIG_DIR/Default/Web Data"
"$THORIUM_CONFIG_DIR/Default/History"
)
for db in "${db_files[@]}"; do
if [[ -f $db ]]; then
log_info "Checking database: $(basename "$db")"
# Simple corruption check - if sqlite3 can't open it, back it up
if command -v sqlite3 &> /dev/null; then
if ! sqlite3 "$db" "PRAGMA integrity_check;" &> /dev/null; then
log_warn "Database may be corrupted: $(basename "$db")"
backup_if_exists "$db"
fi
fi
fi
done
}
# Test if Thorium starts successfully
test_thorium() {
if [[ $TEST_AFTER != true ]]; then
return
fi
log_info "Testing Thorium startup..."
if [[ $DRY_RUN == true ]]; then
echo " [dry-run] Would test thorium-browser startup"
return
fi
# Start Thorium in background
thorium-browser &> /dev/null &
local pid=$!
# Wait a few seconds and check if it's still running
sleep 4
if kill -0 "$pid" 2> /dev/null; then
log_ok "Thorium started successfully! (PID: $pid)"
echo -e "${GREEN}Fix successful!${NC} Thorium is now running."
# Offer to keep it running or kill it
read -r -p "Keep browser running? [Y/n] " response
case "$response" in
[nN]*)
kill "$pid" 2> /dev/null || true
log_info "Browser closed"
;;
*)
log_info "Browser left running"
;;
esac
else
log_error "Thorium still crashing after fixes"
echo -e "${RED}Standard fixes did not resolve the issue.${NC}"
echo ""
echo "Try these additional steps:"
echo " 1. Run with --aggressive flag for deeper cleaning"
echo " 2. Test with fresh profile: thorium-browser --user-data-dir=/tmp/thorium-test"
echo " 3. Reinstall: yay -S thorium-browser-bin"
echo " 4. Check NVIDIA drivers: nvidia-smi"
exit 1
fi
}
# Main execution
main() {
echo "========================================"
echo " Thorium Browser Fix Script"

View File

@ -0,0 +1,179 @@
#!/bin/bash
# Individual Thorium profile repairs: state, locks, caches.
#
# Sourced by fix_thorium.sh; split out to keep it under the 250-line cap.
# Sourced rather than run, so it inherits the caller's strict mode and
# the helper functions and variables defined above the source line.
# Fix 1: Handle corrupted Local State file (most common crash cause)
fix_local_state() {
log_info "Checking Local State file..."
local local_state="$THORIUM_CONFIG_DIR/Local State"
if [[ -f $local_state ]]; then
# Check if it's valid JSON
if ! python3 -c "import json; json.load(open('$local_state'))" 2> /dev/null; then
log_warn "Local State file appears corrupted"
backup_if_exists "$local_state"
else
# Even if valid JSON, back it up as it can still cause crashes
log_info "Local State exists - backing up (common crash source)"
backup_if_exists "$local_state"
fi
else
log_info "No Local State file found (OK for fresh install)"
fi
}
# Fix 2: Clear singleton lock files
fix_singleton_locks() {
log_info "Clearing singleton lock files..."
local locks=(
"$THORIUM_CONFIG_DIR/SingletonLock"
"$THORIUM_CONFIG_DIR/SingletonSocket"
"$THORIUM_CONFIG_DIR/SingletonCookie"
)
local cleared=0
for lock in "${locks[@]}"; do
if remove_if_exists "$lock"; then
((cleared++)) || true
fi
done
if [[ $cleared -eq 0 ]]; then
log_info "No stale lock files found"
fi
}
# Fix 3: Clear GPU cache
fix_gpu_cache() {
log_info "Clearing GPU cache..."
local gpu_paths=(
"$THORIUM_CONFIG_DIR/GPUCache"
"$THORIUM_CONFIG_DIR/Default/GPUCache"
"$THORIUM_CONFIG_DIR/ShaderCache"
"$THORIUM_CONFIG_DIR/Default/ShaderCache"
)
local cleared=0
for cache in "${gpu_paths[@]}"; do
if remove_if_exists "$cache"; then
((cleared++)) || true
fi
done
if [[ $cleared -eq 0 ]]; then
log_info "No GPU cache to clear"
fi
}
# Fix 4: Clear crash reports (can accumulate and cause issues)
fix_crash_reports() {
log_info "Clearing old crash reports..."
local crash_dir="$THORIUM_CONFIG_DIR/Crash Reports"
if [[ -d $crash_dir ]]; then
local crash_count
crash_count=$(find "$crash_dir" -type f 2> /dev/null | wc -l)
if [[ $crash_count -gt 0 ]]; then
if [[ $DRY_RUN == true ]]; then
echo " [dry-run] Would clear $crash_count crash report(s)"
else
rm -rf "$crash_dir"
log_ok "Cleared $crash_count crash report(s)"
fi
fi
fi
}
# Fix 5: Aggressive cleaning (optional)
fix_aggressive() {
if [[ $AGGRESSIVE != true ]]; then
return
fi
log_warn "Applying aggressive fixes (may lose some site data)..."
local aggressive_paths=(
"$THORIUM_CONFIG_DIR/Default/Service Worker"
"$THORIUM_CONFIG_DIR/Default/Cache"
"$THORIUM_CONFIG_DIR/Default/Code Cache"
"$THORIUM_CONFIG_DIR/Default/IndexedDB"
"$THORIUM_CONFIG_DIR/BrowserMetrics"
"$THORIUM_CONFIG_DIR/component_crx_cache"
)
for path in "${aggressive_paths[@]}"; do
remove_if_exists "$path"
done
# Backup potentially corrupted databases
local db_files=(
"$THORIUM_CONFIG_DIR/Default/Web Data"
"$THORIUM_CONFIG_DIR/Default/History"
)
for db in "${db_files[@]}"; do
if [[ -f $db ]]; then
log_info "Checking database: $(basename "$db")"
# Simple corruption check - if sqlite3 can't open it, back it up
if command -v sqlite3 &> /dev/null; then
if ! sqlite3 "$db" "PRAGMA integrity_check;" &> /dev/null; then
log_warn "Database may be corrupted: $(basename "$db")"
backup_if_exists "$db"
fi
fi
fi
done
}
# Test if Thorium starts successfully
test_thorium() {
if [[ $TEST_AFTER != true ]]; then
return
fi
log_info "Testing Thorium startup..."
if [[ $DRY_RUN == true ]]; then
echo " [dry-run] Would test thorium-browser startup"
return
fi
# Start Thorium in background
thorium-browser &> /dev/null &
local pid=$!
# Wait a few seconds and check if it's still running
sleep 4
if kill -0 "$pid" 2> /dev/null; then
log_ok "Thorium started successfully! (PID: $pid)"
echo -e "${GREEN}Fix successful!${NC} Thorium is now running."
# Offer to keep it running or kill it
read -r -p "Keep browser running? [Y/n] " response
case "$response" in
[nN]*)
kill "$pid" 2> /dev/null || true
log_info "Browser closed"
;;
*)
log_info "Browser left running"
;;
esac
else
log_error "Thorium still crashing after fixes"
echo -e "${RED}Standard fixes did not resolve the issue.${NC}"
echo ""
echo "Try these additional steps:"
echo " 1. Run with --aggressive flag for deeper cleaning"
echo " 2. Test with fresh profile: thorium-browser --user-data-dir=/tmp/thorium-test"
echo " 3. Reinstall: yay -S thorium-browser-bin"
echo " 4. Check NVIDIA drivers: nvidia-smi"
exit 1
fi
}
# Main execution