setup-opds.sh: add reconfigure option to update existing OPDS setup, improve Caddyfile generation for auto and manual SSL

This commit is contained in:
Justin Oros
2026-04-20 14:45:08 -07:00
parent 9dc2b221d3
commit dfa3c1ce6d

View File

@@ -26,9 +26,7 @@ if ! command -v docker >/dev/null 2>&1; then
else
curl -fsSL https://get.docker.com | bash
fi
if ! command -v docker >/dev/null 2>&1; then
die "Docker installation failed."
fi
command -v docker >/dev/null 2>&1 || die "Docker installation failed."
info "Docker installed."
else
die "Docker is required. Aborting."
@@ -37,6 +35,32 @@ fi
header "TinyBoard OPDS Setup"
echo ""
EXISTING_SERVER=""
if docker ps -a --format '{{.Names}}' 2>/dev/null | grep -qE '^(dir2opds|stump)$'; then
EXISTING_SERVER=$(docker ps -a --format '{{.Names}}' 2>/dev/null | grep -E '^(dir2opds|stump)$' | head -1)
fi
if [ -n "$EXISTING_SERVER" ]; then
echo "An existing OPDS server was found: ${EXISTING_SERVER}"
echo ""
echo " 1) Reconfigure — update books path, domain, SSL, or auth"
echo " 2) Fresh install — remove existing and start over"
echo " q) Quit"
echo ""
read -rp "Choose [1/2/q]: " RECONF_CHOICE
case "$RECONF_CHOICE" in
1) info "Reconfiguring existing setup..." ;;
2)
docker rm -f dir2opds stump caddy-opds 2>/dev/null || true
info "Existing containers removed."
;;
q|Q) exit 0 ;;
*) die "Invalid choice." ;;
esac
echo ""
fi
echo "Choose an OPDS server:"
echo " 1) dir2opds — lightweight, no database, serves files directly from a folder"
echo " 2) Stump — full-featured book/comic server with web UI and OPDS support"
@@ -69,8 +93,8 @@ read -rp "Hub user [armbian]: " HUB_USER
HUB_USER="${HUB_USER:-armbian}"
HUB_HOME="/home/$HUB_USER"
read -rp "Path to books directory [${HUB_HOME}/mnt/grace/st/data/books]: " BOOKS_PATH
BOOKS_PATH="${BOOKS_PATH:-${HUB_HOME}/mnt/grace/st/data/books}"
read -rp "Path to books directory [${HUB_HOME}/mnt/grace/st/data/books/justin]: " BOOKS_PATH
BOOKS_PATH="${BOOKS_PATH:-${HUB_HOME}/mnt/grace/st/data/books/justin}"
[ -d "$BOOKS_PATH" ] || die "Books directory not found: $BOOKS_PATH"
echo ""
@@ -153,12 +177,12 @@ info "$OPDS_SERVER started on internal network opds-net."
header "Writing Caddyfile"
CADDY_DIR="${OPDS_DIR}/caddy"
mkdir -p "$CADDY_DIR"
mkdir -p "${CADDY_DIR}/data" "${CADDY_DIR}/config"
if [ "$AUTO_HTTPS" = true ]; then
TLS_BLOCK=""
else
if [ "$AUTO_HTTPS" = false ]; then
TLS_BLOCK=" tls ${CERT_PATH} ${KEY_PATH}"
else
TLS_BLOCK=""
fi
if [[ "${USE_AUTH,,}" == "y" ]]; then
@@ -172,25 +196,38 @@ else
AUTH_BLOCK=""
fi
if [ "$AUTO_HTTPS" = false ]; then
cat > "${CADDY_DIR}/Caddyfile" << EOF
$([ "$AUTO_HTTPS" = false ] && echo '{
{
auto_https off
}
:80 {
redir https://{host}{uri} permanent
}
')
${OPDS_DOMAIN} {
encode gzip
${TLS_BLOCK:+$TLS_BLOCK
}${AUTH_BLOCK:+$AUTH_BLOCK
} reverse_proxy http://${OPDS_SERVER}:${OPDS_INTERNAL_PORT} {
${TLS_BLOCK}
${AUTH_BLOCK}
reverse_proxy http://${OPDS_SERVER}:${OPDS_INTERNAL_PORT} {
header_up Host {host}
header_up X-Real-IP {remote}
}
}
EOF
else
cat > "${CADDY_DIR}/Caddyfile" << EOF
${OPDS_DOMAIN} {
encode gzip
${AUTH_BLOCK}
reverse_proxy http://${OPDS_SERVER}:${OPDS_INTERNAL_PORT} {
header_up Host {host}
header_up X-Real-IP {remote}
}
}
EOF
fi
info "Caddyfile written to ${CADDY_DIR}/Caddyfile"
@@ -217,6 +254,8 @@ else
-p 80:80 \
-p 443:443 \
-v "${CADDY_DIR}/Caddyfile:/etc/caddy/Caddyfile:ro" \
-v "${CADDY_DIR}/data:/data" \
-v "${CADDY_DIR}/config:/config" \
-v "${CERT_DIR}:/certs:ro" \
caddy:alpine
fi